Spring Boot 的性能優化

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

如何避免組件自動掃描帶來的問題

我們在上面提到,@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)

...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

接着拷貝 Positive matches 中列出的信息:

DispatcherServletAutoConfiguration
EmbeddedServletContainerAutoConfiguration
ErrorMvcAutoConfiguration
HttpEncodingAutoConfiguration
HttpMessageConvertersAutoConfiguration
JacksonAutoConfiguration
JmxAutoConfiguration
MultipartAutoConfiguration
ServerPropertiesAutoConfiguration
PropertyPlaceholderAutoConfiguration
ThymeleafAutoConfiguration
WebMvcAutoConfiguration
WebSocketAutoConfiguration
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

然後來更新項目配置,顯式地引入這些組件,引入之後,再運行一下應用確保沒有錯誤發生:

@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 {
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在上面的代碼中,我們可以刪掉我們不需要的組件信息,來提高應用的性能,比如在我的項目中,不需要 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然後添加 Undertow:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

啓動項目後,用 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

OneAPM 爲您提供端到端的 Java 應用性能解決方案,我們支持所有常見的 Java 框架及應用服務器,助您快速發現系統瓶頸,定位異常根本原因。分鐘級部署,即刻體驗,Java 監控從來沒有如此簡單。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章