最近在项目中负责Tomcat高并发优化方案写一写新得。
优化1)tomcat默认的并发是75,可以启用线程池根据生产环境硬件设定线程池大小。
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="40"/>
并在使用时引用线程池。
并发在150左右
优化2)tomcat7及之前的版本默认采用BIO,启用NIO模式(NIO与BIO差距自行脑补)
<Connector executor="tomcatThreadPool"
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />
经过这次优化后250的并发量一般是没问题
优化3)APR模式,因为实际生产环境大部分在linux下,所以直说Linux下的安装
(1)先安装gcc (没有yum命令先安装yum,sudo apt -get install yum)
yum -y install gcc
(2) 按装apr和apr-util
下载地址http://apr.apache.org/download.cgi
apr-1.3.2.tar.gz安装:
tar zxvf apr-1.3.2.tar.gz
cd apr-1.3.2
./configure --prefix=/usr/local/apr
make
make install
apr-util-1.3.2.tar.gz 安装:
tar zxvf apr-util-1.3.2.tar.gz
cd apr-util-1.3.2
./configure --prefix=/usr/local/apr-util
make
make install
(3)安装pcre 下载地址https://sourceforge.net/projects/pcre/files/pcre/
unzip -o pcre-8.33.zip # cd pcre-8.33
# ./configure --prefix=/usr/local/pcre
make
make install
(4)安装apache http://httpd.apache.org/download.cgi
tar zxvf httpd-2.4.25.tar.tz
./configure --prefix=/usr/local/apache --enable-modules=all --enable-mods-shared=all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-rewrite --enable-status --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre
make
make install
(5)
配置apache开机自启动
在/etc/rc.local(或者/etc/rc.d/rc.local)中加入如下一下 /opt/apache/bin/apachectl -k start
查看是否正常启动:ps -ef | grep httpd
经过APR优化后并发量轻松破300
-----------------------------华丽的分割线------------------------------------------------------------
网卡会成为tomcat吞吐量的瓶颈,下面方法是建议优化linux配置方案
1. 修改/etc/sysctl.cnf文件,在最后追加如下内容: net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 65536
2. 保存退出,执行sysctl -p生效
经过网卡层的优化,tomcat的吞吐量轻松提升200-300
--------------------------------华丽的分割线----------------------------------------------------------------
优化JVM这是少不了的一层。jvm的主要优化GC内存管理、设置堆、栈大小。需要根据实际的生产情况进行设置,以下仅仅是举例,jvm调优本身就是一门深技术
在%TOMCAT_HOME%/bin/catalina.sh的开头添加上,
declare -x JAVA_OPTS='-Xms1024M -Xmx1024M -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'
-------------------------------华丽的分割线-----------------------------------------------------------------------------
建议:我们在优化的过程中应该多测试,不能仅靠只言片语,我们应该用数据分析总结结论。
END
参考地址