Spring BOOT PERFORMANCE

转自:http://www.alexecollins.com/spring-boot-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

RELATED

  1. Tutorial: Hibernate, JPA & Spring MVC - Part 2
  2. Tutorial: Integration Testing with Selenium - Part 2
  3. Tutorial: Integration Testing with Selenium - Part 1

RECENT

时间: 2024-10-07 02:27:19

Spring BOOT PERFORMANCE的相关文章

Spring Boot 性能优化

spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring Boot 让我们创建和运行项目变得更为迅速,现在已经有越来越多的人使用它.我们已经在几个项目中使用了 Spring Boot ,今天我们就来一起讨论一下如何改进 Spring Boot 应用的性能. 首先,从之前我在开发中遇到的一个问题说起.在一次查看项目运行日志的时候,我偶然发现了一个问题,日志

Spring boot 内存优化

转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Spring and Spring Boot are “heavyweight”, perhaps just because they allow apps to punch above their weight, providing a lot of features for not very muc

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

使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中文地址:使用Ratpack与Spring Boot构建高性能JVM微服务. 在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他

Spring Boot开发之流水无情(二)

上篇散仙写了一个很简单的入门级的Spring Boot的例子,没啥技术含量,不过,其实学任何东西只要找到第一个突破口,接下来的事情就好办了,人最怕什么? 我想莫过于干一件事情,没有下手的地方了,而当你一旦找到了这感觉,就可以很喜悦的顺藤摸瓜般的探索你强烈想探索求知的某种事物了,这种冥冥之中玄而又玄的感觉是什么?回想一下: (1) 当你把第一个某种编程语言的Hello World的例子,成功的运行在一个IDE中  (2) 当你第一次从老家出发到达了某个你从未涉足过的地方  (3) 当你成功的完成了

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.RELEASE 1.4.4 1.1.0.RELEASE 1.3.2 1.0.0.RELEASE https://github.com/helloworldtang/spring-data-elasticsearch 1.None of the configured nodes are availa

一键式Spring集成工具 Spring Boot

最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@Aspect public class ControllerAspect { //对 com.store59.creditmall.controller包下面的所有类的所有带request对象的方法 进行拦截 @Before("execution(* com.store59.creditmall.con

spring boot 实战 / mvn spring-boot:run 参数详解

概述   Spring boot项目通常情况下有如下几种启动方式: 通过主类启动. 通过spring-boot的maven插件spring-boot-maven-plugin方式启动. 通过可执行jar/war包方式启动. 通过Servlet容器启动,如Tomcat.Jetty等.   今天我们来聊一下spring boot的maven插件spring-boot-maven-plugin启动过程中的profiles问题.首先,我们前往网站SPRING INITIALIZR,参照下图创建一个名称为

从实践出发:微服务布道师告诉你Spring Cloud与Spring Boot他如何选择

背景 随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架构的演进过程 单一应用架构(第一代架构) 这是平台最开始的情况,当时流量小,为了节约成本,并将所有应用都打包放到一个应用里面,采用的架构为.net+sqlserver: 表示层 位于最外层(最上层),最接近用户.用于显示数据和接收用户输入的数 据,为用户提供一种交互式操作的界面,平台所使用的是基于.

Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip

spring-boot-assembly 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip 将spring boot项目中的配置文件提取到外部config目录中 将spring boot项目中的启动jar包移动到boot目录中 将spring boot项目中的第三方依赖jar包移动到外部lib目录中 bin目录中是启动,停止,重启服务命令 打包后的目录结构类似于tomcat/maven目录结构 GITHUB项