Tomcat's default configuration is not stable for a long time, that is, not suitable for production environment, it will panic, so that you continue to restart, even in the middle of the night to wake you up. For operating system optimization, it is possible to increase the amount of memory available, improve the frequency of the CPU, to ensure that the file system read and write speed. After the stress test verification, in many concurrent connections, the CPU processing power is stronger, the system runs faster.
Tomcat optimization is not like other software, simple to modify a few parameters on it, its optimization is mainly divided into three aspects, including system optimization, Tomcat itself optimization, Java virtual machine (JVM) tuning. System optimization is not introduced, and then the detailed introduction of Tomcat itself and JVM optimization, with Tomcat 7 for example.
one, Tomcat itself optimization
The optimization of Tomcat's own parameters, this piece is much like Apachehttp Server. Modify the parameters in the XML configuration file, adjust the maximum number of connections, timeout, and so on. Also, we install TOMCAT, and optimization is already started.
1. Choice of working mode
In order to improve performance, first of all, the code to the static and dynamic separation, so that Tomcat only responsible for the JSP file parsing work. As with Apache and Tomcat integration, there are three options for connecting between them, JK, Http_proxy and Ajp_proxy. Compared to JK connection mode, the latter two in the configuration is relatively simple, flexible aspects are not inferior. But in terms of stability is not as time-tested as JK, so it is recommended to use JK connection method.
2, the Connector connector configuration
The previous file describes the Tomcat connector three ways: BIO, NiO and APR, three ways performance is very different, Apr performance is the best, bio performance is the worst. Tomcat 7 uses the Connector default to enable the APR protocol, but requires the system to install the APR library, otherwise you will use the Bio method.
3, configuration file optimization
Configuration file optimization is actually the Server.xml optimization, can improve the ability of Tomcat processing request, let's look at the optimization in the Tomcat container.
By default, Tomcat creates a bound thread pool (maximum number of threads 200) for each connector, and 5 free threads are created by default at any time when the service starts, waiting for user requests.
First, open ${tomcat_home}/conf/server.xml, search "<executor name=" Tomcatthreadpool "", and turn on and adjust to
1 2 |
< Executor name = "Tomcatthreadpool" Nameprefix = "catalina-exec-" MaxThreads = "Minsparethreads =" 20 "Maxsparethreads =" MaxIdleTime = "60000"/> |
Note that Tomcat 7 must install the APR library before opening the thread pool and can be enabled, otherwise there will be errors, and the shutdown.sh script cannot close the process.
Then, modify <connector ...> node, add executor property, search "port=" 8080 "", adjust to
1 2 3 4 5 6 7 8 9 |
< connector& nbsp Executor = "Tomcatthreadpool" port = "8080" protocol = "http/1.1" uriencoding = "UTF-8" connectiontimeout = "30000" enablelookups = "false" disableuploadtimeout = "false" |