一 ,是基于tomcat cluster 来配置,当然这个配置也十分的简单,但是tomcat 官网建议tomcat cluster 配置,tomcat 不能超过4台,如果超过四台tomcat,不仅容易出现广播风暴,而且效率也是最低,为解决tomcat session 共享的问题,介绍一下memcached-session-Manager
二 memcache session manager 原理
MSM(Memcache-session-manager) 支持tomcat6 和tomcat7,利用Value(tomcat 阀值) 对Request 进行跟踪。 当tomcat 收到Request 请求时,从memcached 中加载session,Request 请求结束时,将tomcat session更新至memcached,以达到session 共享的目的,MSM 支持sticky 和non-sticky 两种模式
Sticky 模式:tomcat session 为主session,memcache 为备session。Request 请求到来时,首先从memcached加载备session 到tomcat(仅当tomct jvmroute 发上变化时,否则直接取的是tomcat session);Request 请求结束时,将tomcat session 更新到memcached,以达到主备同步之目的。 Stick模式就是前端的loadbanlence 能保证每个用户的请求都路由到同一个tomcat上。
Non-Sticky模式:tomcat session 为中转session,memcached1为主session,memcache 2 为备session。 Request 请求到来时,从memcached 2 中加载session 到tomcat,(当容器中还是没有session 则从memcached1 加载主session 到tomcat,这中情况是只有一个memcached节点,或者有memcached1出错时),Request请求结束时,将tomcat session更新至memcached1 和memcached 2 中,并清除tomcat session。以达到主备同步之目。Non-sticky则每一次请求都肯能路由到不同tomcat中
MSM 特性
支持Tomcat6 和tomcat7,支持黏性和非黏性session,无单一故障点
可处理tomcat 故障转移,memcached 故障转移,插件式session 序列话,允许异步保存session,以提升响应速度,只有当session 有修改时,才会将session写回到memcached
需要下载觉得jar 包如下:
kryo-1.03.jar
kry0-serializers-0.9.jar
memcached-session-manager-1.6.1.jar
memcached-session-manager-tc6-1.6.1.jar
memcached-2.5.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.1.jar
reflectasm-0.9.jar
spymemcached-2.8.2.jar
三 配置 sticky
1 使用默认的sticky session,kryo序列化方式,基于memcached 的缓存
需要配置$TOMCAT_HOME/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.3.44:11211,n2:192.168.3.44:11212"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
2 开启 tomcat jvmRouter $TOMCAT_HOME/conf/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat46">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat48">
3 配置spymemcached 日志,在tomcat 启动脚本中添加
CATALINA_OPTS="-Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.SunLogger"
4 配置MSM 的logging,在$TOMCAT_HOME/conf/logging.properties配置文件中添加如下
##memcached session manager log level
de.javakaffee.web.msm.level= FINE
# A handler's log level threshold can be set using SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST or ALL
net.spy.memcached.level = WARNING
# To make only the MemcachedConnection less verbose:
#net.spy.memcached.MemcachedConnection.level = WARNING
四 配置non-sticky
1 配置$TOMCAT_HOME/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.3.44:11211,n2:192.168.3.44:11212"
sticky="false"
lockingMode="auto"
requestUriIgnorePattern=".*\.(ico|png|gif|jps|css|js)$"
sessionBackupAsync="flase" sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
2 配置spymemcached 日志,在tomcat 启动脚本中添加
CATALINA_OPTS="-Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.SunLogger"
3 配置MSM 的logging,在$TOMCAT_HOME/conf/logging.properties配置文件中添加如下
##memcached session manager log level
de.javakaffee.web.msm.level= FINE
# A handler's log level threshold can be set using SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST or ALL
net.spy.memcached.level = WARNING
# To make only the MemcachedConnection less verbose:
#net.spy.memcached.MemcachedConnection.level = WARNING
注:参考的blog
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
http://miwucc.iteye.com/blog/1329729
http://gong1208.iteye.com/blog/1596120