Tomcat集群session同步方案有以下几种方式:
以下使用第四种方案,集群环境:
1. nginx最新版本:1.5.7
2. tomcat版本:6.0.37
3. memcached最新版本:1.4.15
4. session复制同步使用memcache-session-manager最新版本:1.6.5
5. 系统:CentOS6.3
一、Nginx安装
- centos6.3默认未安装gcc-c++,先装gcc:
yum -y install gcc-c++
装完后记得reboot系统。
- cd到安装目录
cd /usr/lcoal/src
- 安装pcre库
cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz tar -zxvf pcre-8.21.tar.gz cd pcre-8.21 ./configure make make install
如果wget下载不到的话,去官网下载pcre-8.12.tar.gz包拷贝到src下。
- 安装zlib库
cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install
- 安装ssl
cd /usr/local/src wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz tar -zxvf openssl-1.0.1c.tar.gz
- 安装nginx
cd nginx-1.5.7 ./configure --prefix=/usr/local/nginx/nginx \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.12 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.1c make make install
安装成功,cd /usr/local/nginx/conf/nginx.conf,修改配置文件:
http {
...
upstream localhost {
server localhost:8081;
server localhost:8082;
server localhost:8083;
}
...
}
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
二、memcached安装
- memcached安装较简单,需要先libevent库:
sudo yum install libevent libevent-devel wget http://www.danga.com/memcached/dist/memcached-1.4.15.tar.gz tar zxf memcached-1.4.15.tar.gz cd memcached-1.4.15 ./configure make sudo make install
安装成功(默认安装在bin下),启动:
#-vv 控制台输出 #-d 后台运行 /usr/local/bin/memcached -vv
启动后,可以telnet上去看下状态:
telnet 127.0.0.1 11211 stats
三、tomcat配置
1. 修改server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> ... <Context path="" docBase="/demo/appserver/app/cluster" debug="0" reloadable="true" crossContext="true"> <Manager
className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.2.43: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" /> </Context>
2. 添加mem和msm的依赖jar包
couchbase-client-1.2.2.jar
javolution-5.4.3.1.jar
kryo-1.03.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.5.jar
memcached-session-manager-tc6-1.6.5.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.5.jar
reflectasm-0.9.jar
spymemcached-2.10.2.jar
注意点:
-msm1.6.5依赖了Couchbase,需要添加couchbase-client的jar包,否则启动会报:java.lang.NoClassDefFoundError: com/couchbase/client/CouchbaseClient。
-tomcat6和7使用不同msm支持包:memcached-session-manager-tc6-1.6.5.jar和memcached-session-manager-tc7-1.6.5.jar,只可选一,否则启动报错。
-msm源码中的lib包版本太低:spymemcached-2.7.jar需要使用2.10.2,否则启动tomcat报错:
java.lang.NoSuchMethodError: net.spy.memcached.MemcachedClient.set(Ljava/lang/String;ILjava/lang/Object;)Lnet/spy/memcached/internal/OperationFuture;
at de.javakaffee.web.msm.BackupSessionTask.storeSessionInMemcached(BackupSessionTask.java:227)
kryo-serializers-0.8.jar需要使用0.10版本,否则报错:
Caused by: java.lang.ClassNotFoundException: de.javakaffee.kryoserializers.DateSerializer
tomcat启动成功后可以去刷新页面,ip端口会变化,session是不会变化的:
memcached的状态可以看到同步session的操作日志: