本文章內容:
1. Nginx會話保持Tomcat
1.1 Session Sticky
1.1.1 URI綁定
1.1.2 Cookie綁定
1.2 Session Cluster
1.3 Session Server
1.3.1 使用javolution序列化工具
1.3.2 使用kryo序列化工具
2. Apache會話保持Tomcat
2.1 Session Sticky
2.2 Session Cluster
2.3 Session Server
環境:
參考"負載均衡Tomcat"那篇建構出環境 (安裝、設定好Tomcat-1、Tomcat-2和Proxy主機)。
1. Nginx會話保持Tomcat
1.1 Session Sticky
1.1.1 URI綁定
1.1.1.1 設定nginx.conf
[root@proxy ~]# vim /etc/nginx/nginx.conf
33 upstream tcsrvs {
34 hash $request_uri consistent; #加上這一行,consistent表示使用一致性hash算法
35 server 192.168.169.131:8080;
36 server 192.168.169.132:8080;
37 }
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
1.1.1.2 重載Nginx服務
[root@proxy ~]# nginx -s reload
1.1.1.3 多次訪問"http"//www.ilinux.io/test"顯示都是同一個畫面,Session ID與時間戳也都是同一個
1.1.2 Cookie綁定
將應用轉換成cookie。
1.1.2.1 設定nginx.conf
[root@proxy ~]# vim /etc/nginx/nginx.conf
33 upstream tcsrvs {
34 hash $cookie_name consistent; #加上這一行,consistent表示使用一致性hash算法
35 server 192.168.169.131:8080;
36 server 192.168.169.132:8080;
37 }
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
1.1.2.2 重載Nginx服務
[root@proxy ~]# nginx -s reload
1.1.2.3 多次訪問"http://www.ilinux.io/test"顯示都是同一個畫面,Session ID與時間戳也都是同一個
1.2 Session Cluster
1.2.1 設定tomcat-1、tomcat-2的/etc/tomcat/server.xml,輸入以下<Cluster></Cluster>的內容在<Host></Host>組件中
tomcat-1以及tomcat-2# vim /etc/tomcat/server.xml
125 <Host name="localhost" appBase="webapps"
126 unpackWARs="true" autoDeploy="true">
127
128 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
129 channelSendOptions="8">
130
131 <Manager className="org.apache.catalina.ha.session.DeltaManager"
132 expireSessionsOnShutdown="false"
133 notifyListenersOnReplication="true"/>
134
135 <Channel className="org.apache.catalina.tribes.group.GroupChannel">
136 <Membership className="org.apache.catalina.tribes.membership.McastService"
137 address="228.0.0.4" #集群中的節點的多播地址必須一樣
138 port="45564"
139 frequency="500"
140 dropTime="3000"/>
141 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
142 address="auto"
143 port="4000"
144 autoBind="100"
145 selectorTimeout="5000"
146 maxThreads="6"/>
147
148 <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
149 <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
150 </Sender>
151 <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
152 <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
153 </Channel>
154
155 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
156 filter=""/>
157 <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
158
159 <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
160 tempDir="/tmp/war-temp/"
161 deployDir="/tmp/war-deploy/"
162 watchDir="/tmp/war-listen/"
163 watchEnabled="false"/>
164
165 <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
166 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
167 </Cluster>
...
183 </Host>
1.2.2 為tomcat-1、tomcat-2的應用提供web.xml並設定
tomcat-1以及tomcat-2# cp /etc/tomcat/web.xml /var/lib/tomcat/webapps/test/WEB-INF/
18 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
103 <distributable/> #這一行寫在<web-app></web-app>內
4677 </web-app>
1.2.3 重啟tomcat-1、tomcat-2的Tomcat服務
tomcat-1以及tomcat-2# systemctl restart tomcat.service #可以配合追蹤/var/log/tomcat/catalina.<日期>.log看有沒有錯誤
1.2.4 訪問多次"http://www.ilinux.io/test",可以看到不管訪問到tomcat-1還是tomcat-2,其Session ID和Create on都是一樣的
1.3 Session Server
1. 在tomcat-1、tomcat-2主機上裝上Memcached
tomcat-1以及tomcat-2# yum install memcached -y
2. tomcat-1、tomcat-2主機啟動Memcached服務並設定開機自啟動
tomcat-1以及tomcat-2# systemctl start memcached.service
tomcat-1以及tomcat-2# systemctl enable memcached.service
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.
[root@memcached-1 ~]# ss -tnul
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:11211 *:*
udp UNCONN 0 0 [::]:11211 [::]:*
tcp LISTEN 0 128 *:11211 *:*
tcp LISTEN 0 128 [::]:11211 [::]:*
3. 訪問"https"//github.com",搜索memcached session
4. 點"magro/memcached-session-manager"
5. 在Installation and Configuration點"SetupAndConfiguration wiki page"鏈結
6. 點"Add msm jars to tomcat"鏈結
7. 點"memcached-session-manager-${version}.jar"鏈結
8. 顯示要求https,所以在網址最前面加上"https://"
9. 點"2.3.2/"
10. 滑鼠右鍵"memcached-session-manager-2.3.2.jar"複製鏈結網址
11. 使用wget下載memcached-session-manager-2.3.2.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-manager-2.3.2.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-manager-2.3.2.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar
12. 回到1.3.6點"Add msm jars to tomcat"鏈結 → 點"memcached-session-manager-tc7-${version}.jar"
13. 顯示要求https,所以在網址最前面加上"https://"
14. 點"2.3.2/"
15. 滑鼠右鍵"memcached-session-manager-tc7-2.3.2.jar"複製鏈結網址
16. 使用wget下載memcached-session-manager-tc7-2.3.2.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/2.3.2/memcached-session-manager-tc7-2.3.2.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/2.3.2/memcached-session-manager-tc7-2.3.2.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar
17. 回到1.3.6點"Add msm jars to tomcat"鏈結 → 因為我們是使用memcached當儲存,所以還要點"spymemcached-${version}.jar"鏈結下載
18. 顯示要求https,所以在網址最前面加上"https://"
19. 點"2.9.1/"
20. 滑鼠右鍵"spymemcached-2.9.1.jar"複製鏈結網址
21. 使用wget下載spymemcached-2.9.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/net/spy/spymemcached/2.9.1/spymemcached-2.9.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar spymemcached-2.9.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/net/spy/spymemcached/2.9.1/spymemcached-2.9.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar spymemcached-2.9.1.jar
下面下載序列化工具:
注意:下載的版本很重要,不相容的啟動tomcat服務會有問題。
1.3.1 使用javolution序列化工具
1. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"msm-javolution-serializer"鏈結
2. 顯示要求https,所以在網址最前面加上"https://"
3. 點"2.1.1/"
4. 滑鼠右鍵"msm-javolution-serializer-2.1.1.jar"複製鏈結網址
5. 使用wget下載msm-javolution-serializer-2.1.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-javolution-serializer-2.1.1.jar spymemcached-2.9.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-javolution-serializer-2.1.1.jar spymemcached-2.9.1.jar
6. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"javolution-5.4.3.1"鏈結
7. 點"javolution-5.4.3.1.jar"
8. 滑鼠右鍵"Download"複製鏈結網址
9. 使用wget下載javolution-5.4.3.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://github.com/chenxofhit/memcached-session-manager/raw/master/maven/javolution/javolution/5.4.3.1/javolution-5.4.3.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-javolution-serializer-2.1.1.jar spymemcached-2.9.1.jar javolution-5.4.3.1.jar
[root@tomcat-2 ~]# wget https://github.com/chenxofhit/memcached-session-manager/raw/master/maven/javolution/javolution/5.4.3.1/javolution-5.4.3.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-javolution-serializer-2.1.1.jar spymemcached-2.9.1.jar javolution-5.4.3.1.jar
10. tomcat-1、tomcat-2將下載的jar文件放到Tomcat自己的類文件存放路徑,也就是/usr/share/java/tomcat目錄下
[root@tomcat-1 ~]# mv *.jar /usr/share/java/tomcat
[root@tomcat-2 ~]# mv *.jar /usr/share/java/tomcat
11. 回到上面第6步驟,點"Configure memcached-session-manager as Manager"鏈結
12. 我們使用sticky session
13. tomcat-1、tomcat-2主機設定/etc/tomcat/server.xml,在<Host></Host>裡增加以下幾行
tomcat-1以及tomcat-2# vim /etc/tomcat/server.xml
128 <Context path="/test" docBase="test" reloadable="true">
129 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" #使用的類
130 memcachedNodes="m1:192.168.169.131:11211,m2:192.168.169.131:11211" #m1、m2只是標識,不解析所以填IP
131 failoverNodes="m2" #表示m1出現故障時m2上線
132 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
133 transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" #這裡要改成使用javolution序列化工具的轉碼工廠類
134 />
135 </Context>
14. tomcat-1、tomcat-2主機重新啟動Tomcat服務
tomcat-1以及tomcat-2# # systemctl restart tomcat.service
tomcat-1以及tomcat-2# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 [::]:8009 [::]:*
LISTEN 0 100 [::]:8080 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 [::]:*
15. 訪問多次"http://192.168.0.22/test",可以看到不管訪問到tomcat-1還是tomcat-2,其Session ID、Created on都是一樣的
16. 把m1的服務給關了,模擬Memcached主機故障
[root@tomcat-1 ~]# systemctl stop memcached.service
17. 再訪問多次"http://192.168.0.22/test",依然可以看到不管訪問到tomcat-1還是tomcat-2,其Session ID、Created on都是一樣的,會話不會丟失
1.3.2 使用kryo序列化工具
1. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"msm-kryo-serializer "鏈結
2. 顯示要求https,所以在網址最前面加上https://
3. 點"2.3.2/"
4. 滑鼠右鍵" msm-kryo-serializer-2.3.2.jar"複製鏈結網址
5. 使用wget下載msm-kryo-serializer-2.3.2.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.2/msm-kryo-serializer-2.3.2.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.2/msm-kryo-serializer-2.3.2.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar
6. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"kryo-serializers-0.34+"鏈結
7. 顯示要求https,所以在網址最前面加上https://
8. 點"0.45/"
9. 滑鼠右鍵"kryo-serializers-0.45.jar"複製鏈結網址
10. 使用wget下載kryo-serializers-0.45.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.45/kryo-serializers-0.45.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.45/kryo-serializers-0.45.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar
11. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"kryo-3.x"鏈結
12. 顯示要求https,所以在網址最前面加上https://
13. 點"4.0.2/"
14. 滑鼠右鍵"kryo-4.0.2.jar"複製鏈結網址
15. 使用wget下載kryo-4.0.2.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.2/kryo-4.0.2.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.2/kryo-4.0.2.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar
16. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"minlog "鏈結
17. 顯示要求https,所以在網址最前面加上https://
18. 點"1.3.1/"
19. 滑鼠右鍵"minlog-1.3.1.jar"複製鏈結網址
20. 使用wget下載minlog-1.3.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.1/minlog-1.3.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.1/minlog-1.3.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar
21. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"reflectasm"鏈結
22. 顯示要求https,所以在網址最前面加上https://
23. 點"1.11.9/"
24. 滑鼠右鍵"reflectasm-1.11.9.jar"複製鏈結網址
25. 使用wget下載reflectasm-1.11.9.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.9/reflectasm-1.11.9.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.9/reflectasm-1.11.9.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar
26. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"asm-5.x"鏈結
27. 顯示要求https,所以在網址最前面加上https://
28. 點"9.1/"
29. 滑鼠右鍵"asm-9.1.jar"複製鏈結網址
30. 使用wget下載asm-9.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar asm-9.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar asm-9.1.jar
31. 回到上面第6步驟,點"Add custom serializers to your webapp (optional)"鏈結 → 點"objenesis-2.x"鏈結
32. 顯示要求https,所以在網址最前面加上https://
33. 點"3.1/"
34. 滑鼠右鍵"objenesis-3.1.jar"複製鏈結網址
35. 使用wget下載asm-9.1.jar到tomcat-1、tomcat-2主機
[root@tomcat-1 ~]# wget https://repo1.maven.org/maven2/org/objenesis/objenesis/3.1/objenesis-3.1.jar
[root@tomcat-1 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar asm-9.1.jar objenesis-3.1.jar
[root@tomcat-2 ~]# wget https://repo1.maven.org/maven2/org/objenesis/objenesis/3.1/objenesis-3.1.jar
[root@tomcat-2 ~]# ls
anaconda-ks.cfg memcached-session-manager-2.3.2.jar memcached-session-manager-tc7-2.3.2.jar msm-kryo-serializer-2.3.2.jar spymemcached-2.9.1.jar kryo-serializers-0.45.jar kryo-4.0.2.jar minlog-1.3.1.jar reflectasm-1.11.9.jar asm-9.1.jar objenesis-3.1.jar
36. tomcat-1、tomcat-2將下載的jar文件放到Tomcat自己的類文件存放路徑,也就是/usr/share/java/tomcat目錄下
提示:
a. 使用rpm -ql tomcat-lib可以得知Tomcat自己的類文件存放路徑。
b. tomcat如果是使用官方的二進制包,則路徑在二進制包下的lib目錄。
[root@tomcat-1 ~]# mv *.jar /usr/share/java/tomcat
[root@tomcat-2 ~]# mv *.jar /usr/share/java/tomcat
37. 回到上面第6步驟,點"Configure memcached-session-manager as Manager"鏈結
38. 我們使用sticky session
39. tomcat-1、tomcat-2主機設定/etc/tomcat/server.xml,在<Host></Host>裡增加以下幾行
tomcat-1以及tomcat-2# vim /etc/tomcat/server.xml
128 <Context path="/test" docBase="test" reloadable="true">
129 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" #使用的類
130 memcachedNodes="m1:192.168.169.131:11211,m2:192.168.169.131:11211" #m1、m2只是標識,不解析所以填IP
131 failoverNodes="m2" #表示m1出現故障時m2上線
132 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
133 transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" #這裡要改成使用javolution序列化工具的轉碼工廠類
134 />
135 </Context>
40. tomcat-1、tomcat-2主機重新啟動Tomcat服務
tomcat-1以及tomcat-2# # systemctl restart tomcat.service
tomcat-1以及tomcat-2# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 [::]:8009 [::]:*
LISTEN 0 100 [::]:8080 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 [::]:*
41. 訪問多次"http://192.168.0.22/test",可以看到不管訪問到tomcat-1還是tomcat-2,其Session ID、Created on都是一樣的
42. 把m1的服務給關了,模擬Memcached主機故障
[root@tomcat-1 ~]# systemctl stop memcached.service
43. 再訪問多次"http://192.168.0.22/test",依然可以看到不管訪問到tomcat-1還是tomcat-2,其Session ID、Created on都是一樣的,會話不會丟失
2. Apache會話保持Tomcat
2.1 Session Sticky
使用基於cookie的會話黏性。
2.1.1 設定www-ilinux-io_http_tomcat.conf
[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_http_tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://tcsrvs>
BalancerMember http://192.168.169.131:8080 route=Tomcat-1
BalancerMember http://192.168.169.132:8080 route=Tomcat-2
ProxySet lbmethod=bybusyness
ProxySet stickysession=ROUTEID #使用會話黏性
</Proxy>
<VirtualHost *:80>
ServerName www.ilinux.io
ProxyRequests off
ProxyPreserveHost on
ProxyVia on
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@proxy ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5098:67ae:9678:b052. Set the 'ServerName' directive globally to suppress this message
Syntax OK
2.1.2 重啟Apache服務
[root@proxy ~]# systemctl restart httpd.service
2.1.3 訪問多次"http://www.ilinux.io/test"
2.1.3.1 使用瀏覽器訪問,顯示都是同一個畫面,Session ID與時間戳也都是同一個
2.1.3.2 使用curl訪問,會話黏性失效了嗎?不是,因為curl是不帶cookie的
[root@client ~]# for i in {1..4};do curl -s www.ilinux.io/test/ | grep -i tomcat*;done
<head><title>tomcat-2</title></head>
<h1><font color="green">tomcat-2.lab.com</font></h1>
<head><title>tomcat-1</title></head>
<h1><font color="red">tomcat-1.lab.com</font></h1>
<head><title>tomcat-2</title></head>
<h1><font color="green">tomcat-2.lab.com</font></h1>
<head><title>tomcat-1</title></head>
<h1><font color="red">tomcat-1.lab.com</font></h1>
2.1.4 使用瀏覽器訪問時打開開發者檢測畫面,可以看到Cookie最後帶了ROUTEID
2.2 Session Cluster
同1.2,既然是負載均衡調度,那麼很顯然我們使用哪一種調度器對用戶來講其實都沒有什麼影響,所以像1.2用的是Nginx調度的,現在把換成Httpd調度應該依然沒有任何問題。
2.3 Session Server
同1.3,既然是負載均衡調度,那麼很顯然我們使用哪一種調度器對用戶來講其實都沒有什麼影響,所以像1.3用的是Nginx調度的,現在把換成Httpd調度應該依然沒有任何問題。