tomcat performance optimization (main)

The default parameters of tomcat are formulated for the development environment, but not suitable for the production environment, especially the configuration of memory and threads. The default is very low, which is easy to become a performance bottleneck.

 

tomcat memory optimization

Linux modify TOMCAT_HOME / bin / catalina.sh, add in front

JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"
  
  

windows modify TOMCAT_HOME / bin / catalina.bat, add in front

set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m
  
  

The maximum heap memory is 1024m, which is still low for the current hardware. During implementation, it is still optimized according to the specific hardware configuration of the machine.

 

tomcat thread optimization


  
  
  1. <Connector port="80" protocol="HTTP/1.1" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700"
  2. connectionTimeout= "20000" redirectPort= "8443" />

maxThreads = "600" /// Maximum number of threads
minSpareThreads = "100" /// Number of threads created during initialization
maxSpareThreads = "500" /// Once the number of threads created exceeds this value, Tomcat will close sockets that are no longer needed Thread.
acceptCount = "700" // Specify the number of requests that can be put in the processing queue when all available threads for processing requests are used, and requests exceeding this number will not be processed

 

Here is the optimization of the http connector. If you use apache and tomcat for cluster load balancing, and use the ajp protocol for apache and tomcat protocol forwarding, you need to optimize the ajp connector.


  
  
  1. <Connector port="8009" protocol="AJP/1.3" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700"
  2. connectionTimeout= "20000" redirectPort= "8443" />

 

Since tomcat has multiple connectors, the configuration of tomcat threads also supports multiple connectors sharing a thread pool.

First of all. Open /conf/server.xml and add

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />
  
  

The maximum thread is 500 (general server is enough), the minimum number of idle threads is 20, and the maximum idle time of threads is 60 seconds.

 

Then, modify the <Connector ...> node and add the executor attribute. The executor is set to the name of the thread pool:

<Connector executor="tomcatThreadPool" port="80" protocol="HTTP/1.1"  connectionTimeout="60000" keepAliveTimeout="15000" maxKeepAliveRequests="1"  redirectPort="443" />
  
  

Multiple connectors can share one thread pool, so ajp connector can also be set to use tomcatThreadPool thread pool.

 

Disable DNS query


When the web application wants to record the client's information, it will also record the client's IP address or look up the machine name through the domain name server and convert it to an IP address.

The DNS query needs to occupy the network, and includes the process of obtaining the corresponding IP from many very far servers or non-functional servers, which will consume a certain amount of time.

Modify the Connector element in the server.xml file, modify the attribute enableLookups parameter value: enableLookups = "false"

If true, you can get the actual host name of the remote client by calling request.getRemoteHost () for DNS query, if it is false, do not perform DNS query, but return its ip address

 

 

Set session expiration time

Specify by parameters in conf \ web.xml:


  
  
  1. <session-config>
  2. <session-timeout>180 </session-timeout>
  3. </session-config>
  4. The unit is minutes.

 

Apr plugin improves Tomcat performance

  Tomcat can use APR to provide superior scalability and performance, and better integrate local server technology.

  APR (Apache Portable Runtime) is a highly portable library, which is the core of Apache HTTP Server 2.x. APR has many uses, including access to advanced IO functions (such as sendfile, epoll, and OpenSSL), OS-level functions (random number generation, system status, etc.), local process management (shared memory, NT pipes, and UNIX sockets). These functions can make Tomcat as a normal front-end WEB server, can better integrate with other local web technologies, and generally make Java more efficient as a high-performance web server platform rather than simply as a back-end container.

  In the production environment, especially when directly using Tomcat as a web server, you should use Tomcat Native to improve its performance  

  The best way to measure the benefits of APR for tomcat is to use a slow network (simulating the Internet), open the number of Tomcat threads above 300, and then simulate a large number of concurrent requests.
  If you do n’t have APR, basically 300 threads will be used up quickly, and future requests will have to wait. But with APR, the number of concurrent threads dropped significantly, from the original 300 may immediately drop to only a few dozen, new requests will come in without blocking.
  In the LAN environment, even if it is 400 concurrent, the processing / transmission is completed in an instant, but in the real Internet environment, the page processing time is only 0.1%, and most of the time is used for page transmission. If APR is not used, a thread can only handle one user at a time, which is bound to cause blocking. So using apr in a production environment is very necessary.

Copy code


  
  
  1. (1) Install APR tomcat-native
  2. apr-1.3.8.tar.gz is installed in / usr / local / apr
  3. #tar zxvf apr-1.3.8.tar.gz
  4. #cd apr-1.3.8
  5. #./configure;make;make install
  6. apr-util-1.3.9.tar.gz is installed in / usr / local / apr / lib
  7. #tar zxvf apr-util-1.3.9.tar.gz
  8. #cd apr-useful-1.3.9
  9. #./configure --with-apr=/usr/local/apr ----with-java-home=JDK;make;make install
  10. #cd apache-tomcat-6.0.20/bin
  11. #tar zxvf tomcat-native.tar.gz
  12. #cd tomcat-native/jni/native
  13. #./configure --with-apr=/usr/local/apr;make;make install
  14. (2) Set up Tomcat to integrate APR
  15. Modify tomcat's startup shell (startup.sh), add startup parameters to the file:
  16. CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib" 。
  17. (3) Judge the successful installation:
  18. If you see the following startup log, it means success.
  19. 2007-4-26 15:34:32 org.apache.coyote.http11.Http11AprProtocol init

Copy code

 

Refer to  http://blog.sina.com.cn/s/blog_417b97470100glmi.html

       http://datadig.blog.163.com/blog/static/171229928201082075946726/

Published 331 original articles · 51 praises · 440,000 visits +

The default parameters of tomcat are formulated for the development environment, but not suitable for the production environment, especially the configuration of memory and threads. The default is very low, which is easy to become a performance bottleneck.

Guess you like

Origin blog.csdn.net/y41992910/article/details/97638347