Spring BOOT PERFORMANCE
Posted Yuan.Net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring BOOT PERFORMANCE相关的知识,希望对你有一定的参考价值。
转自:http://www.alexecollins.com/spring-boot-performance/
SPRING BOOT PERFORMANCE
This is an article on how to improve the performance of Spring Boot applications. I‘ve recently been working on a new project. As we primarily use Java and Spring, we‘ve been looking at Spring Boot. It‘s allowed us to get up and running quickly.
Early on, I came across a problem with a prototype for one of our new applications. It was loading the Velocity web page template engine. I could not understand why – it was just some REST services, no web pages. I spent a bit of time looking into this issue, and how to improve the performance of Spring Boot applications, and this is what I found.
COMPONENT SCANNING SLOWS START-UP
By default, you may find yourself using the @SpringBootApplication
annotation to get your application configured automatically. This has a couple of side-effects. One is to enable component scanning. This looks through the classes to find ones annotated with Spring "stereotypes", such as @Component
. This is convenient, especially when you start out, but it has two side-effects:
- It slows application start-up time. This will have a greater impact if you have a large application, or a large number of integration tests that need to start up the application to run.
- It may load beans you don‘t want or need.
You can disable component scanning by removing the @SpringBootApplication
and @ComponentScan
annotations. You‘ll then need to make each bean explicit in your configuration.
// remove @SpringBootApplication and @ComponentScan, replace with @EnableAutoConfiguration
@Configuration
@EnableAutoConfiguration
public class SampleWebUiApplication {
// ...
// you must explicitly list all beans that were being component scanned @Bean
public MessageController messageController(MessageRepository messageRepository) {
return new MessageController(messageRepository);
}
AUTO-CONFIGURATION CAN LOAD MORE THAN YOU NEED
The @SpringBootApplication
annotation implies the @EnableAutoConfiguration
annotation. This enables auto-configuration. This can load components you don‘t need, slowing application start-up and increasing memory and CPU usage. Lets look at how to use this in a more controlled fashion.
If you start your application using -Ddebug
it‘ll print a report of the components it auto-configures:
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)
...
Copy the classes mentioned in the ""positive matches" section of the report:
DispatcherServletAutoConfiguration
EmbeddedServletContainerAutoConfiguration
ErrorMvcAutoConfiguration
HttpEncodingAutoConfiguration
HttpMessageConvertersAutoConfiguration
JacksonAutoConfiguration
JmxAutoConfiguration
MultipartAutoConfiguration
ServerPropertiesAutoConfiguration
PropertyPlaceholderAutoConfiguration
ThymeleafAutoConfiguration
WebMvcAutoConfiguration
WebSocketAutoConfiguration
Update your configuration to explicitly import them, and run your tests to make sure everything is OK.
@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 {
I can see that both JMX and web sockets are listed, but I know I‘m not using them. I can delete them, and any other dependencies I don‘t need, to get a performance improvement. Run your tests again to make sure everything is OK.
CHANGE SERVLET CONTAINER TO UNDERTOW
By default, Spring Boot uses Tomcat. Tomcat uses around 110mb of heap, and has ~16 threads:
Undertow is a lightweight servlet container from JBoss. You can switch to Undertow to get a performance improvement. Firstly, exclude Tomcat from your dependencies:
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
Add Undertow:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Undertow uses around 90MB and has ~13 threads:
CONCLUSION
These are a few small tips on improving the performance of your Spring Boot applications. The benefits are smaller for smaller applications, but for larger applications can quickly become pronounced. Try it out and tell me what you think.
As usual, the code is on Github.