Spring 框架給企業軟件開發者提供了常見問題的通用解決方案,包括那些在未來開發中沒有意識到的問題。但是,它構建的 J2EE 項目變得越來越臃腫,逐漸被 Spring Boot 所替代。Spring Boot 讓我們創建和運行項目變得更為迅速,現在已經有越來越多的人使用它。我們已經在幾個項目中使用了 Spring Boot ,今天我們就來一起討論一下如何改進 Spring Boot 應用的性能。
首先,從之前我在開發中遇到的一個問題說起。在一次查看項目運行日志的時候,我偶然發現了一個問題,日志里顯示這個項目總是加載 Velocity 模板引擎,但實際上這個項目是一個沒有 web 頁面的 REST Service 項目。于是我花了一點時間去尋找產生這個問題的原因,以及如何改進 Spring Boot 應用的性能。在查找了相關的資料后,我得出的結論如下:
組件自動掃描帶來的問題
默認情況下,我們會使用 @SpringBootApplication 注解來自動獲取的應用的配置信息,但這樣也會給應用帶來一些副作用。使用這個注解后,會觸發自動配置( auto-configuration )和 組件掃描 ( component scanning),這跟使用 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 三個注解的作用是一樣的。這樣做給開發帶來方便的同時,也會有兩方面的影響:
1、會導致項目啟動時間變長。當啟動一個大的應用程序,或將做大量的集成測試啟動應用程序時,影響會特別明顯。
2、會加載一些不需要的多余的實例(beans)。
3、會增加 CPU 消耗。
針對以上兩個情況,我們可以移除 @SpringBootApplication 和 @ComponentScan 兩個注解來禁用組件自動掃描,然后在我們需要的 bean 上進行顯式配置:
// 移除 @SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration 來替代
@Configuration
@EnableAutoConfiguration
public class SampleWebUiApplication {
// 
// 用 @Bean 注解明確顯式配置,以便被 Spring 掃描到
@Bean
public MessageController messageController(MessageRepository messageRepository) {
return new MessageController(messageRepository);
} 如何避免組件自動掃描帶來的問題
我們在上面提到,@SpringBootApplication 注解的作用跟 @EnableAutoConfiguration 注解的作用是相當的,那就意味著它也能帶來上述的三個問題。要避免這些問題,我們就要知道我們需要的組件列表是哪些,可以用 -Ddebug 的方式來幫助我們明確地定位:
mvn spring-boot:run -Ddebug … ========================= AUTO-CONFIGURATION REPORT ========================= Positive matches: ----------------- DispatcherServletAutoConfiguration - @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition) - found web application StandardServletEnvironment (OnWebApplicationCondition) ...
接著拷貝 Positive matches
中列出的信息:
DispatcherServletAutoConfiguration
EmbeddedServletContainerAutoConfiguration
ErrorMvcAutoConfiguration
HttpEncodingAutoConfiguration
HttpMessageConvertersAutoConfiguration
JacksonAutoConfiguration
JmxAutoConfiguration
MultipartAutoConfiguration
ServerPropertiesAutoConfiguration
PropertyPlaceholderAutoConfiguration
ThymeleafAutoConfiguration
WebMvcAutoConfiguration
WebSocketAutoConfiguration
然后來更新項目配置,顯式地引入這些組件,引入之后,再運行一下應用確保沒有錯誤發生:
@Configuration
@Import({
DispatcherServletAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
HttpEncodingAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JacksonAutoConfiguration.class,
JmxAutoConfiguration.class,
MultipartAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
ThymeleafAutoConfiguration.class,
WebMvcAutoConfiguration.class,
WebSocketAutoConfiguration.class,
})
public class SampleWebUiApplication {}
在上面的代碼中,我們可以刪掉我們不需要的組件信息,來提高應用的性能,比如在我的項目中,不需要 JMX 和 WebSocket 功能,我就刪掉了它們。刪掉之后,再次運行項目,確保一切正常。
將Servlet容器變成Undertow
默認情況下,Spring Boot 使用 Tomcat 來作為內嵌的 Servlet 容器。我們可以啟動項目,然后用 VisualVM 或者 JConsole 來查看應用所占的內存情況:

以上是我使用 Spring Boot 的默認方式啟動應用后,用 VisualVM 監控到的內存的占用情況:堆內存占用 110M,16 個線程被開啟。
可以將 Web 服務器切換到 Undertow 來提高應用性能。Undertow 是一個采用 Java 開發的靈活的高性能 Web 服務器,提供包括阻塞和基于 NIO 的非堵塞機制。Undertow 是紅帽公司的開源產品,是 Wildfly 默認的 Web 服務器。首先,從依賴信息里移除 Tomcat 配置:
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
然后添加 Undertow:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
啟動項目后,用 VisualVM 監控到的信息顯示:堆內存占用 90M,13個線程被開啟。

總結
這些都是我們在項目開發中使用到的一些優化 Spring Boot 應用的小技巧,對于大的應用性能的提高還是很明顯的。大家可以嘗試一下,然后告訴我們你的測試結果。
最后,附上代碼,大家可以去這里下載:spring-boot-performance。
文中大部分內容參考英國一個架構師的博客 和 DZone 近期發布的文章,在此感謝兩位大牛。參考文章及鏈接:
(1)Spring Boot 性能優化:Spring Boot Performance;
(2)Spring Boot 內存優化:Spring Boot Memory Performance。
(3)https://www.techempower.com/benchmarks/;
(4)Spring 應用程序優化:Optimizing Spring Framework for App Engine Applications。