转自: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:

  1. 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.
  2. 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的更多相关文章

  1. Spring Boot 性能优化

    spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...

  2. Spring boot 内存优化

    转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Sp ...

  3. Spring Boot Memory Performance

    The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...

  4. 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...

  5. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  6. 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 ...

  7. Spring Boot的一个测试用例

    package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...

  8. Complete Guide for Spring Boot Actuator

    You are here to learn about Spring Boot Actuator for collecting metrics about your production grade ...

  9. 使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...

随机推荐

  1. asp.net(c#)有关 Session 操作的几个误区

    1. this.Session["username"] = null  HttpSessionState 内部使 用 NameObjectCollection 类型的集合对象来存储 ...

  2. SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurer、TilesViewResolver、<put-attribute>、<t:insertAttribute>)

    一. 1.定义TilesConfigurer.TilesViewResolver的bean 注意有tiles2和tiles3,这里使用tiles3 (1)java形式 package spittr.w ...

  3. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

    一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. package ...

  4. 忘记commit的一次教训

    由于业务需求,已经上线的系统新增加了一些需求,其中一个需求是,从一个SQLSERVER数据库导入数据到生产的ORCLE数据库, 由于我的失误导致系统上线后 生产的Oracle数据没有导入成功,但是在本 ...

  5. Android 去除list集合中重复项的几种方法

    因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List<"}; List<string> li2 = new List<string& ...

  6. ruby 编写迭代器

    class My def initialize(name,age) @name=name @age=age end def sayName puts @name end def sayAge puts ...

  7. xcode5编译过的xib 如何在xcode4.6下打开

    IOS7一出来,对应的xcode版本变成了5了,这次xcode升级比较大,特别是在源代码编译方面,苹果下足了功夫,编译时间不到原来的一半,忽然强烈觉得android在这方面需要加强啊: 其他不多说,X ...

  8. 编译安装nginx并修改版本头信息—参考实例

    今天做实验的时候,想起我那台yum安装的nginx+php-fpm+mysql服务器上的nginx版本有点低了,并且还要加两个第3方模块,就去nginx官网下载了最新稳定版nginx-1.0.6,好了 ...

  9. 【转】 Xcode基本操作 -- 不错

    原文网址:http://blog.csdn.net/phunxm/article/details/17044337 1.Xcode IDE概览 说明:从左到右,依次是“导航窗格(Navigator)- ...

  10. Sharepoint的网页(Page),网页解析(Parsing)与解析安全处理(Security)

    转:http://www.chawenti.com/articles/8592.html Microsoft SharePoint Foundation 中主要有两种类型的页面,分别是应用程序页(Ap ...