让我们让SpringBoot应用程序更快启动

栏目: Java · 发布时间: 7年前

内容简介:采取下列措施可能会提高SpringBoot启动时间:1. FluxBaseline使用SpringInitializr创建了一个只有Reactive Web的项目。然后,我写了一个WebMVC风格的小控制器。

采取下列措施可能会提高SpringBoot启动时间:

1. FluxBaseline

使用SpringInitializr创建了一个只有Reactive Web的项目。然后,我写了一个WebMVC风格的小控制器。

@SpringBootApplication
@RestController
<b>public</b> <b>class</b> DemoApplication {

  @GetMapping(<font>"/"</font><font>)
  <b>public</b> String home() {
    <b>return</b> </font><font>"Hello"</font><font>;
  }

  <b>public</b> <b>static</b> <b>void</b> main(String[] args) {
    SpringApplication.run(DemoApplication.<b>class</b>, args);
  }
}
</font>

Spring Boot的版本是2.1.0.RELEASE。

启动时需要2.938±0.287 s / op。

现在我有一个基线来检查启动时间。让我们从这里开始。

2. 使用WebFlux,

提高了速度:MyBenchmark.case02_Web :3.281 ± 0.342

3. spring-context-indexer

它似乎创建了组件索引。对大项目有很多组件时有效果

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

4. 懒初始化

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

5. 无验证

加入-noverify:

6. TieredStopAtLevel

加入-XX:TieredStopAtLevel=1

效果很好

7.显式指定SpringConfigLocation

加入-Dspring.config.location=classpath:/application.properties

效果不理想

8.关闭JMX

-Dspring.jmx.enabled=false

9.排除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>

10. 排除 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>

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>

12. AppCDS

AppCDS(应用程序类数据共享)作为商业功能包含在Oracle JDK中。但它可以从OpenJDK 10获得。

似乎AppCDS将信息转储到共享存档中,因此启动时间变短。使用SpringBoot FatJAR,这些库不在CDS的范围内。

13. Flux的thin廋启动器

<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打包应用程序,但我没有使用Thin Launcher的启动类,但指定了Main类以尽可能快地启动启动时间。

14. Thin Launcher + CDS

最后全部应用上述措施,启动不到1秒。


以上所述就是小编给大家介绍的《让我们让SpringBoot应用程序更快启动》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python the Hard Way

Learn Python the Hard Way

Zed Shaw / Example Product Manufacturer / 2011

This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具