让 Spring Boot 启动更快一点

Spring Boot 启动时间优化全攻略 随着 Spring Boot 的广泛应用,开发者享受到了快速开发和自动化配置的便利。然而,随着项目复杂度的增加,Spring Boot 项目启动时间也变得越来越长,这在开发、调试和部署阶段可能会成为效率瓶颈。如何优化 Spring Boot 的启动时间,已经成为开发者和运维人员关注的重要问题。 本文将详细分析影响 Spring Boot 启动时间的主要因素,并提供一套从配置优化到代码调整的全方位优化方案,帮助开发者显著缩短项目的启动时间。 阅读详情

点击上方“芋道源码”,选择“设为星标”

做积极的人,而不是积极废人!

源码精品专栏

 

来源:http://rrd.me/ekc64

  • 1. FluxBaseline

  • 2. WebMVC

  • 3. spring-context-indexer

  • 4. 惰性初始化

  • 5. NoVerify

  • 6. TieredStopAtLevel

  • 7. 指定 SpringConfigLocation 参数

  • 8. 关闭 JMX

  • 9. 取消 Logback

  • 10. 取消 Jackson

  • 11. 取消 HibernateValidator

  • 12. AppCDS

  • 13. 使用 Thin Launcher 的 Flux

  • 14. Thin Launcher + CDS

  • 15. 所有操作都上

  • 更进一步


“Spring有多快?”

这是 2018 Spring One Platform 中的一场会议。看完会议视频,我自己动手试了一下。下面是我做出的测试结果。

还没有观看视频的朋友推荐看一下,非常有意思。

https://springoneplatform.io/2018/sessions/how-fast-is-spring-

本文使用的代码

https://github.com/bufferings/spring-boot-startup-mybench

我使用的是 OpenJDK 11。

java --version
openjdk 11.0.1 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

可以按照下面这样运行基准测试。运行起来可能需要一些时间,下面会执行所有的测试。

./mvnw clean package
(cd benchmarks/; java -jar target/benchmarks.jar)

1. FluxBaseline

我使用 SpringInitializr 创建项目,仅包含 Reactive Web。接下来,我会写一个 WebMVC 风格的极简controller。

@SpringBootApplication
@RestController
public class DemoApplication {

  @GetMapping("/")
  public String home() {
    return "Hello";
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

Spring Boot 版本是 2.1.0.RELEASE。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

启动结果 2.938 ± 0.287 s/op。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op

现在,以这个结果作为基线。让我们从这里开始。

2. WebMVC

我很好奇为什么要用 WebMVC 而不是 WebFlux?我尝试了一下。也许仅仅是为了比较 Tomcat 和 Netty?

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case02_Web                               ss   10  3.281 ± 0.342   s/op

WebFlux 快了一点,难道不是吗?

3. spring-context-indexer

接下来,我试了 spring-context-indexer,似乎创建了 component index。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-indexer</artifactId>
    <optional>true</optional>
</dependency>

嗯…慢了一点点?

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case03_WithContextIndexer                ss   10  3.063 ± 0.102   s/op

我检查了 spring.components,发现只包含了一个组件。了解…我应该尝试更大一点的项目,这样才能看到效果。

#
#Sun Nov 04 18:42:59 JST 2018
com.example.DemoApplication=org.springframework.stereotype.Component

4. 惰性初始化

尝试了惰性初始化。

@Configuration
public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
      beanFactory.getBeanDefinition(beanName).setLazyInit(true);
    }
  }
}

查看结果,启动变快了一点。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case04_WithLazyInit                      ss   10  2.844 ± 0.129   s/op

5. NoVerify

运行加 -noverify 选项:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case05_WithNoVerifyOption                ss   10  2.582 ± 0.060   s/op

启动变得快了一点。不知道为什么有这个结果,稍后需要仔细了解一下。

6. TieredStopAtLevel

运行加 -XX:TieredStopAtLevel=1 选项:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case06_WithTieredStopAtLevel1Option      ss   10  1.980 ± 0.037   s/op

嗯,快多了!减少了几乎2秒。还是不知道这个参数有什么含义,稍后需要仔细了解一下。

7. 指定 SpringConfigLocation 参数

运行加 -Dspring.config.location=classpath:/application.properties 选项:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case07_WithSpringConfigLocationOption    ss   10  3.026 ± 0.139   s/op

嗯,又变慢了。

8. 关闭 JMX

运行加 -Dspring.jmx.enabled=false 选项:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case08_WithJmxDisabledOption             ss   10  2.877 ± 0.097   s/op

变得快了一点。

9. 取消 Logback

从这里开始,我开始减少函数库。开始,取消 Logback:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-logging</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
</dependency>

结果如下:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case09_WithoutLogback                    ss   10  2.904 ± 0.096   s/op

嗯…似乎有一点点改进?

10. 取消 Jackson

接下来是 Jackson

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-json</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

结果如下:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case10_WithoutJackson                    ss   10  2.789 ± 0.093   s/op

结果变快了一点。

11. 取消 HibernateValidator

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>hibernate-validator</artifactId>
            <groupId>org.hibernate.validator</groupId>
        </exclusion>
    </exclusions>
</dependency>

结果如下:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case11_WithoutHibernateValidator         ss   10  2.857 ± 0.084   s/op

也有一点效果。

到这里为止,不再取消函数库了。

12. AppCDS

AppCDS (Application Class Data Sharing) 是 Oracle JDK 的一个企业版功能。OpenJDK 10 开始包含了这个功能。

看起来 AppCDS 转储信息存到了一个共享压缩文件,所以启动时间变短了。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case12_WithAppCds                        ss   10  2.957 ± 0.079   s/op

嗯…并没有变快…然后我阅读了CDS的相关文章,找到原因了。

SpringBoot FatJAR 不在 CDS 管理范围内。

13. 使用 Thin Launcher 的 Flux

嗯,对不起,“Exploded” 基准测试错了。我曾经试着使用 FatJAR,但是 CDS 不能这么干。所以,我转而使用 Thin Launcher,所以 “Exploded” 就变成了 “Thin Launche”。

使用 CDS 以前,我会测试使用 Thin Launcher 打包 JAR 文件的启动速度。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot.experimental</groupId>
                <artifactId>spring-boot-thin-layout</artifactId>
                <version>1.0.15.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

尽管我使用 Thin Launcher 打包 app,但并没有使用 Thin Launcher 启动类,而是使用 Main class 让启动尽可能快一些。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case13_Exploded                          ss   10  2.476 ± 0.091   s/op

嗯,快了一点,对吗?

14. Thin Launcher + CDS

现在,我要使用 AppCDS 。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case14_ExplodedWithAppCds                ss   10  1.535 ± 0.036   s/op

喔!变得更快了!

15. 所有操作都上

最终,我把所有操作都用上。

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case15_AllApplied                        ss   10  0.801 ± 0.037   s/op

小于1秒钟!(∩´∀`)∩耶

更进一步

在 Dave 的视频中,他提到了“函数式 Bean 定义”,尝试仅使用 Spring 不用 SpringBoot,app变得更快了。其中的道理还需要进一步了解。

结果:

Benchmark                                          Mode  Cnt  Score   Error  Units
MyBenchmark.case01_FluxBaseline                      ss   10  2.938 ± 0.287   s/op
MyBenchmark.case02_Web                               ss   10  3.281 ± 0.342   s/op
MyBenchmark.case03_WithContextIndexer                ss   10  3.063 ± 0.102   s/op
MyBenchmark.case04_WithLazyInit                      ss   10  2.844 ± 0.129   s/op
MyBenchmark.case05_WithNoVerifyOption                ss   10  2.582 ± 0.060   s/op
MyBenchmark.case06_WithTieredStopAtLevel1Option      ss   10  1.980 ± 0.037   s/op
MyBenchmark.case07_WithSpringConfigLocationOption    ss   10  3.026 ± 0.139   s/op
MyBenchmark.case08_WithJmxDisabledOption             ss   10  2.877 ± 0.097   s/op
MyBenchmark.case09_WithoutLogback                    ss   10  2.904 ± 0.096   s/op
MyBenchmark.case10_WithoutJackson                    ss   10  2.789 ± 0.093   s/op
MyBenchmark.case11_WithoutHibernateValidator         ss   10  2.857 ± 0.084   s/op
MyBenchmark.case12_WithAppCds                        ss   10  2.957 ± 0.079   s/op
MyBenchmark.case13_Exploded                          ss   10  2.476 ± 0.091   s/op
MyBenchmark.case14_ExplodedWithAppCds                ss   10  1.535 ± 0.036   s/op
MyBenchmark.case15_AllApplied                        ss   10  0.801 ± 0.037   s/op

真的很有意思。感谢!



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:

已在知识星球更新源码解析如下:


如果你喜欢这篇文章,喜欢,转发。

生活很美好,明天见(。・ω・。)ノ♡

Spring boot启动原理及相关组件 ConfigurationClassPostProcessor的优先级为最高,它会对项目中的@Configuration注解修饰的类(@Component、@ComponentScan、@Import、@ImportResource修饰的类也会被处理)进行解析,解析完成之后把这些bean注册到BeanFactory中。在github里,我把Spring Boot应用启动的拓展组件(自定义的应用初始器、监听器、事件、ApplicationRunner)都写了例子,可参照阅读。 阅读详情

相关推荐

[Spring Boot] 1. Spring Boot启动过程源码分析

关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到如今可以这么简便。1. 入口类package com.example.demo;import org.springframework.boot.SpringApplication; import org

不忘初心,好好沉淀 2万+

提升Spring Boot程序启动速度的七大优化技巧

Spring Boot提供了自动配置机制,根据应用程序的依赖项和配置,自动配置各种组件。通过持续的优化和调整,您可以构建出性能卓越的Spring Boot应用程序,提供更好的用户体验和高效的系统运行。另外,您还可以选择更轻量级的替代品或自定义的实现,以减少依赖项的数量和大小。:在Spring Boot 2.3及更高版本中,引入了快速启动模式,它可以明显减少应用程序的启动时间。与传统的数据库相比,嵌入式数据库的启动速度更快,并且不需要额外的配置和安装。这样可以延迟初始化非必需的bean,加快启动速度。

weixin_51702416的博客 1万+

Spring Boot 启动优化攻略

本文针对SpringBoot项目启动缓慢问题展开优化实践。通过自定义SpringApplicationRunListener分析启动各阶段耗时,发现主要瓶颈在Bean实例化环节。采用InstantiationAwareBeanPostProcessor统计各Bean加载时间,定位到分库分表数据源加载元数据和活动服务初始化两个耗时点。优化方案包括:1)减少测试环境分表数量配置;2)通过继承DefaultListableBeanFactory实现异步初始化机制,对ActivityServiceImpl等耗时服务

wjianwei666的专栏 1004

聊聊SpringBoot应用启动优化

随着业务发展,线上业务jar 包越来越大,动不动就几百兆,启动时间也越来越慢,严重影响效率。目前大部分java项目都是使用SpringBoot,这篇文章就来简单聊聊关于SpringBoot应用启动优化。对于一个“历史悠久”的项目,业务代码自然是指数增长,而且在日常业务开发中,我们我都是在做加法,很少会去做删代码之类的操作。另外一般项目都会有很多中间件的初始化,比如:数据连接、redis连接、mq生产者和消费者注册、dubbo生产者和消费者注册、定时任务等;

Huangjiazhen711的博客 3272

Spring Boot下Spring MVC框架的启动原理——SpringBoot应用的启动过程——万字长文

文章目录1:创建 SpringApplication1.1 创建 SpringApplication过程的源码调试1.2 创建 SpringApplication过程的源码解读1.2.1 推断是否是Web环境1.2.2 通过spring工厂设置应用组件1.2.3 推断main方法所在的启动类2:运行 SpringApplication2.1 应用环境的创建与准备2.2 应用上下文的创建(创建IOC容器)2.3 应用上下文的准备(准备IOC容器的基本信息)2.4 应用上下文的刷新(刷新IOC容器) Spr

qq_44891295的博客 1797

Spring Boot2.2.2搭建Spring Boot Admin启动报错

Spring Boot2.2.2 +spring-boot-admin-server:2.1.6不能一起使用,暂没有找到原因,启动报错: at de.codecentric.boot.admin.server.web.reactive.AdminControllerHandlerMapping.registerHandlerMethod(AdminControllerHandlerMappi...

minion_banana的博客 4236

SpringBoot启动扩展应用:干预优化+加快启动时间(干货典藏版)

????这是一个或许对你有用的社群????一对一交流/面试小册/简历优化/求职解惑,欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料:《项目实战(视频)》:从书中学,往事中“练”《互联网高频面试题》:面朝简历学习,春暖花开《架构 x 系统设计》:摧枯拉朽,掌控面试高频场景题《精进 Java 学习指南》:系统学习,互联网主流技术栈《必读 Java 源码专栏》:知其然,知其所以然????这是一个或许...

weixin_44421461的博客 989

Spring Boot的启动流程

- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 创建springbootApplication对象 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 1. 创建springbootApplication对象springboot容器初始化操作 2. 获取当前应用的启动类型。2.1 :通过判断当前classpath是否加载servlet类,返回servlet web启动方式。

流楚丶格念的博客 2万+

SpringBoot应用启动优化

经历这么一个优化过程,我们发现,能定位到关键问题是非常关键的。而透过错综复杂的现象,能看到问题的本质是非常考验能力与经验的。

jvjkfd的博客 1650

Spring Boot(四):Spring Boot启动原理分析

将配置类@SpringBootApplication标注的类所在包及下面所有子包里的所有组件扫描到spring容器。点击@SpringBootApplication,再点击@EnableAutoConfiguration进入类文件。这些自动配置类给我们当前项目的场景提供了一些组件和配置,有了自动配置就免除类手动编写配置文件,注入等等功能。这里管理着springboot中所有依赖的版本,是一个版本控制中心,导入依赖如果不写版本就用这里的版本。该jar包提供一系列的配置类,替换掉了我们的XML配置信息。

Lansonli(蓝深李)的博客 1万+

高斯泼溅(Gaussian Splatting):3D建模的颠覆性革命

高斯泼溅(Gaussian Splatting):3D建模的颠覆性革命

yingtu666的博客 5159

tomcat启动卡住slf4j_让 Spring Boot 启动更快一点

1. FluxBaseline2. WebMVC3. spring-context-indexer4. 惰性初始化5. NoVerify6. TieredStopAtLevel7. 指定 SpringConfigLocation 参数8. 关闭 JMX9. 取消 Logback10. 取消 Jackson11. 取消 HibernateValidator12. AppCDS13. 使用 Thin ...

weixin_39644021的博客 414

【Unity报错】Unity导入海康SDK 报错 DllNotFoundException: PlayCtrl assembly

2.尝试按网上其他的办法(包括使用depengs查看dll依赖等)1.先检查Plugins文件夹中是否存在PlayCtrl文件。3.如果网上查阅的办法都不能解决,请继续往下看。更换低版本的SDK库文件。

weixin_42968584的博客 1127

让SpringBoot启动更快一点

“Spring有多快?” 这是2018SpringOnePlatform中的一场会议。看完会议视频,我自己动手试了一下。下面是我做出的测试结果。 还没有观看视频的朋友推荐看一下,非常有意思。 https://springoneplatform.io/2018/sessions/how-fast-is-spring- 本文使用的代码 https://github.com/b...

KHOST的博客 236

SAP BSP网页开发入门:从SE80创建到MVC实战(附成品展示)

本文详细介绍了SAP BSP网页开发的入门与实战,从SE80环境搭建到MVC架构实现的全流程。通过具体案例和代码示例,帮助开发者掌握业务服务器页面(BSP)技术,提升SAP Web化转型中的开发效率,适用于需要扩展SAP功能的企业场景。

x1y2z的博客 1万+

Simulink标定工作流优化:当A2L文件遇到ELF,如何用Matlab脚本打通‘最后一公里’?

本文探讨了Simulink标定工作流中A2L与ELF文件处理的自动化解决方案。通过Matlab脚本实现A2L文件智能合并与ELF文件自动匹配,解决了传统手动操作效率低下和易出错的问题,显著提升汽车ECU开发效率。重点介绍了脚本实现细节及持续集成系统集成方案。

weixin_30564785的博客 338

spring-boot的三种启动方式

有段时间没有写博客了,也在努力的从传统单机开发向分布式系统过度,所以再次做一些笔记,以方便日后查看。直接进入正题吧,今天记录spring-boot项目的三种启动方式。spring-boot的启动方式主要有三种: 1. 运行带有main方法类 2. 通过命令行 java -jar 的方式 3. 通过spring-boot-plugin的方式一、执行带有main方法类 这种方式...

奔跑的蜗牛的博客 6万+

金融数据自动化:从同花顺API到Excel的智能转换之旅

本文详细介绍了如何利用同花顺API实现金融数据自动化处理,并将其智能转换为Excel格式的全流程。通过Python编程和Excel高级技巧,读者可以掌握从数据获取、清洗到输出的完整解决方案,大幅提升金融数据分析的效率和准确性。文章特别强调了同花顺API的使用技巧和Excel自动化输出的最佳实践。

sql99的博客 462
上一篇: 如何快速判断某 URL 是否在 20 亿的网址 URL 集合中?
下一篇: Java 程序员常用资源工具集合(建议收藏)
公众号-芋道源码
博客等级 码龄10年 1639粉丝 377原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值