Springboot学习教程(十)springboot性能优化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/xuruanshun/article/details/102615061

目录

1.扫包优化

2.JVM参数调优

3.将Tomcat服务器换成Undertow服务器


 

springboot的性能优化,介绍扫包优化,JVM参数调优,以及使用Undertow服务器三种方式。

1.扫包优化

扫包优化是启动项目时优化,而不是运行时优化

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

我们知道,@SpringBootApplication开启了Spring的组件扫描和springboot的自动配置功能,相当于将以下三个注解组合在了一起

  • @Configuration:表名该类使用基于Java的配置,将此类作为配置类
  • @ComponentScan:启用注解扫描
  • @EnableAutoConfiguration:开启springboot的自动配置功能

而@ComponentScan 自动扫包,扫包范围是同级包下的所有类,但有些类是不需要加载的,当我们项目足够大,类足够多时,使用@ComponentScan 同级扫包就比较消耗性能了,所以我们可以精确扫包,只扫一些需要加载的类,这样性能就会高一些。

package com.example.springboot1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.example.springboot1.controller")
public class Springboot1Application {

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

实际上就是不使用@SpringBootApplication注解,而使用 @Configuration + @EnableAutoConfiguration + @ComponentScan(basePackages = "com.example.springboot1.controller")注解的方式

2.JVM参数调优

jvm参数调优,可以影响到项目整体运行,提高吞吐量

调优策略:可以设置初始化堆内存与最大堆内存 相同,减少垃圾回收次数
 

我们可以根据服务器的内存大小,来设置堆参数。

-Xms :设置Java堆栈的初始化大小

-Xmx :设置最大的java堆大小

实例参数: 

-XX:+PrintGCDetails -Xmx1024M -Xms1024M :表示打印垃圾回收日志,Java堆栈初始化大小为1024M,最大堆内存大小为1024M,一般设置相同比较好,而且越大越好,越大垃圾回收次数越少,默认最大堆内存值是4G。

 

JVM参数调优,分为内部调优和外部调优

1)内部调优是在开发工具中配置调优:本地项目调优配置,启动项目

2)外部调优:外部调优是使用命令行,对springboot项目打成的jar包 执行命令,启动项目

java -server -Xms32m -Xmx32m  -jar springboot.jar

 

我们可以通过使用可视化工具 jconsole 或者 jvisualvm,可以查看堆栈内存使用情况,查看我们是否设置成功了。

3.将Tomcat服务器换成Undertow服务器

Undertow也是一个web服务器,Tomcat吞吐量 5000左右,Undertow吞吐量8000左右,就性能而言,Undertow性能比Tomcat和jetty好,以后说不定会取代Tomca,成为微服务的主流服务器。但目前主流的web服务器还是Tomcat服务器。

Undertow 是一个采用 Java 开发的灵活的高性能 Web 服务器,提供包括阻塞和基于 NIO 的非堵塞机制。Undertow 是红帽公司的开源产品,是 Wildfly 默认的 Web 服务器。

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

在项目中使用Undertow取代Tomcate, 从Web依赖中移除 Tomcat 组件,添加Undertow依赖:

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

测试web容器性能方法:

       开启一个线程,发送一万次请求测试Tomcat吞吐效果。

       开启一个线程,发送一万次请求测试Undertow吞吐效果。

使用 jmeter工具,进行测试,测试三次,取平均值

测试报告:

我们可以看出Tomcat吞吐量 5000左右,Undertow吞吐量8000左右,Undertow性能比Tomcat好。

 

猜你喜欢

转载自blog.csdn.net/xuruanshun/article/details/102615061
今日推荐