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


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

@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。
启动时需要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>true</optional>
        </dependency>

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);
    }
  }
}

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秒。