The default Tomcat parameters are set for the development environment, rather than suitable for the production environment, especially the memory and thread configurations. The default values are very low and may become a performance bottleneck.
Tomcat Memory Optimization
Modify tomcat_home/bin/Catalina. Sh in Linux and add
Java_opts ="-XX: permsize = 64 m-XX: maxpermsize = 128 M-xms512m-xmx1024m-duser. timezone = Asia/Shanghai"
Modify tomcat_home/bin/Catalina. BAT in windows and add
Set java_opts =-XX: permsize = 64 m-XX: maxpermsize = 128 M-xms512m-xmx1024m
The maximum heap memory is 1024 mb. The current hardware is still low. During implementation, it is optimized according to the specific hardware configuration of the machine.
Tomcat thread Optimization
connector port =" 80 " protocol =" HTTP/1.1 " maxthreads =" 600 " minsparethreads =" 100 " maxsparethreads =" 500 " acceptcount =" 700 " connectiontimeout =" 20000 " redirectport =" 8443 " />
Maxthreads = "600" // maximum number of threads
Minsparethreads = "100" // Number of threads created during initialization
Maxsparethreads = "500" // once the created thread exceeds this value, Tomcat will be disabled and no longer needed.
Acceptcount = "700" // specifies 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 that exceed this number will not be processed.
Here is the optimization of HTTP ctor. If you use Apache and tomcat for Cluster load balancing and the AJP protocol for Apache and tomcat protocol forwarding, you also need to optimize AJP Connector.
<ConnectorPort= "8009"Protocol= "AJP/1.3"Maxthreads= "600"Minsparethreads= "100"Maxsparethreads= "500"Acceptcount= "700"Connectiontimeout= "20000"Redirectport= "8443" />
Because tomcat has multiple ctor S, the configuration of Tomcat threads also supports sharing one thread pool among multiple ctor S.
First. Open/CONF/server. xml and add
<ExecutorName= "Tomcatthreadpool"Nameprefix= "Catalina-Exec -"Maxthreads= "500"Minsparethreads= "20"Maxidletime= "60000" />
The maximum number of threads is 500 (generally enough on the server), the minimum number of Idle threads is 20, and the maximum idle time of the thread is 60 seconds.
Then, modify the <connector...> node, add the executor attribute, and set executor to the thread pool Name:
<ConnectorExecutor= "Tomcatthreadpool"Port= "80"Protocol= "Https/1.1"Connectiontimeout= "60000"Keepalivetimeout= "15000"Maxkeepaliverequests= "1"Redirectport= "443" />
One thread pool can be shared by multiple ctor s, so AJP Connector can also be set to use the tomcatthreadpool thread pool.
Disable DNS query
When a web applicationProgramWhen you want to record the client information, it also records the Client IP address or searches for the machine name through the Domain Name ServerConvert to IP address.
DNS queries require network occupation, and include obtaining the corresponding IP address from many distant servers or ineffective servers.Process, which will consume a certain amount of time.
ModifyConnector element, modifying attributesEnablelookups parameter value: enablelookups = "false"
If the value is true, you can call request. getremotehost () for DNS query to obtain the actual Host Name of the remote client. If the value is false, no DNS query is performed, but the IP address is returned.
Set the session expiration time
In conf \ WEB. XML, specify the following parameters:
<Session-config><Session-Timeout>180</Session-Timeout></Session-config>The Unit is minute.
APR plugin improves Tomcat Performance
Tomcat can use Apr to provide super scalability and performance, and better integrate local server technology.
APR (Apache Portable Runtime) is a highly portable library, which is the core of Apache HTTP Server 2.x. APR has many functions, including access to advanced Io functions (such as sendfile, epoll, and OpenSSL), OS-level functions (Random Number Generation, system status, and so on), and local process management (shared memory, NT pipeline and Unix sockets ). These functions enable Tomcat as a common front-end web server to better integrate with other local web technologies. In general, Java is more efficient as a high-performance WEB server platform rather than simply as a backend container.
In the product environment, especially when Tomcat is directly used as a Web server, Tomcat native should be used to improve its performance.
The best way to test the benefits of APR for Tomcat is to increase the number of Tomcat threads to over 300 on a slow network (simulating the Internet), and then simulate a large number of concurrent requests.
If it is not compatible with APR, basically 300 threads will soon be used up, and later requests will have to wait. However, with APR, the number of concurrent threads is significantly reduced, from 300 to dozens immediately, and new requests are not blocked.
In the LAN environment test, even if it is 400 concurrent threads, the processing/transmission is completed in an instant. However, in the real Internet environment, the page processing time is less than 0.1%, most of the time is used for page transmission. If APR is not used, a thread can only process one user at a time, which may cause blocking. Therefore, APR is necessary in the production environment.
(1 ) Install APR tomcat- Native APR - 1.3 . 8 . Tar Install. GZ in/usr/local/ APR # Tar Zxvf Apr- 1.3 . 8 . Tar . GZ # cd APR - 1.3 . 8 #. /Configure; Make ; Make Install APR -Util- 1.3 . 9 . Tar Install. GZ in/usr/local/APR/ Lib # Tar Zxvf Apr-util- 1.3 . 9 . Tar . GZ # cd APR -Util- 1.3 . 9 #. /Configure -- With-Apr =/usr/local/APR ---- with-Java-home = JDK; Make ; Make Install # Cd Apache -Tomcat- 6.0 . 20 / Bin # Tar Zxvf tomcat-native. Tar . GZ # cd Tomcat -Native/JNI/ Native #. /Configure -- With-Apr =/usr/local/APR; Make ; Make Install ( 2 ) Set Tomcat integration Apr to modify Tomcat startup shell (startup. Sh ), Add the startup parameter catalina_opts to the file. = " $ Catalina_opts-djava. Library. Path =/usr/local/APR/lib " . ( 3 Check whether the installation is successful. If the following startup log is displayed, the installation is successful. 2007 - 4 - 26 15 : 34 : 32 Org. Apache. Coyote. http11.http11aprprotocol init
Reference http://blog.sina.com.cn/s/blog_417b97470100glmi.html
Http://datadig.blog.163.com/blog/static/171229928201082075946726/