The Spring framework provides enterprise software developers with common solutions to common problems, including those that are not recognized in future development. However, the Java EE project it builds is becoming more and more bloated and gradually replaced by Spring Boot. Spring Boot lets us create and run projects more quickly, and now more and more people are using it. We've already used spring boot in several projects, so let's talk about how to improve the performance of spring boot applications today.
First of all, I started with a problem that I encountered in development. I stumbled across a problem when I looked at the project run log, which showed that the project always loaded the Velocity template engine, but actually this project was a REST Service project without a Web page. So I spent a little time looking for the cause of the problem and how to improve the performance of the Spring Boot application. After finding the relevant information, I came to the following conclusions:
Problems caused by automatic component scanning
By default, we use @SpringBootApplication annotations to automatically get configuration information for an app, but this also brings some side effects to your app. With this annotation, automatic configuration (auto-configuration) and component scanning (component scanning) are triggered, with the use of @Configuration, @EnableAutoConfiguration, and @ Componentscan three annotations work the same. This can be convenient for development, but it will also have two effects:
1, will cause the project start time to grow longer. When launching a large application, or will do a lot of integration testing to launch the application, the impact will be particularly noticeable.
2. Some unnecessary instances (beans) will be loaded.
3, will increase the CPU consumption.
For the above two cases, we can remove the @SpringBootApplication and @ComponentScan two annotations to disable auto-scan of the component and then explicitly configure it on the bean we need:
// 移除 @SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration 来替代@Configuration@EnableAutoConfigurationpublic class SampleWebUiApplication { // ... // 用 @Bean 注解明确显式配置,以便被 Spring 扫描到 @Bean public MessageController messageController(MessageRepository messageRepository) { return new MessageController(messageRepository); }
How to avoid problems caused by automatic component scanning
As we mentioned above, the role of @SpringBootApplication annotations is comparable to that of @EnableAutoConfiguration annotations, which means that it can also lead to three of these problems. To avoid these problems, we need to know what list of components we require, which can be used in a-ddebug way to help us pinpoint them:
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)...
Then copy the Positive matches information listed in:
DispatcherServletAutoConfigurationEmbeddedServletContainerAutoConfigurationErrorMvcAutoConfigurationHttpEncodingAutoConfigurationHttpMessageConvertersAutoConfigurationJacksonAutoConfigurationJmxAutoConfigurationMultipartAutoConfigurationServerPropertiesAutoConfigurationPropertyPlaceholderAutoConfigurationThymeleafAutoConfigurationWebMvcAutoConfigurationWebSocketAutoConfiguration
Then, to update the project configuration, introduce these components explicitly, and then run the application to ensure that no errors occur:
@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 {
In the above code, we can delete the component information that we do not need, to improve the performance of the application, such as in my project, do not need JMX and WebSocket function, I deleted them. After deleting, run the project again to make sure everything is OK.
Turn the servlet container into Undertow
By default, Spring Boot uses Tomcat as the inline Servlet container. We can start the project and then use VisualVM or JConsole to see how the app is taking up the memory:
The above is the memory usage monitored by VisualVM when I started the app using Spring boot's default: Heap memory consumption 110m,16 threads are turned on.
You can switch the WEB server to Undertow to improve application performance. Undertow is a flexible, high-performance WEB server developed in Java that provides non-clogging mechanisms including blocking and NIO-based. Undertow is a red Hat open source product and is the Wildfly default Web server. First, the Tomcat configuration is removed from the dependency information:
<exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion></exclusions>
Then add Undertow:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId></dependency>
After starting the project, the information monitored with VisualVM shows that the heap memory consumption 90m,13 threads are turned on.
Summarize
These are some of the optimizations that we have used in project development to optimize Spring
Boot application tips, for large application performance improvement is still very obvious. You can try it and tell us about your test results.
Finally, attach the code, you can go here to download: Spring-boot-performance.
Most of the content of this article refers to a British architect's blog and Dzone recently published articles, here to thank two Daniel. Reference articles and Links:
(1) Spring Boot performance optimization: Spring boot performance;
(2) Spring Boot memory optimization: Spring Boot Memories performance.
(3) https://www.techempower.com/benchmarks/;
(4) Spring application optimization: Optimizing Spring Framework for app Engine applications.
Resources:
1190000004252885
Http://www.cnblogs.com/softidea/p/5751596.html
"Springboot" Springboot performance optimization