tomcat7 performance tuning

Since RHEL7
has added support for IPv6, the default tomcat monitor is IPv6 address. If you want tomcat to monitor IPv4 address, add the following parameters: JAVA_OPTS="-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses"


JAVA_OPTS="-server -Xmx4g -Xms4g -XX:PermSize=512m -XX:+UseParNewGC -XX:+AggressiveOpts -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses
-Dcom.sun.management.jmxremote -Djava.rmi.server. hostname=XXXX -Dcom.sun.management.jmxremote.authenticate
=false -Dcom.sun.management.jmxremote.ssl=false"



Document source: http://www.cnblogs.com/dsc65749924/p/6081432.html
Note: When tuning tomcat, you need to know what version of tomcat you are using. As the tomcat version develops, new parameters are introduced and old parameters are discarded. This document uses tomcat7 as an example for tuning

1. Thread Pool optimization

Edit the "Tomcat installation directory/conf/server.xml" file and find the following content

<!--

<Executor name="tomcatThreadPool" namePrefix="

        maxThreads="150" minSpareThreads="4"/>

-->











Uncomment, adjust the parameters as follows

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"

        maxThreads="1000" minSpareThreads="25"       

        maxIdleTime=" 600000" acceptCount="500"/>









Parameter description:

name This is the name of the thread pool, it must be unique, the default is OK, we will use this thing in the configuration later.
namePrefix The name prefix of the thread is used to mark the thread name , so that each thread uses this prefix to add the thread number, such as catalina-exec-1 catalina-exec-2
maxThreads The maximum number of threads that the thread pool can hold, tomcat uses threads to process received requests. Each thread processes a request , This value determines the maximum number of requests that can be processed at the same time. The default value is 200
minSpareThreads The minimum number of threads to keep alive, the default value is 4 (tomcat5 has this parameter, but tomcat6 does not have this parameter, and tomcat7 reuses this parameter , by the way, the meanings of these two parameters in 5 and 7 are not the same. If you are interested, you can check the official document.)
maxIdleTime is the time allowed for an idle thread to last before closing an idle thread. Only when the current number of idle threads is greater than the value of minSpareThread will the idle thread be closed. thread
acceptCount The maximum length of the connection request queue when all available request processing threads are used. When the queue is full, all requests are rejected. The default value is 10.

By the way, the following three parameters are maxSpareThreads, maxProcessors and minProcessors

maxSpareThreads: the maximum number of idle threads allowed, a parameter only available in tomcat5 and previous versions, Neither tomcat6 nor tomcat7 has this parameter minProcessors
: the minimum number of idle connection threads, which is used to improve system processing performance After these two parameters, tomcat4 is basically not used, so don't add these two Connector optimization Edit the "Tomcat installation directory/conf/server.xml" file , find the following content and comment it out <Connector port="80" protocol="HTTP/1.1"            connectionTimeout="20000"            redirectPort="8443" /> Find the following content and uncomment it <!-- <Connector executor=" tomcatThreadPool"        port="































































































Responsible for establishing HTTP connections. This connector is used by web applications to access the tomcat server through a browser. The default listening port is 8080. Therefore, what we optimized is the character set used for the 8080 port Connector URIEncoding="UTF-8" URI decoding, which only affects the URI decoding of the GET request and does not affect the decoding of the post.

































enableLookups="false" disables DNS lookups, the default value is true, should be set to false in order to improve processing capability
disableUploadTimeout="true" allows the servlet container to use a different, longer connection timeout when a servlet is executing. The end result is a longer time for the servlet to complete its execution, or a longer timeout when data is uploaded. If not specified, the default is false
connectionTimeout="20000" After the Connector accepts a connection, the time to wait for the first request to occur, in milliseconds. The default value is 60000 (60 seconds)
keepAliveTimeout="15000" The maximum interval between 2 requests in a long connection, after which the connection is disconnected, in milliseconds
maxKeepAliveRequests="1000" Before the server closes the connection, accept The maximum number of HTTP requests. If the value is set to 1, HTTP/1.0 keepalives are disabled, as well as HTTP/1.1 keepalives and pipelining. The default value is 100 if not specified.
useURIValidationHack="false" reduces its unnecessary checking of some urls to reduce overhead
compression="on" Set to on to enable the Connector to use HTTP/1.1 GZIP compression, which can save server bandwidth
compressionMinSize="2048" Enable compressed output Content size, which is set to 2KB
compressableMimeType="text/html, text/xml, text/javascript,
redirectPort="8443" If the Connector supports non-SSL requests, after receiving a request that requires SSL transmission, Catalina will automatically redirect the request to the port number specified here

3. JAVA virtual machine (JVM) optimization

Pay attention to tuning The JVM needs to know what version of the JDK it is using. With the development of the JDK version, new parameters are introduced and old parameters are discarded. This document uses JDK7 as an example for tuning

. The following figure shows the JVM memory structure diagram of JDK7 and previous old JDK versions. Before optimizing the JVM, you must know its memory structure. If you are interested, you can check more information. I will not describe it here.





JDK7's JVM memory consists of Heap (heap space) and Perm (persistent generation). Where Heap = {Old + young = { Eden , from, to } }

JDK8 JVM has removed Perm (persistent generation) from the memory space

Linux Platform, edit the "Tomcat installation directory/conf/catalina.sh" file, the beginning of the file is a large comment wrapped by #, and the following content is added at the end of the comment

export JAVA_OPTS="-server -Xms3072M -Xmx3072M -Xmn512M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:PermSize=256M -XX:MaxPermSize=256M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+ UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSMaxAbortablePrecleanTime=5 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Djava.awt.headless =true"













If you want to print the JVM running log information, you can add the following parameters, -Xloggs specifies the log path

-Xloggc:/path/jvm.log -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX :+PrintGCDetails







Windows platform, then edit the "Tomcat installation directory/conf/catalina.bat" file

set JAVA_OPTS=-server -Xms3072M -Xmx3072M -Xmn512M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:PermSize=256M -XX:MaxPermSize=256M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSMaxAbortablePrecleanTime=5 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Djava.awt.headless= true











If you want to print the JVM running log information, you can add the following parameters, -Xloggs specifies the log path

-Xloggc:D:\path\jvm.log -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX :+PrintGCDetails







parameter description:

-server specifies that the JAVA virtual machine runs in server mode

-Xms is the initial heap memory size. This document is set to 3G. It is necessary to set the appropriate value according to the actual situation such as the number of CPU cores and the total memory of the server. This value is not as large as possible. Too large will increase the pressure of garbage collection. Size, this document is set to 3G, you need to set the appropriate value according to the actual situation such as the number of CPU cores and total memory of the
server
. Thread stack size
-XX:+AggressiveOpts Enable the latest tuning results of the JVM development team, such as: compilation optimization, biased locking, parallel old age collection, introduced after JDK5.6, JDK6 is enabled by default
-XX:+UseBiasedLocking Enables an optimization Thread lock (biased lock), introduced after JDK5.6, JDK6 defaults to open
-XX:PermSize The initial persistent generation memory size, it should be noted that the permanent generation has been completely removed in JDK8, the parameters of the permanent generation -XX:PermSize and -XX:MaxPermSize has also been removed
-XX:MaxPermSize The maximum persistent generation memory size, it should be noted that the permanent generation has been completely removed in JDK8, and the permanent generation parameters -XX:PermSize and -XX:MaxPermSize have also been removed With
-XX:+DisableExplicitGC Suppresses the call to System.gc() method for GC
-XX:+UseConcMarkSweepGC Use Concurrent Mark Sweep (CMS) garbage collector which is old generation garbage collection. The CMS collector performs garbage collection concurrently through multiple threads to minimize pauses caused by garbage collection. With this garbage collector, the -XX:+UseParNewGC parameter will be enabled by default to use Parallel GC (parallel garbage collector) for the new generation.
-XX:+UseParNewGC The new generation uses Parallel GC (parallel garbage collector). If the old generation uses the CMS garbage collection period, the new generation defaults to this garbage collector.
-XX:+CMSParallelRemarkEnabled When the CMS garbage collector reclaims objects, The application has 2 pauses, the first is the initial mark, and the second is the re-mark. Turning on parallel remark can reduce the re-mark pause time.
-XX:+UseCMSCompactAtFullCollection CMS will not defragment the heap. In order to prevent heap fragmentation from causing full gc, Set this parameter to merge fragments during CMS garbage collection. Enabling this option will affect performance to a certain extent
-XX:CMSMaxAbortablePrecleanTime
-XX:+CMSClassUnloadingEnabled Enable CMS to recycle persistent generation to avoid full gc caused by Perm area full
-XX:LargePageSizeInBytes Specify Java Paging page size of heap
-XX:+UseFastAccessorMethods Convert the get() and set() methods to local code, and enable
-XX:+UseCMSInitiatingOccupancyOnly by default. Only when the usage percentage specified by -XX:CMSInitiatingOccupancyFraction is reached, the old age garbage collection will be performed
-XX :CMSInitiatingOccupancyFraction Specifies the percentage of space used by the old age to start garbage collection, JDK5 defaults to 68%, JDK6 defaults to 92%
-Djava.awt.headless Solve the problem that the chart tool in the J2EE project will cause the picture to display in the Linux/Unix environment. come out
-Xloggc Specifies the JVM log output to a file
-XX:+PrintGCDateStamps Prints the time when GC occurred, only supported by JDK6 and JDK7, if it is JDK5, please use -XX:+PrintGCTimeStamps
-XX:+PrintGCApplicationStoppedTime Print the time when the application was stopped by GC
-XX: +PrintGCApplicationConcurrentTime Prints the concurrent execution time of the application when GC is printed
-XX:+PrintGCDetails Prints GC details

IV . Adjust the running mode of the

Tomcat Connector First, get a general understanding of the three running modes of the Tomcat Connector: bio, nio and apr

bio

bio(blocking I/O ), as the name implies, blocking I/O operations, indicating that Tomcat uses traditional Java I/O operations (ie, the java.io package and its subpackages). Tomcat runs in bio mode by default. Unfortunately, in general, bio mode is the least performant of the three operating modes. We can view the current status of the server through the Tomcat Manager.



nio

nio (new I/O) is a new I/O operation method provided by Java SE 1.4 and subsequent versions (that is, the java.nio package and its subpackages). Java nio is a buffer-based Java API that provides non-blocking I/O operations, so nio is also seen as an abbreviation for non-blocking I/O. It has better concurrent running performance than traditional I/O operations (bio). It is also relatively simple to make Tomcat run in nio mode. We only need to configure the following in the /conf/server.xml file of the Tomcat installation directory:





































2015-12-13 16:17:49 org.apache.catalina.core.AprLifecycleListener init

info: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: xxx/xxx (Here is the path information)








The configuration of the Tomcat apr operating mode is a relatively troublesome one among the three operating modes. According to the official documentation, Tomcat apr mode requires the installation of the following three components:

     • APR library [APR library]

     • JNI wrappers for APR used by Tomcat (libtcnative) [On Windows, it is a file named tcnative-1.dll dynamic link library file]

     • OpenSSL libraries[OpenSSL library]

Tomcat6.x starts from 6.0.32 and Tomcat7.x starts from 7.0.30, the Windows version of Tomcat already comes with tcnative-1.dll and other files in the bin directory , and it runs in apr mode by default after startup, so for Windows operating system, we only need to download the latest version of Tomcat and use it directly. The Linux version of Tomcat comes with tomcat-native.tar.gz in the bin directory. This package needs to be compiled and installed together with the APR library before it can be used.


APR installation method Install
under Window (the version before Tomcat7.0.30 needs to be installed)
1. First download the latest version of tcnative-1.dll, download address: http://archive.apache.org/dist/tomcat/tomcat-connectors/native/

2. The file downloaded in this document is tomcat-native-1.2. 2-win32-bin.zip, decompress to get 2 tcnative-1.dlls of 32bit and 64bit, copy the corresponding tcnative-1.dll to the TOMCAT_HOME/bin directory according to the actual operating system bits, and start it

. 3. Verify whether Start
Seeing a message like this proves that apr has started
2015-12-13 15:17:05 org.apache.catalina.core.AprLifecycleListener init
information: Loaded APR based Apache Tomcat Native library 1.1.20.
2015-12-13 15 :17:05 org.apache.catalina.core.AprLifecycleListener init
info: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2015-12-13 15:17:05 org.apache.coyote.http11.Http11AprProtocol init









install under Linux
1. Download the required packages
The download path of apr/apr-iconv/apr-util: http://apr.apache.org/download.cgi
The software downloaded in this document is the following version
apr-1.5.2.tar.gz
apr-iconv-1.2. 1.tar.gz
apr-util-1.5.4.tar.gz

tomcat-native download path: http://archive.apache.org/dist/tomcat/tomcat-connectors/native
This document downloads tomcat-native- 1.2.2-src.tar.gz, if it is a version after Tomcat7.0.30, you can also find tomcat-native.tar.gz under the bin of the tomcat installation directory 

2. Install apr
tar zxvf apr-1.5.2.tar
cd apr -1.5.2
./configure --prefix=/usr/local/apr
make
make install










3. Install apr-iconv
tar -zxvf apr-iconv-1.2.1.tar.gz
cd apr-iconv-1.2.1
./ configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make
make install









4. Install apr-util
tar zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr /local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
make
make install








5. Install tomcat-native
Note: Please check whether openssl-devel is installed before installation, because it is added when compiling With the --with-ssl=yes option, this option requires the support of openssl-devel, and the yum source is installed through yum install openssl-devel. Of course, if this option is not added, openssl-devel may not be installed.

tar zxvf tomcat-native-1.2.2-src.tar.gz
cd tomcat-native-1.2.2-src/native
(if the version is tomcat-native-1.1.x-src.tar.gz, the path is tomcat-native -1.1.x-src/jni/native)
./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.7.0_80 --with-ssl=yes
make
make install









6. Add environment variables
Edit /etc/profile and add the following variable at the end of the file
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib  
and then execute the following command to make the environment variable take effect immediately
source /etc/profile

or modify the tomcat startup script catalina.sh /cactalina.bat
Add startup parameters to the file:
    JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/apr/lib"
or:
    CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local /apr/lib"

Note that if it is a windows platform, please do not add double quotes.

6. Verification If
you see a message like this, it proves that apr is successfully started.
Jul 20, 2011 15:27:32 PM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.20.



You can also view the current status of the server through Tomcat Manager


Five Postscript
In a rigorous attitude, I want to make this document as rigorous as possible, but my level is limited. I would like to ask readers to leave a message for the inappropriateness of the text. I would be very grateful! If it can help readers, it is not in vain for me to write this document. It needs to be stated that the following two blog posts are referenced in the fourth section of this document, and I would like to express my thanks to the bloggers here.
http://www.365mini.com/page/tomcat-connector-mode.htm
http://pengranxiang.iteye.com/blog/1128905

Guess you like

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