提高 Spring Boot 启动速度

/ 转载 / 没有评论 / 651浏览

https://alexecollins.com/spring-boot-performance/

https://stackoverflow.com/questions/27230702/speed-up-spring-boot-startup-time


Spring Boot 性能

这是一篇关于如何提高 Spring Boot 应用程序性能的文章。我最近一直在做一个新项目。由于我们主要使用 Java 和 Spring,因此我们一直在研究Spring Boot它使我们能够快速启动并运行。

早些时候,我遇到了我们的一个新应用程序原型的问题。它正在加载 Velocity 网页模板引擎。我不明白为什么——它只是一些 REST 服务,没有网页。我花了一些时间研究这个问题,以及如何提高 Spring Boot 应用程序的性能,这就是我发现的。

组件扫描减慢启动速度

默认情况下,您可能会发现自己使用@SpringBootApplication注释来自动配置您的应用程序。这有几个副作用。一种是启用组件扫描这会查看类以查找使用 Spring“构造型”注释的类,例如@Component这很方便,尤其是在您刚开始时,但它有两个副作用:

  1. 它会减慢应用程序的启动时间。如果您有一个大型应用程序,或者需要启动应用程序运行的大量集成测试,这将产生更大的影响。
  2. 它可能会加载您不想要或不需要的 bean。

您可以通过删除@SpringBootApplication@ComponentScan注释来禁用组件扫描然后,您需要在配置中使每个 bean 显式。

// remove @SpringBootApplication and @ComponentScan, replace with @EnableAutoConfiguration
@Configuration
@EnableAutoConfiguration
public class SampleWebUiApplication {
<span class="c1" style="color: rgb(108, 139, 159); font-style: italic;">// ...</span>

<span class="c1" style="color: rgb(108, 139, 159); font-style: italic;">// you must explicitly list all beans that were being component scanned	@Bean</span>
<span class="kd" style="color: rgb(246, 221, 98); font-weight: bold;">public</span> <span class="n">MessageController</span> <span class="nf" style="color: rgb(168, 225, 254);">messageController</span><span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">(</span><span class="n">MessageRepository</span> <span class="n">messageRepository</span><span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">)</span> <span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">{</span>
	<span class="k" style="color: rgb(246, 221, 98); font-weight: bold;">return</span> <span class="k" style="color: rgb(246, 221, 98); font-weight: bold;">new</span> <span class="nf" style="color: rgb(168, 225, 254);">MessageController</span><span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">(</span><span class="n">messageRepository</span><span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">);</span>
<span class="o" style="color: rgb(77, 244, 255); font-weight: bold;">}</span>

自动配置可以加载比你需要的更多

@SpringBootApplication注解意味着@EnableAutoConfiguration注释。这将启用自动配置。这会加载您不需要的组件,减慢应用程序启动速度并增加内存和 CPU 使用率。让我们看看如何以更可控的方式使用它。

如果您使用-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)

...

复制报告的“正匹配”部分中提到的类:

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 和 Web 套接字,但我知道我没有使用它们。我可以删除它们以及我不需要的任何其他依赖项,以获得性能改进。再次运行测试以确保一切正常。

将 Servlet 容器更改为 Undertow

默认情况下,Spring Boot 使用 Tomcat。Tomcat 使用大约 110mb 的堆,并且有大约 16 个线程:

公猫

Undertow 是来自 JBoss 的轻量级 servlet 容器。您可以切换到 Undertow以获得性能改进。首先,从您的依赖项中排除 Tomcat:

<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>

添加暗流:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

Undertow 使用大约 90MB 并有大约 13 个线程:

暗流

结论

这些是提高 Spring Boot 应用程序性能的一些小技巧。对于较小的应用程序,好处较小,但对于较大的应用程序,好处很快就会变得明显。试试看,告诉我你的想法。

像往常一样,代码在 Github 上

参考