博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring BOOT PERFORMANCE
阅读量:6692 次
发布时间:2019-06-25

本文共 4950 字,大约阅读时间需要 16 分钟。

转自:

官方优化文档: 

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  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@EnableAutoConfigurationpublic 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:

DispatcherServletAutoConfigurationEmbeddedServletContainerAutoConfigurationErrorMvcAutoConfigurationHttpEncodingAutoConfigurationHttpMessageConvertersAutoConfigurationJacksonAutoConfigurationJmxAutoConfigurationMultipartAutoConfigurationServerPropertiesAutoConfigurationPropertyPlaceholderAutoConfigurationThymeleafAutoConfigurationWebMvcAutoConfigurationWebSocketAutoConfiguration

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  to get a performance improvement. Firstly, exclude Tomcat from your dependencies:

org.springframework.boot
spring-boot-starter-tomcat

Add Undertow:

org.springframework.boot
spring-boot-starter-undertow

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

REFERENCES

RELATED

RECENT

  •  Mar 20
  •  Mar 13
  •  Mar 4

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

你可能感兴趣的文章
win10常用快捷键
查看>>
vmware搭建vSAN提示磁盘不合格或者看不到磁盘的解决办法
查看>>
HashMap和Hashtable的区别
查看>>
Oracle EBS-SQL (INV-5):检查期间拉式物料领用记录数.sql
查看>>
Python之with语句原理
查看>>
在Window环境下多线程与CPU资源分配原则
查看>>
20170303新的开始
查看>>
Python--day25--复习(单继承和多继承的总结)
查看>>
@Html.EditFor()不能添加“只读”html属性;以及disable属性的坑
查看>>
Logger日志级别说明及设置方法、说明
查看>>
7-1 列出连通集 (25 分)
查看>>
Mybatis之Mapper动态代理
查看>>
【转】楼天城楼教主的acm心路历程(作为励志用)
查看>>
vw、vh、vmin、vmax 的含义
查看>>
04.设计模式_抽象工厂模式
查看>>
vue项目搭建
查看>>
c lang codesnippets
查看>>
Machine Learning
查看>>
Ext概述
查看>>
LeetCode – Refresh – Populating Next Right Pointers in Each Node I and II
查看>>