Tomcat uses thread pools to configure high concurrent connections

1: Configure executor properties

Open the / conf/server.xml file and configure a thread pool before Connector:

<Executor name="tomcatThreadPool"   
          namePrefix="tomcatThreadPool-"   
          maxThreads="1000"   
          maxIdleTime="300000"  
          minSpareThreads="200"/>  

Important parameter description:
Name: The name of the shared thread pool.This is the name that Connector will refer to in order to share a thread pool, which must be unique.Default value: None;
namePrefix: Each running thread can have a name string on the JVM.This property sets a prefix to the name string of each thread in the thread pool, and Tomcat appends the thread number to the end of this prefix.Default value: tomcat-exec-;
maxThreads: The maximum number of threads that the thread pool can hold.Default value: 200;
maxIdleTime: The amount of time (in milliseconds) that an idle thread is allowed to persist before Tomcat closes it.Only idle threads will be closed if the number of currently active threads is greater than the value of minSpareThread.Default value: 60000 (one minute).
minSpareThreads: The minimum number of inactive threads Tomcat should always open.Default value: 25.

2: Configure Connector

<Connector executor="tomcatThreadPool"  
           port="8088"
           protocol="HTTP/1.1"  
           connectionTimeout="20000"  
           redirectPort="8443"   
           minProcessors="5"  
           maxProcessors="75"  
           acceptCount="1000"/>  

Important parameter description:
executor: indicates the thread pool corresponding to the value of this parameter;
minProcessors: The number of threads created at server startup to process requests;
maxProcessors: The maximum number of threads that can be created to process requests;
acceptCount: Specifies the number of requests that can be placed in the processing queue when all available threads for processing requests are used. Requests that exceed this number will not be processed.

1. Tomcat memory optimization

tomcat memory optimization is mainly about tomcat startup parameters. We can set JAVA_OPTS parameters in tomcat startup script catalina.sh.

1. Description of JAVA_OPTS parameters

-server enables the server version of jdk;  
-Minimum memory for Xms Java virtual machine initialization;  
-Maximum memory available to the Xmx Java virtual machine;  
-XX:PermSize Memory Permanent Reserve Area  
-XX:MaxPermSize Maximum Permanent Reserved Memory Area   

Current company server memory can generally be increased to a maximum of 2G, so the following configuration can be taken:

Add before cygwin=false

JAVA_OPTS='-Xms1024m -Xmx2048m -XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m'

Once the configuration is complete, restart Tomcat to see if the configuration is valid:

First look at the Tomcat process number:

lsof -i:8088

result

[root@izwz9ajtb45u2jzfp4ljixz ~]# lsof -i:8088
COMMAND   PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
java    27698 root   46u  IPv4 116945134      0t0  TCP *:radan-http (LISTEN)

We can see that the Tomcat process number is 27698.

Check if the configuration is valid:

jmap –heap 27698

You can find Heap Configuration in the output information and see that parameters such as MaxHeapSize are in effect.

2. Tomcat Concurrency Optimization

1.Tomcat connection related parameters

In the <Connector... /> configuration in the Tomcat configuration file server.xml

1. Parameter description

minProcessors: Minimum number of idle connection threads to improve system processing performance, default value is 10
maxProcessors: Maximum number of connection threads, that is, the maximum number of requests processed concurrently, with a default value of 75
acceptCount: Maximum number of connections allowed, should be greater than or equal to maxProcessors, default value is 100
enableLookups: Whether to reverse the domain name, take the value: true or false.To improve processing power, set to false
ConneionTimeout: Network connection timeout in milliseconds.Setting to 0 means never timeout, which is dangerous.Usually set to 30,000 milliseconds.
The parameters associated with the largest number of connections are maxProcessors and acceptCount.If you want to increase the number of concurrent connections, you should increase both parameters.
The maximum number of connections allowed by web server s is also subject to the operating system's kernel parameter settings, typically around 2000 for Windows and 1000 for Linux.

2. Configuration examples in Tomcat
<Connector port="8088"
           protocol="HTTP/1.1"
           maxHttpHeaderSize="8192"
           minProcessors="100"
           maxProcessors="1000"
           acceptCount="1000"
           redirectPort="8443"
           disableUploadTimeout="true"/>

2. Adjust the concurrency processing power of connector connectors

1. Parameter description

maxThreads: Maximum number of threads for client requests
minSpareThreads: Number of socket threads created at Tomcat initialization
maxSpareThreads: Maximum number of idle socket threads for the Tomcat connector
enableLookups: If set to true, domain name resolution is supported, ip address can be resolved to host name
RedirectPort: Forward customer requests to the SL-based redirectPort in situations where a secure channel is required
acceptAccount: The maximum number of listening port queues, after which customer requests will be rejected (cannot be smaller than maxSpareThreads)
ConneionTimeout: Connection timeout
minProcessors: Minimum number of processing threads at server creation
maxProcessors: Maximum number of simultaneous processing threads on the server
URIEncoding: Uniform Encoding of URL s

2. Configuration examples in Tomcat
<Connector port="8088"
           protocol="HTTP/1.1"
           maxHttpHeaderSize="8192"
           maxThreads="1000"
           minSpareThreads="100"
           maxSpareThreads="1000"
           minProcessors="100"
           maxProcessors="1000"
           enableLookups="false"
           URIEncoding="utf-8"
           acceptCount="1000"
           redirectPort="8443"
           disableUploadTimeout="true"/> 

3.Tomcat cache optimization

1. Parameter description

Compression: turn compression on
compressionMinSize: Enable compressed output size, where defaults to 2KB
compressableMimeType: Compression type
ConneionTimeout: Defines the time-out for establishing a customer connection. If -1, it means no time limit for establishing a customer connection

2. Configuration examples in Tomcat
<Connector port="8088"
           protocol="HTTP/1.1"
           maxHttpHeaderSize="8192"
           maxThreads="1000"
           minSpareThreads="100"
           maxSpareThreads="1000"
           minProcessors="100"
           maxProcessors="1000"
           enableLookups="false"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
           connectionTimeout="20000"
           URIEncoding="utf-8"
           acceptCount="1000"
           redirectPort="8443"
           disableUploadTimeout="true"/>  

4. Reference Configuration

1. Old Configuration

Reference network has done the following configuration to the server, take out to share below:

<Connector port="8088"
           protocol="HTTP/1.1"
           maxHttpHeaderSize="8192"
           maxThreads="1000"
           minSpareThreads="25"
           maxSpareThreads="75"
           enableLookups="false"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
           connectionTimeout="20000"
           URIEncoding="utf-8"
           acceptCount="200"
           redirectPort="8443"
           disableUploadTimeout="true" />  

Later, performance bottlenecks were found when visits reached more than 3 million.

2. Changed configuration

<Connector port="8088"
           protocol="HTTP/1.1"
           maxHttpHeaderSize="8192"
           maxThreads="1000"
           minSpareThreads="100"
           maxSpareThreads="1000"
           minProcessors="100"
           maxProcessors="1000"
           enableLookups="false"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
           connectionTimeout="20000"
           URIEncoding="utf-8"
           acceptCount="1000"
           redirectPort="8443"
           disableUploadTimeout="true"/>  

Tags: Java Tomcat xml Javascript

Posted on Wed, 07 Aug 2019 19:28:16 -0700 by MikeUK