memcached服務器兩臺:192.168.223.136,192.168.223.137css
tomcat多實例:192.168.233.146:8081,192.168.223.146:8082前端
for tomcat configure(這裏使用的tomcat8的版本)java
參考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfigurationgit
Add memcached-session-manager jars to tomcatgithub
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.1.1/memcached-session-manager-2.1.1.jarweb
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/2.1.1/memcached-session-manager-tc8-2.1.1.jarredis
wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jarapache
下載須要的memcached的三個jar文件緩存
將這三個jar文件複製到$CATALINA_HOME/lib/目錄下tomcat
因爲採用的多實例
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保持:
一、Example for sticky sessions + kryo(須要的包
)
<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>
二、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