Spring BOOT PERFORMANCE
转自:http://www.alexecollins.com/spring-boot-performance/
官方优化文档: https://spring.io/blog/2015/12/10/spring-boot-memory-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.
REFERENCES
Spring BOOT PERFORMANCE的更多相关文章
- Spring Boot 性能优化
spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...
- Spring boot 内存优化
转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Sp ...
- Spring Boot Memory Performance
The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...
- 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用
这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- Spring Boot + Elasticsearch
spring data elasticsearch elasticsearch 2.0.0.RELEASE 2.2.0 1.4.0.M1 1.7.3 1.3.0.RELEASE 1.5.2 1.2.0 ...
- Spring Boot的一个测试用例
package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...
- Complete Guide for Spring Boot Actuator
You are here to learn about Spring Boot Actuator for collecting metrics about your production grade ...
- 使用Ratpack和Spring Boot打造高性能的JVM微服务应用
使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...
随机推荐
- shell学习总结之自定义函数
shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...
- Hbase region 某个regionserver挂掉后的处理
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwoAAACdCAMAAAAjbX91AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK
- oct(x) 将一个数字转化为8进制
>>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...
- 当 ITOA 遇上 OneAlert,企业可以至少每年节省 3600 小时!
每个工作日,一家大型企业都可能存在一两件优先级为 1 级的事件,五六件优先级为 2 级的事件和百来件优先级为 3 级的事件.试想一下,如果公司所有支持人员都要收到每个事件的通知--不想了,我好方!还能 ...
- JAVA客户端API调用memcached两种方式
1. memcached client for java客户端API:memcached client for java 引入jar包:java-memcached-2.6.2.jar package ...
- IE10与IMG图片PNG显示不了 WP中的WebBrowser中无法查看PNG格式的图片
在IE10下,IMG的图片不能是PNG格式的,PNG格式显示不了,JPG显示就可以
- ASP.NET UpdatePanel实现点击按钮无刷新且执行js脚本
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3770779.html] *.aspx: <asp:ScriptManager ID=& ...
- JS单击隐藏界面元素
1. JS代码 <script type="text/javascript" language="javascript"> // function ...
- Spring事务管理--多个ORM框架在使用时的情况分析
公司的项目已经接近尾声了,总结一下项目中用到的技术,我发现项目中的有些东西还是挺模糊的,只是知道这么用就行了.并不清楚其中的原理.由于公司的项目比较老,是7年前的一个项目了,中间一直有人在维护,也是在 ...
- 应付期间 Payables Periods
(N) AP > Accounting > Control payables periods Click [Period Status] column to Open.