淺談Spring Boot Web 應(yīng)用性能優(yōu)化
默認(rèn)情況下,Spring Boot Web 應(yīng)用會裝配一些功能組件 Bean。
在大多數(shù) Web 應(yīng)用場景下,可以選擇性地關(guān)閉一下自動裝配的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 請求跟蹤 自動裝配
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
顧名思義,該自動裝配用跟蹤 Web 請求,通過Servlet Filter org.springframework.boot.actuate.trace.WebRequestTraceFilter 記錄請求的信息(如:請求方法、請求頭以及請求路徑等),其計(jì)算的過程存在一定的開銷,使用場景罕見,故可選擇關(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)閉后,其請求信息存儲介質(zhì)org.springframework.boot.actuate.trace.TraceRepository沒有存在的必要,故可選擇關(guān)閉。
配置項(xiàng)
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration
關(guān)閉 Web 請求結(jié)果指標(biāo) 自動裝配
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration
該組件將自動裝配org.springframework.boot.actuate.autoconfigure.MetricsFilter,該 Filter主要記錄Web 請求結(jié)果指標(biāo)(如:相應(yīng)狀態(tài)碼、請求方法執(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 中。
使用場景
通常 Web 場景中,瀏覽器通過 HTTP GET 或者 POST 請求 提交 Form 數(shù)據(jù),而非瀏覽器客戶端(如應(yīng)用程序)可能通過 HTTP PUT 請求來實(shí)現(xiàn)。
當(dāng) HTTP 請求頭Content-Type 為 application/x-www-form-urlencoded 時(shí),F(xiàn)orm 數(shù)據(jù)被 encoded。而 Servlet 規(guī)范中, ServletRequest.getParameter*()方法僅對 HTTP POST 方法支持請求參數(shù)的獲取,如:
public intetfacce ServletRequest {
......
public String getParameter(String name);
public Enumeration<String> getParameterNames();
public String[] getParameterValues(String name);
public Map<String, String[]> getParameterMap();
......
}
故 以上方法無法支持 HTTP PUT 或 HTTP PATCH 請求方法(請求頭Content-Type為application/x-www-form-urlencoded)。
org.springframework.web.filter.HttpPutFormContentFilter 正是這種場景的解決方案。
Spring Boot 默認(rèn)場景下,將org.springframework.web.filter.HttpPutFormContentFilter 被org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 自動裝配,以下為 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 使用場景下為非必須組件。
配置項(xiàng)
如果應(yīng)用依賴 Spring Boot 版本 為 1.4.1.RELEASE 以及更高的版本,可通過如下配置,進(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 中。
使用場景
當(dāng) Web 服務(wù)端同一資源(URL)提供了多請求方法的實(shí)現(xiàn),例如 URI :/update 提供了HTTP POST 以及 HTTP PUT 實(shí)現(xiàn)),通常 Web 場景中,瀏覽器僅支持 HTTP GET或者 POST 請求方法,這樣的話,瀏覽器無法發(fā)起 HTTP PUT 請求。
為了瀏覽器可以消費(fèi) HTTP PUT 資源, 需要在服務(wù)端將 HTTP POST 轉(zhuǎn)化成HTTP PUT 請求,為了解決這類問題,Spring 引入org.springframework.web.filter.HiddenHttpMethodFilter Web 組件。
當(dāng)瀏覽器 發(fā)起 HTTP POST 請求時(shí),可通過增加請求參數(shù)(默認(rèn)參數(shù)名稱:"_method")的方式,進(jìn)行HTTP 請求方法切換,
org.springframework.web.filter.HiddenHttpMethodFilter 獲取參數(shù)"_method"值后,將參數(shù)值作為HttpServletRequest#getMethod()的返回值,給后續(xù) Servlet實(shí)現(xiàn)使用。
出于通用性的考慮,org.springframework.web.filter.HiddenHttpMethodFilter通過調(diào)用 #setMethodParam(String) 方法,來修改轉(zhuǎn)換請求方法的參數(shù)名稱。
Spring Boot 默認(rèn)場景下,將org.springframework.web.filter.HttpPutFormContentFilter 被org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 自動裝配,以下為 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 也是特殊場景下所需,故可以關(guān)閉之。
配置項(xiàng)
按目前最新的 Spring Boot 1.5.2.RELEASE 版本中實(shí)現(xiàn),也沒有提供類似spring.mvc.formcontent.putfilter.enabled 這樣的配置項(xiàng)關(guān)閉,無法關(guān)閉。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java本服務(wù)如何調(diào)用本服務(wù)接口
這篇文章主要介紹了java本服務(wù)如何調(diào)用本服務(wù)接口問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring從@Aspect到Advisor使用演示實(shí)例
這篇文章主要介紹了Spring從@Aspect到Advisor使用演示實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
@JsonProperty和@JSONField注解的區(qū)別解析(最新)
Jackson是一款優(yōu)秀的JSON解析庫,添加了依賴之后就可以使用對應(yīng)的注解,讓我們能夠自由的將Java對象和JSON做轉(zhuǎn)換,這篇文章主要介紹了@JsonProperty和@JSONField注解的區(qū)別,需要的朋友可以參考下2024-04-04
Java實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用java語言實(shí)現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
關(guān)于socket發(fā)送數(shù)據(jù)需要注意的問題
這篇文章主要介紹了關(guān)于socket發(fā)送數(shù)據(jù)需要注意的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
探究springboot中的TomcatMetricsBinder
springboot的TomcatMetricsBinder主要是接收ApplicationStartedEvent然后創(chuàng)建TomcatMetrics執(zhí)行bindTo進(jìn)行注冊,TomcatMetrics主要注冊了globalRequest、servlet、cache、threadPool、session相關(guān)的指標(biāo),本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格
這篇文章主要介紹了java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
java應(yīng)用程序如何自定義log4j配置文件的位置
這篇文章主要介紹了java應(yīng)用程序如何自定義log4j配置文件的位置,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解Java Callable接口實(shí)現(xiàn)多線程的方式
這篇文章主要介紹了詳解Java Callable接口實(shí)現(xiàn)多線程的方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

