SpringBoot(十) 性能优化

默认情况下,我们会使用 @SpringBootApplication 注解来自动获取应用的配置信息,但这样也会给应用带来一些副作用.

1、会导致项目启动时间变长。当启动一个大的应用程序,或将做大量的集成测试启动应用程序时,影响会特别明显。

2、会加载一些不需要的多余的实例(beans)。

3、会增加 CPU 消耗。

因为 @SpringBootApplication会触发自动配置(auto-configuration )和 组件扫描 ( component scanning ),

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@SpringBootConfiguration

@EnableAutoConfiguration

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

public @interface SpringBootApplication {

移除@SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration来替代 将Servlet容器

默认情况下,Spring Boot 使用 Tomcat 来作为内嵌的 Servlet 容器,可以将 Web 服务器切换到 Undertow 来提高应用性能。Undertow的吞吐量大于tomcat,

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-web</artifactId>

  <exclusions>

    <exclusion>

      <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-tomcat</artifactId>

    </exclusion>

   </exclusions>

</dependency>

然后添加 Undertow: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>

SpringBoot JVM参数调优 参考:https://zhuanlan.zhihu.com/p/31803182 https://docs.oracle.com/middleware/11119/wls/PERFM/jvm_tuning.htm#i1146060

猜你喜欢

转载自www.cnblogs.com/xianshiwang/p/10116661.html