tomcat配置优化

tomcat在生产上的一些优化配置

自定义jvm和jvm参数配置

windows

自定义jvm 修改{tomcat home}/bin/setclasspath.bat,在开头增加

1
2
set JAVA_HOME={java path}
set JRE_HOME={jre path}

配置jvm参数 修改{tomcat home}/bin/catalina.bat,在开头增加

1
2
3
4
5
6
7
8
9
10
##jvm参数(仅供参考)
set JAVA_OPTS=%JAVA_OPTS% -server -Djava.awt.headless=true -Xms4096m -Xmx4096m -XX:PermSize=256m -XX:MaxPermSize=256m
JAVA_OPTS =%JAVA_OPTS% -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection
JAVA_OPTS =%JAVA_OPTS% -XX:CMSFullGCsBeforeCompaction=0 -XX:+UseFastAccessorMethods -XX:CMSInitiatingOccupancyFraction=70
JAVA_OPTS =%JAVA_OPTS% -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:{gc log path}/gc.log
##jmx参数(仅供参考)
set CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port={jms port}
CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false
CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.access.file={access file path}
CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.password.file={passwd file path}

Linux

自定义jvm 修改{tomcat home}/bin/setclasspath.sh,在开头增加

1
2
export JAVA_HOME={java path}
export JRE_HOME={jre path}

配置jvm参数 修改{tomcat home}/bin/catalina.bat,在开头增加

1
2
3
4
5
6
7
8
9
10
##jvm参数(仅供参考)
export JAVA_OPTS=$JAVA_OPTS -server -Djava.awt.headless=true -Xms4096m -Xmx4096m -XX:PermSize=256m -XX:MaxPermSize=256m
JAVA_OPTS =$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection
JAVA_OPTS =$JAVA_OPTS -XX:CMSFullGCsBeforeCompaction=0 -XX:+UseFastAccessorMethods -XX:CMSInitiatingOccupancyFraction=70
JAVA_OPTS =$JAVA_OPTS -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:{gc log path}/gc.log
##jmx参数(仅供参考)
set CATALINA_OPTS=$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port={jms port}
CATALINA_OPTS=$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false
CATALINA_OPTS=$CATALINA_OPTS -Dcom.sun.management.jmxremote.access.file={access file path}
CATALINA_OPTS=$CATALINA_OPTS -Dcom.sun.management.jmxremote.password.file={passwd file path}

access and passwd file template

access

1
2
3
4
5
#name	rights
jmxControl readwrite \
create javax.management.monitor.*,javax.management.timer.* \
unregister
jmxMonitor readonly

passwd

1
2
3
#name	password
jmxControl xxx
jmxMonitor xxx

tomcat Connector 常用优化

http 协议

endpoint适用性选择


场景 选择
稳定的 BIO>NIO=APR
SSL APR>NIO>BIO
低并发 BIO>APR>NIO
高并发(非长连接) BIO>APR>NIO
高并发(长连接) APR>NIO>BIO

配置实例

1
2
3
4
5
6
7
8
9
10
11
<Connector port="8080"
protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443"
maxThreads="500"
minSpareThreads="20"
acceptCount="100"
disableUploadTimeout="true"
enableLookups="false"
URIEncoding="UTF-8"
22 server="Web Serv 1.0"/>

apr的安装
下载 tomcat native , apr
在上面两个连接分别下载
apr-x.x.x.tar.gz
apr-util-x.x.x.tar.gz
Native x.x.x Source Release tar.gz

解压后按顺序安装

1
2
3
4
5
6
7
8
9
10
11
cd apr-x.x.x
./configure --prefix=/opt/tomcat-native/apr
make && make install

cd apr-util-x.x.x
./configure --prefix=/opt/tomcat-native/apr-utils --with-apr=//opt/tomcat-native/apr
make && make install

cd tomcat-native-x.x.x-src
./configure --prefix=/opt/tomcat-native/tomcat-native --with-apr=/opt/tomcat-native/apr --with-java-home=/opt/jdk1.8
make && make install

设置环境变量

1
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/tomcat-native/tomcat-native/lib:/opt/tomcat-native/apr/lib:/opt/tomcat-native/apr-utils/lib

tomcat启动如有一下日志说明安装成功

1
INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.5.2.

参考资料

Tomcat 8(十)HTTP/AJP Connector、Bio/Nio/Apr性能对照

Tomcat的三种模式及并发调优

How to Optimize Tomcat Performance

tomcat 8 connector 配置文档

tomcat 7 connector 配置文档