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:

  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:

tomcat

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:

undertow

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

转载于:https://www.cnblogs.com/fangyuan303687320/p/5631411.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值