SoFunction
Updated on 2025-03-08

Introduction to Tomcat concurrency optimization method

There are three common operating modes for Tomcat, namely bio, nio, apr. It is recommended to use apr in the production environment. For details, please read the previous blog post "Tomcat - Running Mode

Install APR

[root@liuyazhuang ~]# yum -y install apr apr-devel openssl-devel 
[root@liuyazhuang ~]# tar zxvf  
[root@liuyazhuang ~]# cd tomcat-native-1.1.24-src/jni/native 
[root@liuyazhuang native]# ./configure –with-apr=/usr/bin/apr-1-config –with-ssl=/usr/include/openssl/ 
[root@liuyazhuang native]# make && make install 

After the installation is completed, the following prompt message will appear

Libraries have been installed in: 
/usr/local/apr/lib 

After the installation is successful, you also need to set environment variables for tomcat, by adding 1 line to the file:

Added below this section:

============ 
# OS specific support. $var _must_ be set to either true or false. 
cygwin=false 
darwin=false 
============== 
CATALINA_OPTS=”-=/usr/local/apr/lib” 

Modify the corresponding conf/ on the 8080 end

protocol=”.http11.Http11AprProtocol” 

After starting tomcat, check the log

more TOMCAT_HOME/logs/ 
Apr 07, 2017 11:49:12 AM  init 
INFO: Loaded APR based Apache Tomcat Native library 1.1.31 using APR version 1.3.9. 
Apr 07, 2017 11:49:12 AM  init 
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 
Apr 07, 2017 11:49:12 AM  initializeSSL 
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1e 11 Feb 2013) 
Apr 07, 2017 11:49:13 AM  init 
INFO: Initializing ProtocolHandler [“http-apr-8080”] 
Apr 07, 2017 11:49:13 AM  init 
INFO: Initializing ProtocolHandler [“ajp-apr-8009”] 
Apr 07, 2017 11:49:13 AM  load 
INFO: Initialization processed in 1125 ms 

Tomcat optimization

Tuning

Add the following statement to TOMCAT_HOME/bin/, the specific value depends on the situation.
Add it to the following CATALINA_OPTS above:

JAVA_OPTS=-Xms512m -Xmx1024m -XX:PermSize=512M -XX:MaxNewSize=1024m -XX:MaxPermSize=1024m 

Detailed explanation of parameters

-Xms  JVM initialize heap memory size
-Xmx  Maximum memory of JVM heap
-Xss   Thread stack size
-XX:PermSize JVM non-heap area initial memory allocation size
-XX:MaxPermSize JVM non-heap area maximum memory

Suggestions and precautions:

The -Xms and -Xmx options are set to the same heap memory allocation to avoid adjusting the heap size after each GC. The heap memory is recommended to account for 60% to 80% of the memory; non-heap memory is non-recyclable memory, and the size depends on the project; the thread stack size is recommended to be 256k.

The 32G memory configuration is as follows:

JAVA_OPTS=-Xms20480m -Xmx20480m -Xss1024K -XX:PermSize=512m -XX:MaxPermSize=2048m 

2. Turn off DNS reverse query

Add the following parameters to <Connector port=”8080″

enableLookups=”false” 

3. Use APR

For specific installation operations, see the beginning of the article

4. Optimize tomcat parameters

&lt;Connector port=”8080″ 
protocol=”.http11.Http11AprProtocol” 
connectionTimeout=”20000″ //Link timeoutredirectPort=”8443″ 
maxThreads=”500″//Set the maximum number of threads that process customer requests, which determines the number of threads that the server can respond to customer requests at the same time, and the default is 200minSpareThreads=”20″//The number of initialized threads, the minimum number of idle threads, the default is 10acceptCount=”1000″ //When all the threads that can be used to process the request are used, they can be placed in the processing queue for the number of requests. Requests with the number of requests exceeding this number will not be processed. The default is 100enableLookups=”false” 
URIEncoding=”UTF-8″ /&gt; 

Summarize

The above is all the content of this article about Tomcat concurrency optimization method. Interested friends can continue to refer to:Detailed explanation of the method of optimizing Tomcat configuration (memory, concurrency, cache, etc.)A brief discussion on the three operating modes of TomcatIntroduction to the method of opening JMX service in TomcatIf there are any shortcomings, please leave a message and point it out, I hope it will be helpful to everyone.