標籤:
本文記錄nginx+redis+tomcat實現session共用的過程
nginx安裝:http://blog.csdn.net/grhlove123/article/details/47834673
redis安裝:http://blog.csdn.net/grhlove123/article/details/47783471
準備兩個tomcat,修改相應的連接埠 8080,9090
修改nginx.conf加上:
upstream backend {
server 10.10.49.23:8080 max_fails=1 fail_timeout=10s;
server 10.10.49.15:8081 max_fails=1 fail_timeout=10s;
}
修改nginx.conf的location成
location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
}
啟動nginx。
下載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:
<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" />
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="10.10.49.20"
port="6379"
database="0"
maxInactiveInterval="60" />
</Context>
在tomcat/webapps/test放一個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
通過訪問http://10.10.49.20/test/
重新整理:
可以看到雖然Server從1111變為2222,但session的建立時間沒有變化,這就完成了session共用。
centos 6.6 搭建nginx1.80+redis-3.0.2.tar + tomcat8做負載平衡