環境準備:實驗環境有限,都安裝在一臺機器上了
tomcat7 兩個
nginx1.8
redis3.2.8
準備兩個tomcat,修改相應的端口
名稱 | IP | 端口 | tomcat版本 | JDK |
tomcat-1 | 192.168.1.133 | 8081 | 7.0.69 | 1.7.0_79 |
tomcat-2 | 192.168.1.133 | 8082 | 7.0.69 | 1.7.0_79 |
nginx.conf配置:
下載tomcat-redis-session-manager相應的jar包,主要有四個:
修改兩個tomcat的conf/context.xml:
- <Context> - - <!-- Default set of monitored resources --> - <WatchedResource>WEB-INF/web.xml</WatchedResource> - - <!-- Uncomment this to disable session persistence across Tomcat restarts --> - <!-- - <Manager pathname="" /> - --> - - <!-- Uncomment this to enable Comet connection tacking (provides events - on session expiration as well as webapp lifecycle) --> - <!-- - <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> - --> - <!-- 添加你的redis信息(根據實際配置做相應修改) --> - <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> - <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" - host="localhost" - port="6379" - database="0" - maxInactiveInterval="60" /> - </Context>
在tomcat/webapps/test放一個index.jsp(index.jsp 做測試使用)
- <%@ page language="java" %> - <html> - <head><title>TomcatA</title></head> - <body> - - <table align="centre" border="1"> - <tr> - <td>Session ID</td> - <td><%= session.getId() %></td> - </tr> - <tr> - <td>Created on</td> - <td><%= session.getCreationTime() %></td> - </tr> - </table> - </body> - </html> - sessionID:<%=session.getId()%> - <br> - SessionIP:<%=request.getServerName()%> - <br> - SessionPort:<%=request.getServerPort()%> - <% - //爲了區分,第二個可以是222 - out.println("This is Tomcat Server 1111"); - %>
啓動tomcat,發現有異常:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve 類找不到
分別打開三個jar包,確實沒有這個類,解決可以參考:
http://blog.csdn.net/qinxcb/article/details/42041023
這裏說的就是lib下的包有問題,沒下載對,剛剛上面的包已經經過測試了;
通過訪問http://192.168.1.133/test/
刷新:
可以看到雖然Server從1111變爲2222,但session的創建時間沒有變化,這就完成了session共享。
最後感謝原作者,原文鏈接:http://blog.csdn.net/grhlove123/article/details/48047735