本文记录nginx+redis+tomcat实现session共享的过程
nginx安装:http://www.aiuxian.com/article/p-2617914.html
redis安装:http://www.aiuxian.com/article/p-2617912.html
准备两个tomcat,修改相应的端口
名称 | IP | 端口 | tomcat版本 | JDK |
tomcat1 | 10.10.49.23 | 8080 | 7.0.40 | 1.7.0_25 |
tomcat2 | 10.10.49.15 | 8081 | 7.0.40 | 1.7.0_25 |
1 | 1 |
修改nginx.conf加上:
1
2
3
4
5 1upstream backend {
2 server 10.10.49.23:8080 max_fails=1 fail_timeout=10s;
3 server 10.10.49.15:8081 max_fails=1 fail_timeout=10s;
4 }
5
1
2 1修改nginx.conf的location成
2
1
2
3
4
5
6 1location / {
2 root html;
3 index index.html index.htm;
4 proxy_pass http://backend;
5 }
6
1
2 1 启动nginx。
2
下载tomcat-redis-session-manager相应的jar包,主要有三个:
wget https://github.com/downloads/jcoleman/tomcat-redis-session-manager/tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
wget http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
wget http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar
下载完成后拷贝到$TOMCAT_HOME/lib中
修改两tomcat的context.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1<Context>
2
3 <!-- Default set of monitored resources -->
4 <WatchedResource>WEB-INF/web.xml</WatchedResource>
5
6 <!-- Uncomment this to disable session persistence across Tomcat restarts -->
7 <!--
8 <Manager pathname="" />
9 -->
10
11 <!-- Uncomment this to enable Comet connection tacking (provides events
12 on session expiration as well as webapp lifecycle) -->
13 <!--
14 <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
15 -->
16
17 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
18 <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
19 host="10.10.49.20"
20 port="6379"
21 database="0"
22 maxInactiveInterval="60" />
23</Context>
24
1
2 1 在tomcat/webapps/test放一个index.jsp
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 1<%@ page language="java" %>
2<html>
3 <head><title>TomcatA</title></head>
4 <body>
5
6 <table align="centre" border="1">
7 <tr>
8 <td>Session ID</td>
9 <td><%= session.getId() %></td>
10 </tr>
11 <tr>
12 <td>Created on</td>
13 <td><%= session.getCreationTime() %></td>
14 </tr>
15 </table>
16 </body>
17</html>
18sessionID:<%=session.getId()%>
19<br>
20SessionIP:<%=request.getServerName()%>
21<br>
22SessionPort:<%=request.getServerPort()%>
23<%
24//为了区分,第二个可以是222
25out.println("This is Tomcat Server 1111");
26%>
27
启动tomcat,发现有异常:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve 类找不到
分别打开三个jar包,确实没有这个类,解决可以参考:
http://www.aiuxian.com/article/p-2001826.html
http://www.aiuxian.com/article/p-2001826.html
刷新:
可以看到虽然Server从1111变为2222,但session的创建时间没有变化,这就完成了session共享。
版权声明:本文为博主原创文章,未经博主允许不得转载。