Home  >  Article  >  Java  >  What is the method for java server container tuning?

What is the method for java server container tuning?

WBOY
WBOYforward
2023-05-11 23:37:041066browse

1. Why project performance tuning is necessary

Before the project is released, the project needs to be stress tested to detect project performance problems. For example: the project response time is slow, the project every Problems such as the small number of requests that can be solved at one time, project bottlenecks, and slow project data query times need to be tuned after detection. This means that if the response time of your project interface exceeds ten seconds, you will not make any adjustments. A series of measures, then there is a problem with this project. The purpose of performance tuning is to make the project more optimized, RT (running response time) is smaller, and TPS (throughput - "requests received from the database per second) is larger etc.

Generally in enterprises, JMeter or K8s will be used. Some enterprises will build their own stress testing platform. After the project is written, the project will be stress tested. When the project is decided, the response time of the project will be measured. Requirements, make a rough judgment on the project's request data, and developers must write interfaces based on these requirements. If the interface response time exceeds the established data and the project cannot support such a large request, the project and project interface need to be database, Tuning of containers, cache, etc.

2. Performance tuning of service containers

2.1 Tuning of cut-through Tomcat in SpringBoot: optimizing the maximum number of threads

Tuning instructions:

maxThreadsMaximum number of threads: Measures the number of tasks that the web server can process simultaneously

accept-count Maximum number of waits: QueueThe maximum number of waits that can be accepted. Exceeded to reject the request.

Max Connections: The maximum number of connections at the same time.

When the number of links is the largest, it will continue to request and enter the wait. If it exceeds the maximum wait, it will be rejected.

The maximum number of threads in SpringBoot is 200. In many cases, the maximum number of threads 200 is not enough. Generally speaking, the server configuration of 1cpu2G is set to 200, and the server configuration of 4cpu8G is set to 800. It can Greatlyimprove TPS and reduce RT.

Tuning settings

Modify the configuration file application.yml

# Tomcat的 maxConnections、maxThreads、acceptCount三大配置,
#分别表示最大连接数,最大线程数、最大的等待数,可以通过application.yml配置文件来改变这个三个值,一个标
#准的示例如下:
server.tomcat.uri-encoding: UTF-8
# 思考问题:一台服务器配置多少线程合适?
server.tomcat.accept-count: 1000 # 等待队列最多允许1000个请求在队列中等待
server.tomcat.max-connections: 20000 # 最大允许20000个链接被建立
## 最大工作线程数,默认200, 4核8g内存,线程数经验值800
server.tomcat.threads.max: 800 # 并发处理创建的最大的线程数量
server.tomcat.threads.min-spare: 100 # 最大空闲连接数,防止突发流量

Confirm that the modified configuration takes effect.

You can use the configuration in SpringBoot. Add

# 暴露所有的监控点
management.endpoints.web.exposure.include: '*'
# 定义Actuator访问路径
management.endpoints.web.base-path: /actuator
# 开启endpoint 关闭服务功能
management.endpoint.shutdown.enabled: true
  • to the configuration file just now to check that the configuration takes effect: ip:port/actuator

  • Search tomcat

2.2 Network IO model tuning

Network IO is system file reading and writing IO. The system uses NIO (high performance, Synchronous, non-blocking), in fact, NIO2 (ultra-high performance, asynchronous, non-blocking) has been embedded by default, but you need to call the NIO2 API, but this part is based on the system, AIO (NIO2) depends on the operating system, such as Linux supports AIO

In fact, AIO is an optimization of NIO, which enhances support for file processing and file system features. After the system uses AIO, the response time will be reduced and stable.

Tuning settings

Put it directly into the java class in the project code and generate a java configuration class

@Configuration
    public class TomcatConfig {
        //自定义SpringBoot嵌入式Tomcat
        @Bean
        public TomcatServletWebServerFactory servletContainer() {
            TomcatServletWebServerFactory tomcat = new
                TomcatServletWebServerFactory() {};
            tomcat.addAdditionalTomcatConnectors(http11Nio2Connector());
            return tomcat;
        }
        //配置连接器nio2
        public Connector http11Nio2Connector() {
            Connector connector=new
                Connector("org.apache.coyote.http11.Http11Nio2Protocol");
            Http11Nio2Protocol nio2Protocol = (Http11Nio2Protocol)
                                               connector.getProtocolHandler();
            //等待队列最多允许1000个线程在队列中等待
            nio2Protocol.setAcceptCount(1000);
            // 设置最大线程数
            nio2Protocol.setMaxThreads(1000);
            // 设置最大连接数
            nio2Protocol.setMaxConnections(20000);
            //定制化keepalivetimeout,设置30秒内没有请求则服务端自动断开keepalive链接
            nio2Protocol.setKeepAliveTimeout(30000);
            //当客户端发送超过10000个请求则自动断开keepalive链接
            nio2Protocol.setMaxKeepAliveRequests(10000);
            // 请求方式
            connector.setScheme("http");
            connector.setPort(9003); //自定义的端口,与源端口9001可以共用,知识改了连接器而已
            connector.setRedirectPort(8443);
            return connector;
        }
    }
  • Check that the configuration takes effect: ip:9003/ Calling the interface

Note: Tomcat also has a mode called apr, which automatically turns on aio. The above method uses another method, but generally this is not used for tuning. This part is just Make a preliminary understanding, because not all systems support AIO

2.3 Container Optimization Tomcat Upgrade Undertow

As we all know, SpringBoot has embedded Tomcat, but in fact SpringBoot has three types of servers embedded: Tomcat (mature, stable, high-performance server), apache development, Jetty (lightweight, fast and flexible), Undertwo (high performance, high flexibility).

In Spring Boot, Tomcat is used as the built-in web server by default. You can also configure it accordingly in the configuration file and choose to use Jetty or Undertow.

Configuration operation process:
  • Exclude tomcat in spring-boot-starter-web

<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

Import starters of other containers

<!--导入undertow容器依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
  • Configuration

# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接
server.undertow.threads.io: 800
# 阻塞任务线程池, 当执行类似servlet请求阻塞IO操作, undertow会从这个线程池中取得线程
# 默认值是IO线程数*8
server.undertow.threads.worker: 8000
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
# 每块buffer的空间大小越小,空间就被利用的越充分,不要设置太大,以免影响其他应用,合适即可
server.undertow.buffer-size: 1024
# 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region
# 是否分配的直接内存(NIO直接分配的堆外内存)
server.undertow.direct-buffers: true

The above is the detailed content of What is the method for java server container tuning?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete