一、Tomcat 优化(Tomcat8)

内存优化 catalina.sh

1
JAVA_OPTS="-server -Xms2048M -Xmx2048M  -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=5  -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_HOME/logs/heap.dump"

并发优化

参考 ${tomcat}/webapps/docs/config/http.html

主要参数 作用
maxConnections 最大连接数,The maximum number of connections that the server will accept and process at any given time.
acceptCount 最大接收数,The maximum queue length for incoming connection requests when all possible request processing threads are in use.
maxThreads 工作线程,The maximum number of request processing threads to be created by this Connector.
minSpareThreads 最小空闲的工作线程(初始化线程数),The minimum number of threads always kept running.

其他优化

  1. 参考 ${tomcat}/webapps/docs/config/host.html

    autoDeploy:This flag value indicates if Tomcat should check periodically for new or updated web applications while Tomcat is running.

  2. 参考 ${tomcat}/webapps/docs/config/http.html

    enableLookups:false

  3. 参考 ${tomcat}/webapps/docs/config/context.html

    reloadable:false

  4. connector:apr优化

    详见 http://apr.apache.org/,这是一种全新的网络调度模型,打破了传统的 BIO 和 NIO限制。

    注意:开启了 apr 之后,JVM 用到的 native 内存会增大,因此要适当调大 Metaspace 空间,添加 JVM 选项:

    1
    2
    -XX:MetaspaceSize=128m
    JAVA_OPTS="-server -Xms2048M -Xmx2048M -XX:MetaspaceSize=128M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_HOME/logs/heap.dump"

二、nginx 优化

文档地址:http://nginx.org/en/docs/

工作线程数和并发连接数

1
worker_rlimit_nofile 20480; #每个进程打开的最大的文件数=worker_connections*2是安全的,受限于操作系统(/etc/security/limits.conf)
1
2
3
4
5
vi /etc/security/limits.conf
* hard nofile 204800
* soft nofile 204800
* soft core unlimited
* soft stack 204800
1
2
3
4
5
6
worker_processes 4; #cpu,如果nginx单独在一台机器上,一般为核数的1~2倍
events {
worker_connections 10240; #每一个进程打开的最大连接数,包含了nginx与客户端和upstream之间的连接
multi_accept on; #可以一次建立多个连接
use epoll;
}

操作系统优化

配置文件 /etc/sysctl.conf

1
2
3
4
5
sysctl -w net.ipv4.tcp_syncookies=1; #防止一个套接字在有过多试图连接到达时引起过载
sysctl-w net.core.somaxconn=1024; #默认128,连接队列
sysctl-w net.ipv4.tcp_fin_timeout=10; #timewait的超时时间
sysctl -w net.ipv4.tcp_tw_reuse=1; #os直接使用timewait的连接
sysctl -w net.ipv4.tcp_tw_recycle=0; #回收禁用

Keepalive 长连接

nginx 与 upstream server:

1
2
3
4
upstream server_pool{
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
keepalive 300; #300个长连接
}

同时要在 location 中设置:

1
2
3
4
5
location /  {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

客户端与 nginx(默认是打开的):

1
2
3
keepalive_timeout 60s; #长连接的超时时间
keepalive_requests 100; #100个请求之后就关闭连接,可以调大
keepalive_disable msie6; #ie6禁用

启用压缩

1
2
3
4
5
6
7
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on; #Vary: Accept-Encoding
gzip_static on; #如果有压缩好的,直接使用

状态监控

1
2
3
4
5
6
location = /nginx_status {
stub_status on;
access_log off;
allow <YOURIPADDRESS>;
deny all;
}

输出结果:

1
2
3
4
Active connections: 1 
server accepts handled requests
17122 17122 34873
Reading: 0 Writing: 1 Waiting: 0

Active connections: 当前实时的并发连接数
accepts: 收到的总连接数
handled: 处理的总连接数
requests: 处理的总请求数
Reading: 当前有多少个读,读取客户端的请求
Writing: 当前有多少个写,向客户端输出
Waiting: 当前有多少个长连接 (reading + writing)
reading – nginx reads request header
writing – nginx reads request body, processes request, or writes response to a client
waiting – keep-alive connections, actually it is active - (reading + writing)

实时请求信息统计 ngxtop

https://github.com/lebinh/ngxtop

  1. 安装 python-pip

    1
    2
    yum install epel-release
    yum install python-pip
  2. 安装 ngxtop

    1
    pip install ngxtop
  3. 使用

    指定配置文件:ngxtop -c ./conf/nginx.conf

    查询状态是200:ngxtop -c ./conf/nginx.conf --filter 'status == 200'

    查询哪个 ip 访问最多:ngxtop -c ./conf/nginx.conf --group-by remote_addr

三、LVS 四层负载均衡

是什么

LVS:linux virtual server

相关文章:

http://www.linuxvirtualserver.org/
http://zh.linuxvirtualserver.org/

http://www.austintek.com/LVS/LVS-HOWTO/HOWTO/
http://www.linuxvirtualserver.org/whatis.html

工作模式

  1. VS/NAT:修改报文头信息
  2. VS/TUNE:IP隧道
  3. VS/DR:必须得在同一个网段(一般用这个)

八种调度算法

轮询,加权轮询,最小连接,加权最小连接,局部最小连接,带复制的局部最小连接,目标地址散列,原地址散列

四、Keepalived 高可用

http://www.keepalived.org/

Keepalived 双机热备