tomcat 之 session服务器 (memcache)
#: 在tomcat各节点安装memcached [root@node1 ~]# yum install memcached -y #: 下载tomcat所需的jar包(此处在视频中找软件) [root@node1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-manager-2.3.2.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/2.3.2/memcached-session-manager-tc8-2.3.2.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.3/spymemcached-2.12.3.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.2/msm-kryo-serializer-2.3.2.jar
[root@node1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.45/kryo-serializers-0.45.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/com/esotericsoftware/kryo/3.0.3/kryo-3.0.3.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.1/minlog-1.3.1.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/org/ow2/asm/asm/5.2/asm-5.2.jar [root@node1 ~]# wget http://repo1.maven.org/maven2/org/objenesis/objenesis/2.6/objenesis-2.6.jar #:将jar包拷贝到lib目录下 [root@node1 ~]# cp -r *.jar /usr/local/tomcat/lib/ #:修改配置 [root@node1 ~]# vi /usr/local/tomcat/conf/context.xml <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.5.101:11211,n2:192.168.5.102:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/> [root@node2 ~]# vim /usr/local/tomcat/conf/context.xml <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.5.101:11211,n2:192.168.5.102:11211" failoverNodes="n2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
注意:这个Mem是交叉存储,1存到2上,2存到1上。 当2的mem挂掉后,1的才会存到1上。每个tomcat本身也有session,当自己没有的时候才会在mem上查找
#:将所有jar包给节点2拷贝一份
[root@node1 ~]# scp -r /usr/local/tomcat/lib/*.jar 192.168.5.102:/usr/local/tomcat/lib/
#:启动
#:测试
non-sticky模式 原理 从msm 1.4.0之后开始支持non-sticky模式。 Tomcat session为中转Session,n1为主session,n2为备session。产生的新的Session会发送给主、备memcached,并清除本地Session。 n1下线,n2转正。n1再次上线,n2依然是主Session存储节点。 #配置tomcat [root@node1 tomcat]# vim conf/context.xml <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.5.101:11211,n2:192.168.5.102:11211" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> #:测试
redis配置(注意此处的lib库中之安装redis相关的就可以,不用安装tomcat相关的) #:下载redis [root@node1 tomcat]# yum install redis -y #:修改Redis监听地址 [root@node1 tomcat]# vim /etc/redis.conf bind 0.0.0.0 #:修改配置文件(后端所有tomcat指向同一个Redis) [root@node1 tomcat]# vim conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="redis://192.168.5.101:6379"
sticky="false"
sessionBackupAsync="false"
lockingMode="uriPattern:/path1|/path2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>