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 ...
随机推荐
- android Failure [INSTALL_FAILED_OLDER_SDK] 安装apk失败
安装文件与运行环境的skd不匹配 打开源码目录下的AndroidManifest.xml文件,然后注释掉或者删除掉这行: <uses-sdk android:minSdkVersion=&quo ...
- nginx server_参数配置总结(转)
转:http://onlyzq.blog.51cto.com/1228/535279 Nginx中的server_name指令主要用于配置基于名称的虚拟主机,server_name指令在接到请求后的匹 ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- WCF - Developers Tools
For developing a WCF service application, there are mainly two tools – Microsoft Visual Studio and C ...
- 更换手机号或者更换手机后QQ设备锁的设置问题
更换手机号 一步到位,更改密保手机号,OK了 更换手机 老卡插入 登录QQ,OK了 更换手机号和手机 老卡插入新手机 登录QQ 新卡插入新手机 更改密保手机号,OK了
- hadoop运行流程分析源代码级
前言: 最近一直在分析hadoop的运行流程,我们查阅了大量的资料,虽然从感性上对这个流程有了一个认识但是我总是感觉对mapreduce的运行还是没有一个全面的认识,所以决定从源代码级别对mapred ...
- 函数xdes_find_bit
使用方法 free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE,hint % FSP_EXTENT_SIZE, mtr); /**************** ...
- eight - zoj 1217 poj 1077
学习了多位大牛的方法,看看到底能把时耗降到多少? A* // zojfulltest: 30000ms # include <stdio.h> # include <ctype.h& ...
- Memcached 两款.NET客户端的郁闷事儿
不久以后就要负责一个比较大的项目,有多大?反正就是挺大的.现在处于筹备阶段,我主要负责系统框架搭建,在系统缓存这一块决定采用Http运行时缓存+memcached. memcached 以前用过几次 ...
- Android 实用代码七段(一)
前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: htt ...