Springboot optimization built-in server Tomcat optimization (underTow)

Preface

View through org.springframework.boot.autoconfigure.web.ServerProperties, which includes the settings of three servers: tomcat, jetty, and undertow , tomcat is enabled by default.

Tomcat self-optimization

# Tomcat
server:
  tomcat:
    uri-encoding: UTF-8
    #最小线程数
    min-spare-threads: 500
    #最大线程数
    max-threads: 2500
    #最大链接数
    max-connections: 6500
    #最大等待队列长度
    accept-count: 1000
    #请求头最大长度kb
    max-http-header-size: 1048576
    #请请求体最大长度kb
    #max-http-post-size: 2097152
  #服务http端口
  port: 8080
  #链接建立超时时间
  connection-timeout: 12000
  servlet:
    #访问根路径
    context-path: /son

There are two more important ones:Initial number of threadsandMaximum number of threads .

Initial number of threads: To ensure that when a large number of users visit, it can accept requests stably.

Maximum number of threads: is used to ensure system stability.

Other parameter optimization:

min-spare-threads: The minimum number of spare threads, the number of initialized threads when tomcat starts.

max-threads: The maximum number of threads that Tomcat can create. Each thread handles one request. After this number of requests is exceeded, client requests can only be queued and cannot be processed until a thread is released. (It is recommended that this configuration number can be between 200 and 250 times the number of CUP cores on the server)

accept-count: When the number of HTTP requests calling the Web service reaches the maximum number of threads of tomcat, and a new HTTP request arrives, tomcat will place the request in the waiting queue. This acceptCount refers to the maximum wait that can be accepted. number, default 100. If the waiting queue is also full, new requests coming at this time will be rejected by tomcat (connection refused).

max-connections: This parameter refers to the maximum number of connections that tomcat can accept at the same time. Generally this value is greater than (max-threads)+(accept-count).

connection-timeout: The maximum waiting time. If no data comes in, wait for a period of time and then disconnect and release the thread.

 

UnderTow optimization 

Why become UnderTow?

We stress tested Tomcat and Undertow under the same machine configuration, and the test results obtained are as follows:

Tomcat:

UnderTow:

Memory usage comparison:

in conclusion:

Undertow's performance is better than Tomcat in high-concurrency business scenarios

Method to realize:

POM file removes tomcat and replaces it with underTow

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId><!-- 移除掉默认支持的 Tomcat -->
            <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>

 Configuration related parameters:

# Whether to open the undertow log, the default is false
server.undertow.accesslog.enabled=false
# Set the directory where the access log is located server.undertow.max-http-post-size=0 # Set the maximum length of HTTP POST content, no limit by default server. undertow.worker-threads= # Specify the number of worker threads, the default is 8 times the number of I/O threads server.undertow.io-threads= # Specify the number of I/0 threads of the worker thread, the default is 2 or the number of CPUs
server.undertow.accesslog.dir=logs





Finally, just start Springboot directly in the main method.​ 

Guess you like

Origin blog.csdn.net/qq_31536117/article/details/134499778