一、 需求
多个tomcat要一起协同工作有几种办法,可以考虑的方案有以下几个:
1. 使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单。但这个方案的效率比较低,在大并发下表现并不好。
2. 利用nginx的基于访问ip的hash路由策略,保证访问的ip始终被路由到同一个tomcat上,这个配置更简单。但如果应用是某一个局域网大量用户同时登录,这样负载均衡就没什么作用了。
3. 利用memcached把多个tomcat的session集中管理,前端在利用nginx负载均衡和动静态资源分离,在兼顾系统水平扩展的同时又能保证较高的性能。
这里选择方案3。
二、环境
系统:Linux
软件:nginx-1.2.7,memcached-1.4.15,tomcat7。
依赖:libevent-2.0.21-stable(memcached),
asm-3.2.jar
minlog-1.2.jar
reflectasm-1.0.jar
kryo-1.03.jar
kryo-serializers-0.9.jar(如果在官方下载的有问题,我就遇到缺少了DateSerializer类,请自己去git上打包:https://github.com/magro/kryo-serializers)
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar
memcached-session-manager-tc6-1.6.3.jar
msm-kryo-serializer-1.6.3.jar
spymemcached-2.8.4.jar
三、搭建
1.Memcached(官网:http://memcached.org/):
tar zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/desp/local/libevent-2.0.21
make
make install
-------------------------------------------------------------------
tar zxvf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/desp/local/memcached-1.4.15 --with-libevent=/desp/local/libevent-2.0.21/
make
make install
2.Ngnix(如想安装其他模块,请参考官网:http://nginx.org/cn/):
tar zxvf nginx-1.2.7.tar.gz
cd nginx-1.2.7
./configure --prefix=/desp/ nginx/
make
make install
修改nginx.conf(http://nginx.org/cn/docs/)
#user nobody;
worker_processes 16;
pid sbin/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
client_body_buffer_size 128k;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_types text/javascript text/plain text/css application/xml application/x-javascript;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Connection Close;
server_names_hash_max_size 1024;
server_names_hash_bucket_size 1024;
# Default cache parameters for use by virtual hosts
# Set the cache path to tmpfs mounted disk, and the zone name
# Set the maximum size of the on disk cache to less than the tmpfs file system size
proxy_cache_path ./cache levels=1:2 keys_zone=pscms:100m max_size=800m;
proxy_temp_path ./proxy;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#配置后端服务器信息
upstream web_server {
#ip_hash;
server 172.168.1.222:8080 max_fails=3 fail_timeout=30s;
server 172.168.1.222:8081 max_fails=3 fail_timeout=30s;
}
server {
listen 8888; ## listen for ipv4
#listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name 172.168.1.221;
charset utf-8;
access_log logs/host.access.log main;
#access_log off;
location ~ .*\.(jsp|action)?$ {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://web_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache pscms; #进行缓存,使用Web缓存区cache_one
proxy_cache_valid 200 304 1h; #对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 301 302 5m;
proxy_cache_valid any 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding ""; #(或是后台服务器关闭gzip),这样这台机器才不会缓存被压缩的文件,造成乱码
proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。
proxy_pass http://web_server;
expires 15m;
}
location / {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://web_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
3.tomcat
把asm-3.2.jar,minlog-1.2.jar,reflectasm-1.0.jar,kryo-1.03.jar,kryo-serializers-0.9.jar,memcached-session-manager-1.6.3.jar,memcached-session-manager-tc7-1.6.3.jar(有6、7两个jar分别对应tomcat6、7),msm-kryo-serializer-1.6.3.jar,spymemcached-2.8.4.jar放到tomcat/lib/下.
修改context.xml或者server.xml:
jvmroute就是给memcached的标识。
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.168.1.221:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico|jpeg|htm|html)$"
sessionBackupAsync="false"
sessionBackupTimeout="1800000"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
更多选项请查看:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
此项目地址:https://github.com/magro/memcached-session-manager
四:原理
原文:http://xylonwang.iteye.com/blog/1269704,http://miwucc.iteye.com/blog/1329729
MSM(memcached-session-manager) 支持tomcat6 和tomcat7 ,利用 Value(Tomcat 阀)对Request进行跟踪。Request请求到来时,从memcached加载session,Request请求结束时,将tomcat session更新至memcached,以达到session共享之目的, 支持 sticky 和 non-sticky 模式。
1.Sticky 模式:
tomcat session 为主session,memcached 为备session。Request请求到来时, 从memcached加载备 session 到 tomcat (仅当tomcat jvmroute发生变化时,否则直接取tomcat session);Request请求结束时,将tomcat session更新至memcached,以达到主备同步之目的。
2.Non-Sticky模式:
tomcat session 为 中转session, memcached1 为主 session,memcached 2 为备session。Request请求到来时,从memcached 2加载备 session 到 tomcat,(当 容器 中还是没有session 则从memcached1加载主 session 到 tomcat, 这种情况是只有一个memcached节点,或者有memcached1 出错时),Request请求结束时,将tomcat session更新至 主memcached1和备memcached2,并且清除tomcat session 。以达到主备同步之目的。