首页 > 技术文章 > Tomcat集群基于ip_hash实现Session

xiaozhebao 2020-03-30 15:35 原文

环境:

IP 角色 软件
192.168.1.131 proxy-server nginx
192.168.1.124:8080 Tomcat1 tomcat
192.168.1.124:8081 Tomcat2 tomcat
192.168.1.124:8082 Tomcat3 tomcat
192.168.1.124:8083 Tomcat4 tomcat

 

 

 

 

 

 

 

 

 

简单实现基于ip hash 实现会话保持

 

 1 [root@gz01-nginx-proxy-master ~]# vim /usr/local/nginx/conf/nginx.conf
 2 http {
 3     include       mime.types;
 4     default_type  application/octet-stream;
 5     upstream tomcatlist {
 6         ip_hash;
 7         server 192.168.1.124:8080 weight=1;
 8         server 192.168.1.124:8081 weight=2;
 9         server 192.168.1.124:8082 weight=3;
10         server 192.168.1.124:8083 weight=4;
11         }
12  location / {
13 
14         proxy_pass http://tomcatlist;
15 
16         }
17 location ~* \.(jsp|do)$ {
18             proxy_pass http://tomcatlist;
19             proxy_set_header Host $host;
20         }
21 [root@gz01-nginx-proxy-master ~]# /usr/local/nginx/sbin/nginx -t
22 nginx: the configuration file /usr/local/nginx-1.8.1/conf/nginx.conf syntax is ok
23 nginx: configuration file /usr/local/nginx-1.8.1/conf/nginx.conf test is successful
24 [root@gz01-nginx-proxy-master ~]# /usr/local/nginx/sbin/nginx -s reload

站点建立测试页面

 1 192.168.1.124:8080
 2 <%@ page language="java" %>
 3 <%@ page import="java.util.*" %>
 4 <html>
 5 <head>
 6 <title>>Tomcat 192.168.1.124:8080 </title>
 7         </head>
 8         <body>
 9 
10 <h1><font color="blue">Tomcat 192.168.1.124:8080 </h1>
11 <table align="centre" border="1">
12 <tr>
13 <td>Session ID</td>
14 <% session.setAttribute("abc","abc"); %>
15 <td><%= session.getId() %></td>
16 </tr>
17 <tr>
18 <td>Created on</td>
19 <td><%= session.getCreationTime() %></td>
20 </tr>
21 </table>
22         </body>
23 </html>
24 192.168.1.124:8081
25 <%@ page language="java" %>
26 <%@ page import="java.util.*" %>
27 <html>
28 <head>
29 <title>>Tomcat 192.168.1.124:8081 </title>
30         </head>
31         <body>
32 
33 <h1><font color="blue">Tomcat 192.168.1.124:8081 </h1>
34 <table align="centre" border="1">
35 <tr>
36 <td>Session ID</td>
37 <% session.setAttribute("abc","abc"); %>
38 <td><%= session.getId() %></td>
39 </tr>
40 <tr>
41 <td>Created on</td>
42 <td><%= session.getCreationTime() %></td>
43 </tr>
44 </table>
45         </body>
46 </html>
47 192.168.1.124:8082
48 <%@ page language="java" %>
49 <%@ page import="java.util.*" %>
50 <html>
51 <head>
52 <title>>Tomcat 192.168.1.124:8082 </title>
53         </head>
54         <body>
55 
56 <h1><font color="blue">Tomcat 192.168.1.124:8082 </h1>
57 <table align="centre" border="1">
58 <tr>
59 <td>Session ID</td>
60 <% session.setAttribute("abc","abc"); %>
61 <td><%= session.getId() %></td>
62 </tr>
63 <tr>
64 <td>Created on</td>
65 <td><%= session.getCreationTime() %></td>
66 </tr>
67 </table>
68         </body>
69 </html>
70 192.168.1.124:8083
71 <%@ page language="java" %>
72 <%@ page import="java.util.*" %>
73 <html>
74 <head>
75 <title>>Tomcat 192.168.1.124:8083 </title>
76         </head>
77         <body>
78 
79 <h1><font color="blue">Tomcat 192.168.1.124:8083 </h1>
80 <table align="centre" border="1">
81 <tr>
82 <td>Session ID</td>
83 <% session.setAttribute("abc","abc"); %>
84 <td><%= session.getId() %></td>
85 </tr>
86 <tr>
87 <td>Created on</td>
88 <td><%= session.getCreationTime() %></td>
89 </tr>
90 </table>
91         </body>
92 </html>

浏览器测试http://192.168.1.131

 

 

访问页面,一直匹配到192.168.1.124:8082的server 上,没有轮询;会话保持成功.

现在测试把192.168.124:8082关掉,在访问。

1 [root@gz01-tomcat-node01 ~]# /usr/local/tomcat/bbs.fox.com/bbs.tomcat stop
2 Using CATALINA_BASE:   /usr/local/tomcat/bbs.fox.com
3 Using CATALINA_HOME:   /usr/local/tomcat
4 Using CATALINA_TMPDIR: /usr/local/tomcat/bbs.fox.com/temp
5 Using JRE_HOME:        /usr/local/jdk
6 Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

 

 虽然有负载均衡,会访问到下一台,但会话已经丢失;基于访问ip的hash策略,即当前用户的请求都集中定位到一台服务器中,这样单台服务器保存了用户的session登录信息,如果宕机,则等同于单点部署,会丢失,会话不复制。

 

推荐阅读