淺談Spring Boot Web 應(yīng)用性能優(yōu)化
默認(rèn)情況下,Spring Boot Web 應(yīng)用會(huì)裝配一些功能組件 Bean。
在大多數(shù) Web 應(yīng)用場(chǎng)景下,可以選擇性地關(guān)閉一下自動(dòng)裝配的Spring 組件 Bean,以達(dá)到提升性能的目的。
配置項(xiàng)優(yōu)化
Spring Boot Web 應(yīng)用加速 完整配置項(xiàng)
management.add-application-context-header = false spring.mvc.formcontent.putfilter.enabled = false spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration
配置項(xiàng)匯總
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration
關(guān)閉 Web 請(qǐng)求跟蹤 自動(dòng)裝配
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
顧名思義,該自動(dòng)裝配用跟蹤 Web 請(qǐng)求,通過(guò)Servlet Filter org.springframework.boot.actuate.trace.WebRequestTraceFilter
記錄請(qǐng)求的信息(如:請(qǐng)求方法、請(qǐng)求頭以及請(qǐng)求路徑等),其計(jì)算的過(guò)程存在一定的開銷,使用場(chǎng)景罕見(jiàn),故可選擇關(guān)閉。
配置項(xiàng)
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration
當(dāng)org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
關(guān)閉后,其請(qǐng)求信息存儲(chǔ)介質(zhì)org.springframework.boot.actuate.trace.TraceRepository
沒(méi)有存在的必要,故可選擇關(guān)閉。
配置項(xiàng)
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration
關(guān)閉 Web 請(qǐng)求結(jié)果指標(biāo) 自動(dòng)裝配
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration
該組件將自動(dòng)裝配org.springframework.boot.actuate.autoconfigure.MetricsFilter
,該 Filter主要記錄Web 請(qǐng)求結(jié)果指標(biāo)(如:相應(yīng)狀態(tài)碼、請(qǐng)求方法執(zhí)行時(shí)間等),該信息一定程度上與反向代理服務(wù)器(nginx)功能重疊,故可選擇關(guān)閉。
配置項(xiàng)
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration
可關(guān)閉 Servlet Web 組件
org.springframework.web.filter.HttpPutFormContentFilter
引入版本
org.springframework.web.filter.HttpPutFormContentFilter
由 Spring Framework 3.1 版本引入,分發(fā)在 org.springframework:spring-web
中。
使用場(chǎng)景
通常 Web 場(chǎng)景中,瀏覽器通過(guò) HTTP GET 或者 POST 請(qǐng)求 提交 Form 數(shù)據(jù),而非瀏覽器客戶端(如應(yīng)用程序)可能通過(guò) HTTP PUT 請(qǐng)求來(lái)實(shí)現(xiàn)。
當(dāng) HTTP 請(qǐng)求頭Content-Type 為 application/x-www-form-urlencoded 時(shí),F(xiàn)orm 數(shù)據(jù)被 encoded。而 Servlet 規(guī)范中, ServletRequest.getParameter*()方法僅對(duì) HTTP POST 方法支持請(qǐng)求參數(shù)的獲取,如:
public intetfacce ServletRequest { ...... public String getParameter(String name); public Enumeration<String> getParameterNames(); public String[] getParameterValues(String name); public Map<String, String[]> getParameterMap(); ...... }
故 以上方法無(wú)法支持 HTTP PUT 或 HTTP PATCH 請(qǐng)求方法(請(qǐng)求頭Content-Type
為application/x-www-form-urlencoded
)。
org.springframework.web.filter.HttpPutFormContentFilter
正是這種場(chǎng)景的解決方案。
Spring Boot 默認(rèn)場(chǎng)景下,將org.springframework.web.filter.HttpPutFormContentFilter
被org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
自動(dòng)裝配,以下為 Spring Boot1.4.1.RELEASE 以及更好版本定義(可能存在一定的差異):
@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration { ...... @Bean @ConditionalOnMissingBean(HttpPutFormContentFilter.class) @ConditionalOnProperty(prefix = "spring.mvc.formcontent.putfilter", name = "enabled", matchIfMissing = true) public OrderedHttpPutFormContentFilter httpPutFormContentFilter() { return new OrderedHttpPutFormContentFilter(); } ...... }
綜上所述,org.springframework.web.filter.HttpPutFormContentFilter
在絕大多數(shù) Web 使用場(chǎng)景下為非必須組件。
配置項(xiàng)
如果應(yīng)用依賴 Spring Boot 版本 為 1.4.1.RELEASE 以及更高的版本,可通過(guò)如下配置,進(jìn)行將 org.springframework.web.filter.HttpPutFormContentFilter
關(guān)閉:
spring.mvc.formcontent.putfilter.enabled = false
org.springframework.web.filter.HiddenHttpMethodFilter
引入版本
org.springframework.web.filter.HiddenHttpMethodFilter
由 SpringFramework 3.0 版本引入,分發(fā)在org.springframework:spring-web
中。
使用場(chǎng)景
當(dāng) Web 服務(wù)端同一資源(URL)提供了多請(qǐng)求方法的實(shí)現(xiàn),例如 URI :/update 提供了HTTP POST 以及 HTTP PUT 實(shí)現(xiàn)),通常 Web 場(chǎng)景中,瀏覽器僅支持 HTTP GET或者 POST 請(qǐng)求方法,這樣的話,瀏覽器無(wú)法發(fā)起 HTTP PUT 請(qǐng)求。
為了瀏覽器可以消費(fèi) HTTP PUT 資源, 需要在服務(wù)端將 HTTP POST 轉(zhuǎn)化成HTTP PUT 請(qǐng)求,為了解決這類問(wèn)題,Spring 引入org.springframework.web.filter.HiddenHttpMethodFilter Web
組件。
當(dāng)瀏覽器 發(fā)起 HTTP POST 請(qǐng)求時(shí),可通過(guò)增加請(qǐng)求參數(shù)(默認(rèn)參數(shù)名稱:"_method")的方式,進(jìn)行HTTP 請(qǐng)求方法切換,
org.springframework.web.filter.HiddenHttpMethodFilter
獲取參數(shù)"_method"值后,將參數(shù)值作為HttpServletRequest#getMethod()
的返回值,給后續(xù) Servlet實(shí)現(xiàn)使用。
出于通用性的考慮,org.springframework.web.filter.HiddenHttpMethodFilter
通過(guò)調(diào)用 #setMethodParam(String)
方法,來(lái)修改轉(zhuǎn)換請(qǐng)求方法的參數(shù)名稱。
Spring Boot 默認(rèn)場(chǎng)景下,將org.springframework.web.filter.HttpPutFormContentFilter
被org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
自動(dòng)裝配,以下為 Spring Boot 1.4.1.RELEASE 以及更好版本定義(可能存在一定的差異):
@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration { ...... @Bean @ConditionalOnMissingBean(HiddenHttpMethodFilter.class) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() { return new OrderedHiddenHttpMethodFilter(); } ...... }
綜上所述,org.springframework.web.filter.HiddenHttpMethodFilter 也是特殊場(chǎng)景下所需,故可以關(guān)閉之。
配置項(xiàng)
按目前最新的 Spring Boot 1.5.2.RELEASE 版本中實(shí)現(xiàn),也沒(méi)有提供類似spring.mvc.formcontent.putfilter.enabled 這樣的配置項(xiàng)關(guān)閉,無(wú)法關(guān)閉。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實(shí)現(xiàn)
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07Springboot全局異常捕獲及try catch區(qū)別解析
這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel,本篇示例當(dāng)中不僅僅有后端,而且還提供了前端html,html當(dāng)中利用js將后端 輸出流直接下載為文件,需要的朋友可以參考下2022-03-03Java中的線程池ThreadPoolExecutor深入解析
這篇文章主要介紹了Java中的線程池ThreadPoolExecutor深入解析,線程池,thread pool,是一種線程使用模式,線程池維護(hù)著多個(gè)線程,等待著監(jiān)督管理者分配可并發(fā)執(zhí)行的任務(wù),需要的朋友可以參考下2023-11-11Spring?Boot指標(biāo)監(jiān)控及日志管理示例詳解
Spring Boot Actuator可以幫助程序員監(jiān)控和管理SpringBoot應(yīng)用,比如健康檢查、內(nèi)存使用情況統(tǒng)計(jì)、線程使用情況統(tǒng)計(jì)等,這篇文章主要介紹了Spring?Boot指標(biāo)監(jiān)控及日志管理,需要的朋友可以參考下2023-11-11springboot AutoConfigureAfter控制Bean的注入順序方法詳解
這個(gè)文章主要介紹一下@AutoConfigureAfter在spring框架中的作用,在使用過(guò)程中,很多開發(fā)人員在使用它的時(shí)候都出現(xiàn)了問(wèn)題,問(wèn)題比較多的就是它們的注冊(cè)順序總不是我們預(yù)期的,下面介紹一下正常的使用方法,感興趣的朋友一起看看吧2024-05-05Spring?Boot如何在加載bean時(shí)優(yōu)先選擇我
這篇文章主要介紹了Spring?Boot如何在加載bean時(shí)優(yōu)先選擇我,在?Spring?Boot?應(yīng)用程序中,我們可以采取三種方式實(shí)現(xiàn)自己的?bean?優(yōu)先加載,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03