Spring Boot Web 應用性能優化

默認狀況下,Spring Boot Web 應用會裝配一些功能組件 Bean。java

在大多數 Web 應用場景下,能夠選擇性地關閉一下自動裝配的Spring 組件 Bean,以達到提高性能的目的。nginx

配置項優化

Spring Boot Web 應用加速 完整配置項

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

配置項彙總

spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration

關閉 Web 請求跟蹤 自動裝配

org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration

顧名思義,該自動裝配用跟蹤 Web 請求,經過Servlet Filter org.springframework.boot.actuate.trace.WebRequestTraceFilter 記錄請求的信息(如:請求方法、請求頭以及請求路徑等),其計算的過程存在必定的開銷,使用場景罕見,故可選擇關閉。web

  • 配置項
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration

org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration

org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration關閉後,其請求信息存儲介質org.springframework.boot.actuate.trace.TraceRepository沒有存在的必要,故可選擇關閉。spring

  • 配置項
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration

關閉 Web 請求結果指標 自動裝配

org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration

該組件將自動裝配org.springframework.boot.actuate.autoconfigure.MetricsFilter,該 Filtersegmentfault

主要記錄Web 請求結果指標(如:相應狀態碼、請求方法執行時間等),該信息必定程度上與反向代理服務器(nginx)功能重疊,故可選擇關閉。瀏覽器

  • 配置項
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration

可關閉 Servlet Web 組件

org.springframework.web.filter.HttpPutFormContentFilter

  • 引入版本

org.springframework.web.filter.HttpPutFormContentFilter 由 Spring
Framework 3.1 版本引入,分發在 org.springframework:spring-web 中。性能優化

  • 使用場景

一般 Web 場景中,瀏覽器經過 HTTP GET 或者 POST 請求 提交 Form 數據,而非瀏覽
器客戶端(如應用程序)可能經過 HTTP PUT 請求來實現。服務器

當 HTTP 請求頭Content-Typeapplication/x-www-form-urlencoded
,Form 數據被 encoded。而 Servlet 規範中, ServletRequest.getParameter*()
方法僅對 HTTP POST 方法支持請求參數的獲取,如:websocket

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)。mvc

org.springframework.web.filter.HttpPutFormContentFilter 正是這種場景的解
決方案。

Spring Boot 默認場景下,將
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(HttpPutFormContentFilter.class)
    @ConditionalOnProperty(prefix = "spring.mvc.formcontent.putfilter", name = "enabled", matchIfMissing = true)
    public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
        return new OrderedHttpPutFormContentFilter();
    }

    ......

}

綜上所述,org.springframework.web.filter.HttpPutFormContentFilter 在絕大
多數 Web 使用場景下爲非必須組件。

  • 配置項

若是應用依賴 Spring Boot 版本 爲 1.4.1.RELEASE 以及更高的版本,可經過以下配置,
進行將 org.springframework.web.filter.HttpPutFormContentFilter 關閉:

spring.mvc.formcontent.putfilter.enabled = false

org.springframework.web.filter.HiddenHttpMethodFilter

  • 引入版本

org.springframework.web.filter.HiddenHttpMethodFilter 由 Spring
Framework 3.0 版本引入,分發在 org.springframework:spring-web 中。

  • 使用場景

當 Web 服務端同一資源(URL)提供了多請求方法的實現,例如 URI :/update 提供了
HTTP POST 以及 HTTP PUT 實現),一般 Web 場景中,瀏覽器僅支持 HTTP GET
或者 POST 請求方法,這樣的話,瀏覽器沒法發起 HTTP PUT 請求。

爲了瀏覽器能夠消費 HTTP PUT 資源, 須要在服務端將 HTTP POST 轉化成
HTTP PUT 請求,爲了解決這類問題,Spring 引入
org.springframework.web.filter.HiddenHttpMethodFilter Web 組件。

當瀏覽器 發起 HTTP POST 請求時,可經過增長請求參數(默認參數名稱:"_method")
的方式,進行HTTP 請求方法切換,
org.springframework.web.filter.HiddenHttpMethodFilter 獲取參數"_method"
值後,將參數值做爲 HttpServletRequest#getMethod()的返回值,給後續 Servlet
實現使用。

出於通用性的考慮,org.springframework.web.filter.HiddenHttpMethodFilter
經過調用 #setMethodParam(String) 方法,來修改轉換請求方法的參數名稱。

Spring Boot 默認場景下,將
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 也是特殊
場景下所需,故能夠關閉之。

  • 配置項

按目前最新的 Spring Boot 1.5.2.RELEASE 版本中實現,也沒有提供相似
spring.mvc.formcontent.putfilter.enabled 這樣的配置項關閉,沒法關閉。

瞭解更多?

更多關於 Spring Boot Web 應用性能優化內容,請參考《Java 微服務實戰系列課堂》,其中將有系統和深刻的討論。

相關文章
相關標籤/搜索