Tomcat Performance Tuning

Source: Internet
Author: User
Tags gz file dedicated server apache tomcat

1. Integrate Apache

Although Tomcat can also be used as a Web server, the 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, let's talk about the clear division of labor between the HTML and JSP functions, so that Tomcat can only process the JSP part, and other Web servers such as Apache and IIS can handle it, thus greatly improving the running efficiency of Tomcat.

Deployment method:

  • Install the Apache server
  • Deploy Tomcat
  • Copy mod_jk.so to the modules directory.
  • Modify httpd. conf and mod_jk.conf

Use Cases:Application systems that use a large number of static pages

 

2. Apache + Tomcat Cluster

For systems with high concurrency requirements, Server Load balancer is required to share the pressure on Tomcat servers.

There are four Load Balancing Methods:

1) Through DNS, but it can only be implemented in turn and cannot handle faults.

2) Server Load balancer is provided based on ms iis and Windows 2003 Server.

3) hardware is implemented through the switch function or a dedicated server Load balancer device.

4) install software on a server Load balancer instance.

Server Load balancer using Apache HTTP Server, and tomcat for cluster nodes are used to achieve load balancing in the preceding 4th Methods: Apache + Tomcat cluster. This method can maximize the performance of servers. You can deploy multiple Tomcat servers on servers with higher configurations, or deploy Tomcat on multiple servers separately, JK is used to integrate Apache and tomcat. After verification, when the system responds to a large number of users, the Apache + 3tomcat cluster> Apache + 2tomcat cluster> Apache integrates Tomcat> A single Tomcat cluster and multiple Tomcat containers are used, if a tomcat instance goes down, the system can continue to use it. Therefore, if the hardware system has excellent performance, you need to maximize the performance of the software. You can add a tomcat cluster.

Configuration file used by the Apache + Tomcat cluster:

  • Httpd. conf
  • Mod_jk.conf (jk information configuration, including JK path)
  • Workers. properties (connection definition file for Tomcat server)

The running parameters that need to be adjusted for Apache:

1) set MPM Parameters

  • Threadperchild --- used to set the number of threads for each process (in windows, the default value is 64 and the maximum value is 1920. The recommended value is 100 ~ 500)
  • Maxrequestperchild --- maximum number of requests that each sub-process can process (largely dependent on the server's memory. The recommended value is 3000)

2) disable DNS and name resolution

  • Hostnamelookup off

3) Open the 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

Deployment method:

  • Install the Apache server
  • Deploy Tomcat Clusters
  • Copy mod_jk.so to the modules directory.
  • Modify configuration files

Use Cases:High number of concurrent users

 

3. JVM Optimization

1) JDK version Selection

To meet the needs of the project, we recommend that you use a JVM with a higher version. In general, the higher version of the product will improve the speed and efficiency compared with the lower version. 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.

2) Select the JVM version

Sun's JVM dynamic library has two versions: client and server, which are optimized for desktop and server applications respectively. The Client Version loading speed is fast, server version loading speed is slow but it runs fast. Enter Java-version on the command line to view the version currently configured by JVM. If you want to modify the jvm的, you can modify the JVM. dll called by java.exe, which is determined by JVM. cfg. Edit % java_home %/JRE/lib/i386/JVM. in the cfg file, the first line is-client (default is the Client Version), and the-server known of the second line is placed in the first line, as shown below:

-Server known
-Client known
-Hotspot aliased_to-Client
-Classic warn
-Native Error
-Green Error

Restart Tomcat and enter Java-version in the command line. The following information is displayed:

Java version "1.6.0 _ 13"
Java (TM) se Runtime Environment (build 1.6.0 _ 13-b03)
Java hotspot (TM) server VM (build 11.3-B02, mixed mode)

It indicates that tomcat has been changed to the JVM of the server version. You can perform a comparative test to compare the performance difference between the JVM of the Client Version and the server version to determine which version of JVM to use. In addition, you can also consider replacing the JVM of other vendors, such as Oracle's jrockit.


3) JVM Parameter Optimization

Example of JVM startup parameter configuration:
Export java_opts = "-server-xms1400m-xmx1400m-xss512k-XX: + bytes-XX: + usebiasedlocking-XX: permsize = 128 M-XX: maxpermsize = 256 m-XX: + disableexplicitgc-XX: maxtenuringthreshold = 31-XX: + useconcmarksweepgc-XX: + useparnewgc-XX: + cmsparallelremarkenabled-XX: + signature-XX: Signature = 128 M-XX: + usefastaccessormethods-XX: + usecmsinitiatingoccupancyonly-djava. AWT. headless = true"

 

4. Tomcat container Optimization

1) disable DNS query

To eliminate the time consumed by DNS queries, you can disable DNS queries in Tomcat:

  • Enablelookups = "false" --- in the server. xml file

2) Adjust the number of threads

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 maximum CPU utilization, keeping idle time at the lowest level, and thus receiving 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 enough 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 it takes one second to process each request task, 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, so it is necessary to prevent uncontrollable traffic (or malicious service attacks), thus exceeding the memory used by the virtual machine. To increase the number of concurrent connections, increase the two parameters at the same time (but do not forget that the maximum number of connections allowed by the Web server is also subject to the kernel parameter settings of the operating system ).

Tomcat5 adjusted these parameters (in server. XML:

  • Maxthreads: Tomcat uses threads to process each request received. This value indicatesMaximum number of threads that can be created by Tomcat.
  • Acceptcount: specifies the number of threads that can be used to process requests,Number of requests that can be placed in the processing queue, Requests exceeding this number will not be 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:Number of threads created during Tomcat Initialization.
  • Maxsparethreads: Once the created thread 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.

 

5. ARP Database

The so-called Apache Tomcat native library is actually called Apr, all called Apache Portable Runtime and tomcat. Apache Tomcat native library is a set of localized socket, thread, and Io components developed by Apache to improve Tomcat performance. That is to say, Apache Tomcat has advanced Io functions and calls functions at the operating system level, as well as local process processing, these can make Tomcat more like a web server (Like Apache), rather than just being used to interpret JSP, that is to say, to improve the throughput performance of individual Tomcat as a server.

In tomcat, the APR library is actually used to read files and perform network transmission by using JNI (short for Java Native Interface, which is called locally in Java) in Tomcat. 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 Tomcat Apr, 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.

How can I determine whether the APR library is enabled for Tomcat? You can view the Tomcat startup log Catalina. out:

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 to org. Apache. Coyote. http11.http11aprprotocol start.

APR reference: http://tomcat.apache.org/tomcat-5.5-doc/apr.html

 

Install and deploy APR:

1. Install APR in Windows
APR for Windows is a file named: tcnative-1.dll. Go to the following URL to download the matching version of The tcnative-1.dll, the download of the tcnative-1.dll file to the <$ java_home>/bin directory, start Tomcat You Can See That tomcat has been loaded deployed APR: loaded APR based Apache Tomcat native library 1.1.20

2. Install APR in Linux
1) install APR
Download apr-1.2.12.tar.gz to the http://apr.apache.org and install it with the following command:
Tar-xvf apr-1.2.12.tar.gz
CD apr-1.2.12
./Configure -- prefix =/tomcat/APR
Make
Make install

2) install APR-UTIL
Download apr-util-1.2.12.tar.gz to the http://apr.apache.org and install it with the following command:
Tar-xvf apr-util-1.2.12.tar.gz
CD apr-util-1.2.12
./Configure -- prefix =/tomcat/APR -- With-Apr =/tomcat/APR
Make
Make install

3) install Tomcat native Library
CD/usr/local/tomcat/bin
Tar zxvf tomcat-native.tar.gz
CD tomcat-native-1.1.10-src/JNI/native
./Configure -- prefix =/tomcat/APR -- With-Apr =/tomcat/APR-with-Java-home =/usr/JDK
Make
Make install

4) EDIT tomcat/bin/Catalina. Sh
Set java_opts = "$ catalina_opts-djava. Library. Path = tomcat/APR/lib"
Before # ----- execute the requested command ---------------------------------------------

5) add Environment Variables
# Vi/etc/profile
Add:
Export LD_LIBRARY_PATH =/usr/local/APR/lib

6) After restarting tomcat, check the Tomcat output log. It can be seen that APR has been loaded successfully.
# Vim/usr/local/tomcat/logs/Catalina. Out

Information: loaded Apache Tomcat native library 1.1.10.
2008-7-8 10:20:27 org. Apache. Catalina. Core. aprlifecyclelistener init
Information: Apr capabilities: ipv6 [True], sendfile [True], accept filters [false], random [True].
10:20:27 org. Apache. Coyote. http11.http11aprprotocol init

 

6. Compression

HTTP compression can greatly improve the speed of browsing websites. The principle is that after the client requests a webpage, the server compresses the webpage file and downloads it to the client, the client browser is responsible for decompression and browsing. Compared with ordinary browsing processes such as HTML, CSS, JS, and text, this can save about 40% of the traffic. More importantly, it can also compress pages dynamically generated, including CGI, PHP, JSP, ASP, Servlet, shtml, and other output pages. The compression efficiency is astonishing.

  • Compression = "on" enable the compression function
  • Compressionminsize = "2048" enables the size of compressed output content. The default value is 2 kb.
  • Nocompressionuseragents = "gozilla, Traviata" does not enable compression for the following browsers:
  • Compressablemimetype = "text/html, text/XML" compression type

(In server. XML) is configured as follows:

<Connector port="8088" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"               compression="on"                compressionMinSize1="2048"                noCompressionUserAgents="gozilla, traviata"                compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"/>

 

Overview of Tomcat optimization:

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 relieves Tomcat's processing capability. 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.

 

Important parameter description:

1) maxthreads-Monitoring and adjusting the number of connection threads

Maxthreads is the maximum number of concurrent threads. If the concurrent Request volume exceeds this value, Tomcat will not add more threads, and concurrent requests will enter the queue. Increasing the value of maxthreads can increase the concurrent processing capability of Tomcat. However, setting a high value of maxthreads will also affect the performance, occupy too many system resources, and even cause Tomcat to crash. The number of threads can be monitored on the Tomcat Manager status page.

Test tomcat7 to illustrate the impact of maxthreads on Tomcat server performance:

Tomcat heap memory is set to 256 MB. Use LoadRunner to start 50 virtual users to test the jpetstore homepage (http: // 192.168.1.101: 8080/jpetstore/shop/index.shtml ), the following test results are displayed:
(1) The default configuration is maxthreads = 200, and the average transaction response time is 0.390.
(2), maxthreads = 5, average Transaction Response Time: 0.355
(3), maxthreads = 50, average Transaction Response Time: 0.369
(4), maxthreads = 3, average Transaction Response Time: 0.559
As you can see, when maxthreads is set for an hour, Tomcat's request processing speed decreases. If strict concurrency is set in the test script (the lr_rendezvous function is inserted), the following result is displayed:
(1), maxthreads = 3, average Transaction Response Time: 2.609
(2), maxthreads = 50, average Transaction Response Time: 0.597
As you can see, in the case of high concurrency, if the maxthreads value of Tomcat is set relatively small, it will seriously affect the performance. Generally, you need to set a value greater than the maximum number of concurrent requests at the same time.
Note: Generally, the maximum number of connections allowed by Web servers is limited by the kernel parameter settings of the operating system. Generally, Windows has about 2000 connections and Linux has about 1000 connections. In centos, run the following command to view the maximum number of socket connections: ulimit-
Among them, open files are the maximum number of files that can be opened. The default value of centos is 1024. You can set this value by modifying the VI/etc/security/limits. conf file. For example, to 32768, you can add the following values after the file:
* Soft nofile 32768
* Hard nofile 32768
Save and restart the system.

To verify the impact of Linux system parameters on socket connections, we can conduct a comparative test and modify/etc/security/limits. conf:
* Soft nofile 200
* Hard nofile 200
Start 200 VU concurrency in LoadRunner (set the set point). In the Controller's connection and connection per second graph, the number of connections is less (compared to the default 1024 ):

(1) set the parameter to 200
Average Transaction Response Time: 19.664
Connections: 177.376
Connections per second-New Connection: 28.082
Connections per second-connection shutdown: 28.082

(2) set the parameter to 1024 (default)
Average Transaction Response Time: 2.712
Connections: 315.136
Connections per second-New Connection: 114.000
Connections per second-connection shutdown: 114.000

When the establishment of a socket connection is restricted, Tomcat's concurrent processing capability is also significantly affected, and the average transaction response time increases sharply.

 

2) connectiontimeout-Connection timeout settings

Connectiontimeout is the wait time (in milliseconds) for connector to accept connections to the submitted URI. The default value is 60000 (60 seconds ). If ontimeout is set to a relatively small value, a 502 error may occur when a large number of users access the website.

Telnet test: use Telnet to connect to Tomcat directly. If you do not keep the input, the connection will be disconnected soon (if the connectiontimeout time is set to a small value). If you do not keep the input, the connection will be retained.

For detailed explanation and configuration of this item, refer to Tomcat help documentation: http://tomcat.apache.org/tomcat-5.5-doc/config/http.html

 

 

3)AcceptcounT-Set the maximum number of queues

Acceptcount indicates how many new connection requests are allowed to wait in the queue when all threads are used to process requests. When the queue is full, any new request will be rejected. The default value is 100.
The configuration example is as follows:

<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"maxThreads="800" acceptCount="1000"/>


Maxthreads is the maximum number of threads started by Tomcat. The default value is 200. acceptcount is the number of requests queued when the number of threads started by Tomcat reaches the maximum. The default value is 100, the two values work together, and the following three situations may occur:

  • Scenario 1: accept a request. At this time, the number of threads started by tomcat has not reached the number set by maxthreads. Tomcat starts a thread to process the request.
  • Case 2: accept a request. At this time, the number of threads started by tomcat has reached the number set by maxthreads. Tomcat will put the request in the waiting queue and wait for the idle thread to be available.
  • Case 3: accept a request. At this time, the number of threads started by tomcat has reached the number set by maxthreads, and the number of requests in the waiting queue has reached the number set by acceptcount, at this time, Tomcat will directly reject this request and return the connection refused error.

 

 

For other performance-related adjustment parameters of Sun's hotspot JVM, refer:

Http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#PerformanceTuning

 

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.