9 - [SpringBoot performance optimization]

A sweep packet optimization

Sweep start packet belongs to optimize, optimization not run

1.1 components automatically scan problems caused by

Use @SpringBootApplicationannotations, the package will traverse the following sub-categories, can affect performance.

By default, we will use @SpringBootApplicationannotations to automatically obtain configuration information for the application, but it will also bring some side effects to the application.

After using this annotation, triggers 自动配置(auto-configuration) and 组件扫描(component scanning), which with the use of @Configuration, @EnableAutoConfigurationand @ComponentScanthe role of three notes is the same. Doing so bring convenience to developers, there will be a threefold effect:

  1. The project will lead to longer startup time. When starting a large application, or to do a lot of integration tests start the application, the impact will be particularly pronounced.
  2. Loads some unwanted extra instance (beans).
  3. Increase CPU consumption.

For the above three cases, we can be removed @SpringBootApplicationand @ComponentScantwo component annotations to disable automatic scanning, then we need beanto be explicitly configured on:

// 移除 @SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration 来替代
// @SpringBootApplication
@ComponentScan(basePackages = "com.snow")
@EnableAutoConfiguration
public class App {

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

}

2 SpringBoot JVMParameter Tuning

JVM Tuning parameters, belonging to the overall optimization

The memory size of the server to set the parameters of the heap.

  • -Xms: Set the initial size of the Java stack
  • -Xmx: Set the maximum heap size java
    instance parameters-XX:+PrintGCDetails -Xmx32M -Xms1M

Local project Tuning:
Here Insert Picture Description

External operation Tuning:

java -server -Xms32m -Xmx32m  -jar springboot_v2.jar

3 Servletcontainer becomesUndertow

Step 3.1

By default, Spring Boot using the embedded Tomcat as a Servlet container.

Web server can be switched to Undertow to improve application performance.

Undertow is a flexible, high-performance Web server Java development services including blocking NIO-based and non-blocking mechanism. Undertow is Red Hat's open source products, is Wildfly default Web server.

First, remove the Tomcat configuration dependent on information from inside:

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

Start, we found using Undertow:
Here Insert Picture Description

3.2 Test

Create thread groups
Here Insert Picture Description
each request 10000
Here Insert Picture Description

Creating HTTP request:
Here Insert Picture Description

test:
Here Insert Picture Description

Create an aggregate reports:
Here Insert Picture Description

Test:
Click Run, view the test report
Here Insert Picture Description
Here Insert Picture Description

name of server First run Second run Third run average value
Tomcat 4773 5194 5334.7 5100
Undertow 6666 6373 6451 6496
Published 663 original articles · won praise 213 · views 130 000 +

Guess you like

Origin blog.csdn.net/weixin_42112635/article/details/104878172