最近由于工作需要看了很多tomcat性能优化的资料,在此记录总结一下,以备日后之需。
总结起来其实有三点:
一、tomcat启动JVM参数调优
具体做法为在catalina.bat前面加上JAVA_OPTS参数设置
set JAVA_OPTS=
-server #以服务器模式启动,启动速度慢,但更稳定,性能更好
-Xms1024M
-Xmx1024M #-Xms与-Xmx设成一样的值,避免JVM因为频繁的GC导致性能大起大落
-Xss512k
-XX:+AggressiveOpts
-XX:+UseBiasedLocking
-XX:PermSize=64M #内存永久保留区域
-XX:MaxPermSize=300M #内存永久保留区域
-XX:+DisableExplicitGC
-XX:MaxTenuringThreshold=31
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:+CMSParallelRemarkEnabled
-XX:+UseCMSCompactAtFullCollection
-XX:LargePageSizeInBytes=128m
-XX:+UseFastAccessorMethods
-XX:+UseCMSInitiatingOccupancyOnly
-Djava.awt.headless=true
具体数字要根据你的生产环境来调整。
二、开启tomcat线程池,设置最大连接数量
这里主要是设置server.xml
<Executor
maxThreads="1000"//The max number of active threads in this pool, default is 200
minSpareThreads="100"//The minimum number of threads always kept alive, default is 25
name="tomcatThreadPool"
namePrefix="catalina-exec-"
prestartminSpareThreads="true"//Whether minSpareThreads should be started when starting the Executor or not, the default is false
/>
<Connector
SSLEnabled="fasle"
acceptCount="1000"
connectionTimeout="20000"
enableLookups="false"
executor="tomcatThreadPool"
port="8080"
protocol="org.apache.coyote.http11.Http11AprProtocol"
redirectPort="8444"
useBodyEncodingForURI="true"
URIEncoding="UTF-8"
/>
其中maxThreads
是连接池中的最大连接数量,而acceptCount
为连接池占满时等待请求的最大队列长度,超过这个数量的连接tomcat不处理,因此maxThreads+acceptCount
就是tomcat的最大连接数,可根据业务具体需要来调整这两个数值。
三、开启APR模式
protocol
则是设置tomcat处理请求的方式,
org.apache.coyote.http11.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector.
从性能上来说APR>NIO>BIO
,启用APR模式需要安装APR依赖库,windows平台tomcat7自带,linux中则需要手动安装,在此不做详解。
注:因为现在的项目基本都做了动静分离,静态文件不需要tomcat处理,所以tomcat文件压缩配置也就不需要了。
本文是对博客
http://blog.chopmoon.com/favorites/231.html
http://blog.youkuaiyun.com/ldx891113/article/details/51735171
的总结,原文更加详细,同时tomcat的配置参数也只提到了关键的几个,如果要详细了解可参考tomcat官方文档
http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
里面对配置参数有详细的说明。