一:環境介紹:
OS:CentOS7.3
Ngninx:1.10.2
Tomcat:7
注:便于實驗,此處關閉全部服務器的防火墻,selinux(iptables -F 于 setenforce 0)
二:會話保持的重要性
在生成環境中,我們的Tomcat服務器肯定要做冗余或者高可用,如果沒有做會話保持,那么用戶訪問頁面時只要狀態丟失,那么是會造成cookie丟失這種情況的,舉個最簡單的例子:結合上邊的圖大家可以想象下,你的Tomcat服務器做了冗余,但是沒有做會話保持, 用戶訪問你的網站時登錄了自己的賬號(此時訪問的Tomcat1),而Tomcat1服務器是會給用戶生成cookie來記錄賬戶信息。此時如果你長時間未操作,或者刷新由于Nginx調度用戶會訪問到Tomcat2上,而用戶瀏覽器里保存的是Tomcat1的cookie,所以會重新請求!接下來可以想象了,用戶需要進場去輸入自己的賬號信息,這樣的用戶體驗是非常差的!而這里,我會演示,怎么做Tomcat集群的會話保持。
三:操作步驟
在Tomcat1服務器上
#安裝Tomcat [root@tomcat1] yum install -y tomcat-webapps tomcat-admin-webapps tomcat #安裝Tomcat測試應用 [root@tomcat1] mkdir -pv /usr/share/tomcat/webapps/test/{libclasses,lib,WEB-INF} [root@tomcat1]nano /usr/share/tomcat/webapps/test/index.jsp #貼入以下測試代碼(請勿用vim) <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>#加入會話保持配置信息 [root@tomcat1]vim /etc/tomcat/server.xml #在Engine配置段中貼入以下代碼 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.32" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.37.138" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> #然后修改Engine 添加 jvmRoute="TcA" <Engine name="Catalina" defaultHost="localhost" jvmRoute="TcA" > #啟動Tomcat [root@tomcat1]systemctl start tomcat [root@tomcat1]ss -tunl #稍等幾秒查看8080和45564端口是否啟用在Tomcat2服務器上
#安裝Tomcat [root@tomcat1] yum install -y tomcat-webapps tomcat-admin-webapps tomcat #安裝Tomcat測試應用 [root@tomcat1] mkdir -pv /usr/share/tomcat/webapps/test/{libclasses,lib,WEB-INF} [root@tomcat1]nano /usr/share/tomcat/webapps/test/index.jsp #貼入以下測試代碼(請勿用vim) <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>#加入會話保持配置信息 [root@tomcat1]vim /etc/tomcat/server.xml #在Engine配置段中貼入以下代碼 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.32" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.37.129" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> #然后修改Engine 添加 jvmRoute="TcA" <Engine name="Catalina" defaultHost="localhost" jvmRoute="TcB" > #啟動Tomcat [root@tomcat1]systemctl start tomcat [root@tomcat1]ss -tunl #稍等幾秒查看8080和45564端口是否啟用在Nginx服務器上
[root@tomcat1]yum install -y nginx [root@tomcat1]vim /etc/nginx/nginx.conf #在http配置段中添加 upstream tomsrv { server 192.168.37.138:8080; server 192.168.37.129:8080; } #在server配置段的默認根location中 location / { proxy_pass http://tomsrv; } [root@tomcat1]systemctl start nginx四:測試訪問
無論你訪問到哪臺服務器,重新訪問session是被記錄的,不需要重新請求。 如果各位有余力還可以把tomcat服務器中的server.xml文件中的<cluster>配置段刪除,再測試訪問,你會發現每次請求的session都不一樣。
五:實驗中遇到的問題
tomcat的8080 和45564端口起不來,首先確保配置文件正確,然后確保你的網卡支持MULTICAST,其他問題看日志解決。
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.37.138 netmask 255.255.255.0 broadcast 192.168.37.255 inet6 fe80::6189:fb1b:4a6e:af04 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ea:d6:e8 txqueuelen 1000 (Ethernet) RX packets 63440 bytes 69237595 (66.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 20513 bytes 2979675 (2.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0原創文章,作者:cnc,如若轉載,請注明出處:http://www.www58058.com/76852