首先安裝niginx,跟redishtml
而後準備兩個tomcat,修改他們的端口號java
修改nginx.conf加上:nginx
[html] web
upstream backend { redis
server 10.10.49.23:8080 max_fails=1 fail_timeout=10s; apache
server 10.10.49.15:8081 max_fails=1 fail_timeout=10s; tomcat
} session
修改nginx.conf的location成app
location / { webapp
root html;
index index.html index.htm;
proxy_pass http://backend;
}
下載tomcat-redis-session-manager相應的jar包,主要有三個:
tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar,jedis-2.5.2.jar,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" %>
開啓niginx服務
經過訪問http://端口號/test/
刷新
能夠看到雖然Server從1111變爲2222,但session的建立時間沒有變化,這就完成了session共享。