tomcat tuning

In the online environment, we use tomcat as the web server, and its processing performance is directly related to the user experience. In the usual work and study, the following six kinds of tuning experience are summarized.
1. Server resources The performance of the CPU, memory, and hard disk that the
server can provide has a decisive impact on the processing capacity.
(1) In the case of high concurrency, there will be a large number of operations, so the speed of the CPU will directly affect the processing speed.
(2) In the case of a large amount of data processing in the memory, there will be a large memory capacity requirement. You can use parameters such as -Xmx -Xms -XX:MaxPermSize to divide different functional blocks of the memory. We have encountered insufficient memory allocation before, causing the virtual machine to be in full GC all the time, resulting in a serious drop in processing power.
(3) The main problem of the hard disk is the read and write performance. When a large number of files are read and written, the disk can easily become a performance bottleneck. The best way is to use the cache mentioned below.
2. Utilize caching and compression
For static pages, it is best to cache them so that you don't have to read them from disk every time. Here we use Nginx as the cache server to cache images, css and js files, which effectively reduces the access to the backend tomcat.
In addition, in order to speed up the network transmission speed, it is also essential to turn on gzip compression. But considering that tomcat already needs to deal with a lot of things, the compression work is handed over to the front-end Nginx to complete.
In addition to text can be compressed with gzip, in fact, many pictures can also be pre-compressed with image processing tools, finding a balance point can make the loss of image quality small and the file size can be reduced a lot. I have seen a picture compressed from more than 300 kb to dozens of kb, and I can hardly see the difference.
3. Adopt a cluster
The performance of a single server is always limited, and the best way is to achieve horizontal expansion, so forming a tomcat cluster is an effective way to improve performance. We still use Nginx as the server for request offloading, and multiple tomcats on the backend share sessions to work together.
4. Optimize tomcat parameters
Here , take the parameter configuration of tomcat7 as an example, you need to modify the conf/server.xml file, mainly to optimize the connection configuration and close the client dns query.
<Connector port="8080" 
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           connectionTimeout="20000"
           redirectPort="8443"  maxThreads=
           "500" 
           minSpareThreads="20"
           acceptCount="100"
           disableUploadTimeout="true"
           enableLookups="false" 
           URIEncoding="UTF-8" />
5.
If the BIO model adopted by the APR library tomcat is used by default, the performance will be seriously degraded under hundreds of concurrency. Tomcat comes with a NIO model, and it can also call the APR library to implement operating system level control.
The NIO model is built-in, and it is very convenient to call. Just modify the protocol in the above configuration file to org.apache.coyote.http11.Http11NioProtocol, and restart it to take effect. I have changed the above configuration, the default is HTTP/1.1.
APR needs to install third-party libraries, which will significantly improve performance under high concurrency. For the specific installation method, please refer to "The APR based Apache Tomcat Native library which allows ...java.library...Exception under Linux to solve the Tomcat startup report]"
6. Optimizing the network
Joel also clearly proposed that optimizing the network card driver can effectively improve performance, which is very important for cluster environments. It is especially important when working. Since we use a linux server, optimizing the kernel parameters is also a very important task. Optimization parameters for a reference:
1. Modify the /etc/sysctl.cnf file and append the following at the end:
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 65536

2. 保存退出,执行sysctl -p生效

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993355&siteId=291194637