tomcat&memcached实现session共享

Tomcat:apache-tomcat-7.0.67.tar.gz

memcached:memcached-1.4.25.tar.gz

libevent:libevent-2.0.22-stable.tar.gz

1,memcached安装请参考上一篇文章;

安装所需之lib包

http://pan.baidu.com/s/1bfPIdo

2,测试session 共享之项目依然使用前几篇文章程序;

3,tomcat需要更改以下几个地方:

[root@hnxz-vm-mysql-p01 apache-tomcat-7.0.67]# vim conf/context.xml

 在<Context>属性中增加 

     <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:10.207.238.61:11211"
                sticky="false"
                failoverNodes="n2"
                sessionBackupAsync="false"
                lockingMode="auto"
                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"  
                />

memcachedNodes 可以添加多个

暂时没有做到failover 在failover过程中报错 但memcached 就不会出现问题,但存在单点故障;

Jan 14, 2016 11:26:02 AM de.javakaffee.web.msm.LockingStrategy$OnAfterBackupSessionTask call
INFO: Could not store secondary backup of session 1694C94A2A6DEBC4F5D1ED172519184E-n1
java.lang.IllegalArgumentException: The given node id n1 is not part of this list [n2]
at de.javakaffee.web.msm.NodeIdList.getNextNodeId(NodeIdList.java:60)
at de.javakaffee.web.msm.MemcachedNodesManager.getNextAvailableNodeId(MemcachedNodesManager.java:351)
at de.javakaffee.web.msm.SuffixBasedNodeLocator.getNodeId(SuffixBasedNodeLocator.java:110)
at de.javakaffee.web.msm.SuffixBasedNodeLocator.getPrimary(SuffixBasedNodeLocator.java:97)
at net.spy.memcached.MemcachedConnection.addOperation(MemcachedConnection.java:1101)
at net.spy.memcached.MemcachedConnection.enqueueOperation(MemcachedConnection.java:1081)
at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:315)
at net.spy.memcached.MemcachedClient.add(MemcachedClient.java:812)
at de.javakaffee.web.msm.LockingStrategy$OnAfterBackupSessionTask.pingSessionBackup(LockingStrategy.java:520)
at de.javakaffee.web.msm.LockingStrategy$OnAfterBackupSessionTask.call(LockingStrategy.java:481)
at de.javakaffee.web.msm.LockingStrategy$OnAfterBackupSessionTask.call(LockingStrategy.java:435)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

4,然后启动tomcat测试是否可以session 共享;

5,apache nginx 负载均衡在写出,请参考以前文章;

你可能感兴趣的:(tomcat,memcached)