Tomcat performance tuning solution

1. Operating system optimization
For operating system optimization, it is to increase the available memory capacity as much as possible, increase the frequency of the CPU, and ensure the read and write rate of the file system. It has been verified by the stress test that in the case of many concurrent connections, the stronger the processing power of the CPU, the faster the system runs. .
[Applicable scene] Any item.
2. Java Virtual Machine Tuning The JVM of SUN
should be selected. On the premise of meeting the needs of the project, try to choose a JVM with a higher version. Generally speaking, the speed and efficiency of a higher version of the product will be improved compared to the lower version.
Compared with JDK1.3, the performance of JDK1.4 is improved by nearly 10%-20%, and the performance of JDK1.5 is improved by 25%-75% than that of JDK1.4.
Therefore, it is recommended to use JDK1.6 for higher performance requirements.
[Applicable scene] Any item.
3. Apache integrates the Tomcat
Web server to process HTTP requests, and the application server provides business logic for applications through many protocols. Although Tomcat can also be used as a web server, its processing speed of static html is not as fast as that of Apache, and its function as a web server is far inferior to that of Apache. Therefore, Apache and Tomcat are integrated, and the functional parts of html and Jsp are clearly divided. Let Tomcat only handle the Jsp part, and the rest are handled by web servers such as Apache and IIS, thereby greatly improving the operating efficiency of Tomcat.
If a project uses a lot of static pages, a large number of pictures, etc., and has a large number of visits, it is recommended to use Apache to integrate Tomcat to improve the overall performance of the system.
There are three ways to integrate Apache and Tomcat, namely JK, http_proxy and ajp_proxy. The JK way is the most common way. JK itself has two versions, 1 and 2. The latest version of 1 is 1.2.8, and version 2 Already abandoned. http_proxy uses the mod_proxy module that comes with Apache to use proxy technology to connect to Tomcat. The Ajp_proxy connection method is actually the same as the http_proxy method, which is provided by mod_proxy. Just replace http:// in the configuration with ajp://, and connect to the port where Tomcat's AJP Connector is located.
Compared with the connection method of JK, the latter two are relatively simple in configuration and are not inferior in terms of flexibility. But in terms of stability, it is not as well-tested as JK, so it is recommended to use JK's connection method.
Apache+JK+Tomcat configuration:
The two configuration files used are: httpd.conf and mod_jk.conf. Among them, httpd.conf is the configuration file of the Apache server, which is used to load the JK module and specify the JK configuration file information. mod_jk.conf is the connection definition file to the Tomcat server.
[Deployment steps]
1. Install Apache server
2. Deploy Tomcat
3. Copy mod_jk.so to the modules directory
4. Modify httpd.conf and mod_jk.conf
[Applicable scenarios] Application systems that use a lot of static pages.
4. Apache and Tomcat clusters
For systems with high concurrency requirements, we need to adopt a load balancing method to share the pressure on the Tomcat server. There are about four kinds of load balancing implementations: the first is through DNS, but it can only be distributed in turn and cannot handle faults; the second is based on MS IIS, the windows 2003 server itself has a load balancing service; the third is the hardware method , through the switch function or a special load balancing device; the fourth is the software method, which is carried out through a load balancing server on which the software is installed. Using Apache Httpd Server as a load balancer, Tomcat cluster nodes can use Tomcat to achieve the above fourth method. This method is more flexible and relatively low cost. Another great advantage is that it can be based on the application and server conditions. Do some flexible configuration. Therefore, it is recommended to use Apache+Tomcat cluster to achieve load balancing.
Using a Tomcat cluster can maximize the performance of the server. You can deploy multiple Tomcats on a server with a higher configuration, or you can deploy Tomcat on multiple servers separately. The integration method of Apache and Tomcat is still the JK method. After verification, the response of the system to the large number of users is Apache+3Tomcat cluster > Apache+2Tomcat cluster > Apache integrated Tomcat > single Tomcat. And when using Apache + multi-Tomcat cluster deployment, if one Tomcat goes down, the system can continue to be used. Therefore, if the performance of the hardware system is sufficiently superior, it is necessary to maximize the performance of the software, and the method of increasing the Tomcat cluster can be adopted.
The configuration files used in the Apache+Tomcat cluster are httpd.conf, mod_jk.conf, and workers.properties. The mod_jk.conf is the configuration of the JK information, including the path of the JK, and the workers.properties configuration file is the connection definition file for the Tomcat server.
Apache needs to adjust the operating parameters so that it can build a web service suitable for the corresponding network environment. The optimized configurations that can be performed are as follows:
1. Set up MPM (Multi Processing Modules). ThreadPerChild, this parameter is used to set the number of threads per process. In Windows environment, the default value is 64, and the maximum value is 1920. It is recommended to set it between 100 and 500. If the server performance is high, the value will be larger, and vice versa. MaxRequestPerChild indicates the maximum number of requests each child process can handle. The value of this parameter depends to a greater extent on the memory of the server. If the memory is relatively large, it can be set to a large parameter. Otherwise, set a small value. The recommended value is 3000.
2. Turn off DNS and name resolution HostnameLookups off
3 . Turn on the UseCanonicalName module UseCanonicalName on
4. Turn off redundant modules Generally speaking, the modules that do not need to be loaded are mod_include.so, mod_autoindex.so, mod_access.so, mod_auth.so.
5. Turn on KeepAlive support 
KeepAlive on, KeepAliveTimeout 15 MaxKeepAliveRequests 1000
     According to actual experience, the effect of improving system performance through Apache and Tomcat clustering is very obvious. This method can maximize the use of hardware resources and share the pressure of a single Tomcat through the processing of multiple Tomcats.
[Deployment steps]
1. Install the Apache server
2. Deploy a Tomcat cluster, that is, multiple identical Tomcats.
3. Copy mod_jk.so to the modules directory
4. Modify httpd.conf, mod_jk.conf and workers.properties
[Applicable scenarios] Systems with a relatively high number of concurrent users and online users.
V. Tomcat's own optimization
1. JVM parameter tuning: -Xms<size> represents the size of the JVM initialization heap, -Xmx<size> represents the maximum value of the JVM heap. The size of these two values ​​is generally set as required. When the memory required by the application exceeds the maximum heap size, the virtual machine will prompt a memory overflow and cause the application service to crash. Therefore, it is generally recommended that the maximum heap size be set to 80% of the maximum available memory. In catalina.bat, set JAVA_OPTS='-Xms256m -Xmx512m', indicating that the initial memory is 256MB, and the maximum memory that can be used is 512MB.
2. Disable DNS query
 When the web application asks to record the client's information, it also records the client's IP address or converts the machine name to the IP address by looking up the machine name through the domain name server. DNS query needs to occupy the network, and includes the process of obtaining the corresponding IP from many distant servers or non-functional servers, which will consume a certain amount of time. In order to eliminate the impact of DNS query on performance, we can turn off DNS query by modifying the enableLookups parameter value in the server.xml file: Tomcat4
<

Connector className="org.apache.coyote.tomcat4.CoyoteConnector" port="80" minProcessors= "5" maxProcessors="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="







      These parameters have been adjusted in Tomcat5, see the following properties:
maxThreads Tomcat uses threads to process each request it receives. This value represents the maximum number of threads that Tomcat can create.
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.
connectionTimeout Network connection timeout, unit: milliseconds. Setting it to 0 means that it will never time out, which is a hidden danger. Usually can be set to 30000 milliseconds.
minSpareThreads The number of threads created when Tomcat is initialized.

maxSpareThreads Once the number of threads created exceeds this value, Tomcat will close socket threads that are no longer needed.
     The best way is to set it up a few times and test it to observe the response time and memory usage. It may be different on different machine, OS or VM combinations, and not all web sites have the same traffic, so there is no one-size-fits-all solution to the value of the number of threads.
6. APR library Using
the APR library in Tomcat is actually using JNI in Tomcat to read files and perform network transmission. It can greatly improve the processing performance of Tomcat for static files. At the same time, if you use HTTPS for transmission, it can also improve the processing performance of SSL.
Generally under Windows, you can directly download the compiled binary version of the dll library file to enable Tomcat to enable APR. Generally, it is recommended to copy the library file tcnative-1.dll to the bin directory of Tomcat. Under Linux, you can directly decompress and install the tomcat_native.tar.gz file in the bin directory. Before compiling, make sure that the apr library has been installed.
How can I tell if Tomcat has enabled the APR library? The method is to look at the startup log of Tomcat:
if APR is not enabled, the startup log generally has the following entry:
org.apache.coyote.http11.Http11Protocol start
If APR is enabled, this log will become:
org.apache. coyote.http11.Http11AprProtocol start       tcnative
-1.dll Download address: http://tomcat.heanet.ie/native/
   Tuning overview
Network resources are optimized, and it is best to use a higher version of the JDK. For a system with a large number of static pages, using Apache to integrate Tomcat, the static pages are handled by Apache, and the dynamic parts are handled by Tomcat, which can greatly liberate Tomcat's processing capabilities. Using the ARP library can also greatly improve Tomcat's ability to process static files. For systems with high concurrency requirements, using Apache and Tomcat clusters to share the load among multiple Tomcats can greatly improve the performance of the system and make full use of hardware resources. At the same time, Tomcat itself needs to be optimized, including increasing memory, adjusting the number of concurrent threads, etc.

Guess you like

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