memcached服务器两台:192.168.223.136,192.168.223.137
tomcat多实例:192.168.233.146:8081,192.168.223.146:8082
for tomcat configure(这里使用的tomcat8的版本)
参考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
Add memcached-session-manager jars to tomcat
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.1.1/memcached-session-manager-2.1.1.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/2.1.1/memcached-session-manager-tc8-2.1.1.jar
wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
下载需要的memcached的三个jar文件
将这三个jar文件复制到$CATALINA_HOME/lib/目录下
由于采用的多实例
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.16
export CATALINA_BASE=/usr/local/tomcat-instance/tomcat1
如果仅仅只是用java来做序列化器只需要这三个包就ok
配置tomcat多实例:
tomcat1的添加配置如下:
<Context path="" docBase="ROOT" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.223.136:11211,n2:192.168.223.137:11211" 这里的ip就是memcached服务器的两个ip
failoverNodes="n1"
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
/>
</Context>
将红色添加的部分放进各应用程序的context组件内
tomcat2的添加配置和tomcat1一样:
<Context path="" docBase="ROOT" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.223.136:11211,n2:192.168.223.137:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
/>
</Context>
开启各自的tomcat的服务
接下来配置memcached服务器,这里采用的yum安装:
yum install memcached -y(两台服务器192.168.223.136,192.168.223.137分别都给装上)
访问中可以看见session会话保持已经成功,查看memcached缓存信息:
[root@wadeson ~]# memcached-tool 127.0.0.1
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
6 304B 553s 1 2 no 0 0 0
然后只需要配置前端代理到tomcat即可实现负载均衡
另补充:
如果需要采用其他的序列进行session保持:
1、Example for sticky sessions + kryo(需要的包
- kryo-serializer: msm-kryo-serializer, kryo-serializers-0.34+, kryo-3.x, minlog, reflectasm, asm-5.x, objenesis-2.x
)
<Context> ... <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211" failoverNodes="n1" requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> </Context>
2、javolution-serializer: msm-javolution-serializer, javolution-5.4.3.
<Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.155:11211,n2:192.168.1.11:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory" 各个序列器就这里做了变化
/>
</Context>
3、If you're using Redis, you need the jedis-2.9.0.jar.
更多详情参考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration