Table of Contents
1. Why project performance tuning is necessary
2. Performance tuning of service containers
2.1 Tuning of cut-through Tomcat in SpringBoot: optimizing the maximum number of threads
Tuning instructions:
Tuning settings
2.2 Network IO model tuning
2.3 Container Optimization Tomcat Upgrade Undertow
Configuration operation process:
Home Java javaTutorial What is the method for java server container tuning?

What is the method for java server container tuning?

May 11, 2023 pm 11:37 PM
java server

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 # 最大空闲连接数,防止突发流量
Copy after login

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
Copy after login
  • 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;
        }
    }
Copy after login
  • 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
Copy after login

Import starters of other containers

<!--导入undertow容器依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Copy after login
  • 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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles