Tomcat + Memcached实现session共享
Linux Memcached安装
Linux系统安装memcached,要先安装libevent库。
yum install libevent libevent-devel
下载http://memcached.org,并上传解压缩
#解压缩
tar -zxvf memcached-1.6.6.tar.gz
#进入目录
cd memcached-1.6.6
#配置
./configure --prefix=/usr/local/memcached-1.6.6
#编译并安装
make && make install
Memcached运行
#帮助命令
/usr/local/memcached-1.6.6/bin/memcached -h
启动选项:
- -d是启动一个守护进程;
- -m是分配给Memcache使用的内存数量,单位是MB;
- -u是运行Memcache的用户;
- -l是监听的服务器IP地址,可以有多个地址;
- -p是设置Memcache监听的端口,,最好是1024以上的端口;
- -c是最大运行的并发连接数,默认是1024;
- -P是设置保存Memcache的pid文件。
作为后台服务程序运行
/usr/local/memcached-1.6.6/bin/memcached -p 11211 -m 64m -d
或者
/usr/local/memcached-1.6.6/bin/memcached -d -m 64M -u root -l 192.168.0.200 -p 11211 -c 256 -P /tmp/memcached.pid
Nginx配置
upstream tomcat{
server 192.168.2.52:8080;
server 192.168.2.53:8080;
}
location /tomcat {
proxy_pass http://tomcat/;
}
Tomcat配置
1.每个tomcat的context.xml中加入
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.71.100:11211"
sticky="false"
lockingMode="auto"
sessionBackupAsync="false"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
2.到tomcat的lib目录下,添加jar包
链接:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#add-custom-serializers-to-your-webapp-optional
此处以tomcat8为例
<!-- memcached-session-manager-tc8 依赖 memcached-session-manager -->
<dependency>
<groupId>de.javakaffee.msm</groupId>
<artifactId>memcached-session-manager-tc8</artifactId>
<version>2.3.2</version>
</dependency>
<!-- memcached-session-manager 依赖 spymemcached-->
<dependency>
<groupId>de.javakaffee.msm</groupId>
<artifactId>memcached-session-manager</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.0</version>
</dependency>
<!-- msm-kryo-serializer 依赖 kryo-serializers、 kryo-serializers -->
<dependency>
<groupId>de.javakaffee.msm</groupId>
<artifactId>msm-kryo-serializer</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.41</version>
</dependency>
<!-- kryo 依赖minlog、reflectasm、objenesis -->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>minlog</artifactId>
<version>1.3.0</version>
</dependency>
<dependency><!-- tomcat7不需要这个包??? -->
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.2</version>
</dependency>
<!--reflectasm 依赖 asm -->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>reflectasm</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.4</version>
</dependency>
3.在每个tomcat的webapps/Root目录下修改index.html,加入如下代码
<h1>server-02</h1><% out.print( session.getId()); %>