Tomcat performance optimization solution

Source: Internet
Author: User
Tags dedicated server domain name server
I. operating system optimization for operating system optimization, it is to increase the available memory capacity as much as possible, increase the CPU frequency, and ensure the read/write rate of the file system. Stress tests show that the CPU processing capability is stronger and the system runs faster when many concurrent connections exist .. [Suitable... information &

 

I. operating system optimization

For operating system optimization, it is to increase the available memory capacity as much as possible, increase the CPU frequency, and ensure the read/write rate of the file system. Stress tests show that the CPU processing capability is stronger and the system runs faster when many concurrent connections exist ..

[Applicable scenario] any project.

II. Java virtual machine optimization

SUN's JVM should be selected. when meeting the project's needs, we should try to use a JVM with a higher version. In general, higher version products will improve the speed and efficiency compared with lower versions.

JDK1.4 has improved the performance by nearly 10%-20% over JDK1.3, and JDK1.5 has improved the performance by 25%-75% over JDK1.4.

Therefore, JDK1.6 is recommended for high performance requirements.

[Applicable scenario] any project.

III. integrate Apache with Tomcat

The Web server specifically processes HTTP requests. 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 inferior to that of Apache, and its function as a web server is far inferior to that of Apache. Therefore, Apache and Tomcat are integrated, the division of html and Jsp functions is clearly defined, so that Tomcat can only process Jsp, and other web servers such as Apache and IIS can process them, thus greatly improving Tomcat's operational efficiency.

If a project uses a large number of static pages and images, and has a large access volume, we recommend that you use Apache to integrate Tomcat to improve the overall system performance.

There are three ways to integrate Apache and Tomcat: JK, http_proxy, and ajp_proxy. the JK method is the most common method. JK has two versions: 1 and 2. the latest version of 1 is 1.2.8, and version 2 has been abandoned. Http_proxy uses the mod_proxy module provided by Apache to connect to Tomcat using the proxy technology. The Ajp_proxy connection method is actually the same as the http_proxy method, and is provided by mod_proxy. You only need to replace http: // in the configuration with ajp: //, and connect to the port of the Tomcat AJP Connector.

Compared with JK's connection method, the last two methods are relatively simple in configuration, and the flexibility is not inferior at all. However, the stability is not as long as JK has been tested, so we recommend using the JK connection method.

Apache + JK + Tomcat configuration:

The two configuration files used are httpd. conf and mod_jk.conf. Httpd. conf is the configuration file of the Apache server. it 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 the Apache server

2. deploy Tomcat

3. copy mod_jk.so to the modules directory.

4. modify httpd. conf and mod_jk.conf

[Application scenario] a large number of static page application systems are used.

4. Apache and Tomcat clusters

For systems with high concurrency requirements, we need to use server load balancer to share the pressure on Tomcat servers. There are four load balancing implementations: first, DNS, but it can only be implemented in turn, and fault cannot be handled; second, it is based on ms iis, windows 2003 server itself carries the server load balancer service. The third is the hardware mode, which is implemented through the switch function or a dedicated server load balancer device. The fourth is the software mode, install software on a server load balancer instance. Using Apache Httpd Server for load balancer, Tomcat cluster nodes can use Tomcat to achieve the fourth method above. this method is flexible and the cost is relatively low, another major advantage is that you can make some flexible configuration based on the application and server conditions. Therefore, we recommend using Apache + Tomcat clusters for load balancing.

Using Tomcat clusters can maximize the performance of servers. you can deploy multiple Tomcat servers on servers with high configurations, or deploy Tomcat on multiple servers, apache and Tomcat are integrated in JK mode. After verification, the system responds to a large number of users. Apache + 3366cat cluster> Apache + 2Tomcat cluster> Apache Integrated Tomcat> single Tomcat. In addition, when the Apache + multi-Tomcat cluster deployment mode is adopted, if a Tomcat fails, the system can continue to use it. Therefore, when the hardware system performance is better enough, to maximize the performance of the software, you can add a Tomcat cluster.

The configuration files used by the Apache + Tomcat cluster include httpd. conf, mod_jk.conf, and workers. properties. Mod_jk.conf is the configuration of JK information, including the JK path. the workers. properties configuration file is the connection definition file for the Tomcat server.

Apache needs to adjust the running parameters to build a web service suitable for the corresponding network environment. The following configuration can be optimized:

1. set MPM (Multi Processing Modules Multi-channel Processing module ). ThreadPerChild: this parameter is used to set the number of threads for each process. in Windows, the default value is 64 and the maximum value is 1920. it is recommended to set it to a value between and. if the server performance is high, the value is larger, and vice versa. MaxRequestPerChild indicates the maximum number of requests that each sub-process can process. The value of this parameter depends more on the memory of the server. if the memory is large, it can be set to a large parameter. Otherwise, a smaller value is set. the recommended value is 3000.

2. disable DNS and name resolution HostnameLookups off

3. open UseCanonicalName module UseCanonicalName on

4. disable redundant modules. generally, modules that do not need to be loaded include mod_include.so, mod_autoindex.so, mod_access.so, and mod_auth.so.

5. enable KeepAlive support

KeepAlive on, KeepAliveTimeout 15 MaxKeepAliveRequests 1000

Based on actual experience, the effect of improving system performance through Apache and Tomcat clusters is very obvious. this method can maximize the use of hardware resources, the processing of multiple Tomcat servers is used to share the pressure on a single Tomcat server.

[Deployment steps]

1. install the Apache server

2. deploy the Tomcat cluster, that is, multiple identical Tomcat servers.

3. copy mod_jk.so to the modules directory.

4. modify httpd. conf, mod_jk.conf, and workers. properties.

[Application scenario] a system with a large number of concurrent users and online users.

V. Tomcat optimization

1. JVM parameter optimization:-Xms Indicates the size of the JVM initialization heap,-Xmx Indicates the maximum value of the JVM heap. The size of these two values is generally set as needed. When the memory required by the application exceeds the maximum value of the heap, the VM will prompt a memory overflow and cause the application service to crash. Therefore, we recommend that you set the maximum value of the heap to 80% of the maximum available memory. In catalina. bat, set JAVA_OPTS = '-Xms256m-xmx512m', which indicates that the initial memory is 256 MB and the maximum memory available is 512 MB.

2. disable DNS query

When a web application wants to record information about the client, it also records the IP address of the client or converts the machine name from the domain name server to the IP address. DNS queries require network occupation, and include the process of obtaining the corresponding IP address from many servers that are far away or do not work, which consumes a certain amount of time. To eliminate the impact of DNS queries on performance, we can disable DNS queries by modifying the value of the enableLookups parameter in the server. xml file:

Tomcat4

 

 

Tomcat5

 

3. adjust the number of threads

The parameter for performance control through the application Connector is the number of threads created to process requests. Tomcat uses the thread pool to accelerate the response speed to process requests. In Java, a thread is the path when the program is running and a code segment that is independent of other control threads in a program. They share the same address space. Multithreading helps programmers write efficient programs with the highest CPU utilization, keeping idle time at the lowest level, and thus accepting more requests.

In Tomcat4, you can control the number of threads by modifying the values of minProcessors and maxProcessors. These values have been set to the default value after installation and are sufficient for use, but these values are increased as the site expands. The number of threads created when the minProcessors server is started should be sufficient to process a small amount of load. That is to say, if only five click events occur per second in a day and each request task takes one second to process, it is enough to set the number of threads in advance. However, when your site traffic is large, you need to set a larger number of threads to the value of the maxProcessors parameter. The value of maxProcessors is also limited. it is necessary to prevent uncontrollable traffic (or malicious service attacks), thus exceeding the memory used by virtual machines. To increase the number of concurrent connections, increase these two parameters at the same time. The maximum number of connections allowed by the web server is also subject to the kernel parameter settings of the operating system. generally, Windows has about 2000 connections, and Linux has about 1000 connections.

These parameters have been adjusted in Tomcat5. please refer to the following attributes:

MaxThreads Tomcat uses threads to process each request received. This value indicates the maximum number of threads that Tomcat can create.

AcceptCount indicates the number of requests that can be placed in the processing queue when the number of threads that can be used to process requests is used. requests exceeding this number are not processed.

ConnnectionTimeout network connection timeout, in milliseconds. If it is set to 0, it indicates that the request never times out. this setting has potential risks. Generally, it can be set to 30000 Ms.

MinSpareThreads the number of threads created during Tomcat initialization.

 

Once the thread created by maxSpareThreads exceeds this value, Tomcat closes the socket thread that is no longer needed.

The best way is to set multiple times and perform tests to observe the response time and memory usage. Different machines, operating systems, or virtual machines may have different combinations, and not all websites have the same traffic, therefore, there is no one-size-fits-all solution to determine the number of threads.

VI. APR database usage

In Tomcat, the APR library is used to read files and perform network transmission in Tomcat using JNI. It can greatly improve Tomcat's processing performance on static files. if you use HTTPS transmission, it can also improve the processing performance of SSL.

Generally, in Windows, you can directly download the compiled binary version of the dll library file to enable APR for Tomcat. it is recommended to copy the library file tcnative-1. dll to the bin directory of Tomcat. In linux, you can directly decompress and install the tomcat_native.tar.gz file under the bindirectory. before compiling, make sure that the apr Library has been installed.

How can I determine whether the APR Library is enabled for Tomcat? The method is to view the Tomcat startup log:

If APR is not enabled, the startup log generally contains the following:

Org. apache. coyote. http11.Http11Protocol start

If APR is enabled, this log is changed:

Org. apache. coyote. http11.Http11AprProtocol start

Tcnative-1.dll: http://tomcat.heanet.ie/native/

Tuning overview

Based on the above analysis, if you want Tomcat to achieve the optimal effect, you must first strive to optimize the operating system and network resources, and use a later version of JDK. For systems with a large number of static pages, the static pages are handled by Apache by integrating Apache with Tomcat, and the dynamic pages are handled by Tomcat, which greatly frees Tomcat from processing capabilities. Using the ARP library can also greatly improve Tomcat's ability to process static files. For systems with high concurrency requirements, the Apache and Tomcat clusters are used to separately share the load to multiple Tomcat servers, which greatly improves the system performance and makes full use of hardware resources. At the same time, you need to optimize Tomcat itself, including increasing the memory and adjusting the number of concurrent threads.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.