Ubuntu服务器上JSP并发处理技巧
一 架构与负载均衡
upstream tomcat_servers {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
location / {
proxy_pass http://tomcat_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
二 Tomcat与JVM关键配置
<Executor name="tomcatThreadPool"
namePrefix="catalina-exec-"
maxThreads="500"
minSpareThreads="30"
maxIdleTime="60000"
prestartminSpareThreads="true"
maxQueueSize="100"/>
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
enableLookups="false"
compression="on"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
acceptCount="100"/>
JAVA_OPTS="$JAVA_OPTS -server \
-Xms4g -Xmx4g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:+PrintGCDetails -Xloggc:$CATALINA_HOME/logs/gc.log"
三 Linux系统层面优化
# /etc/security/limits.conf
* soft nofile 655360
* hard nofile 655360
* soft nproc 655360
* hard nproc 655360
# /etc/sysctl.conf
net.core.somaxconn = 8192
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 10000
net.core.rmem_max = 1310720
net.core.wmem_max = 1310720
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
sysctl -p --system。注意不同内核版本对 tcp_tw_recycle 的支持差异,生产谨慎启用。四 应用与数据库层优化
五 压测与监控闭环
top/htop、free -m、df -h、iftop 观察资源瓶颈。