nginx+tomcat+redis完成session共享

nginx+tomcat+redis完成session共享

本文记录nginx+Redis+tomcat实现session共享的过程

nginx安装:http://blog.csdn.net/grhlove123/article/details/47834673

redis安装:http://blog.csdn.net/grhlove123/article/details/47783471

准备两个tomcat,修改相应的端口


名称IP端口tomcat版本JDK
tomcat1lcoalhost80807.0.401.7.0_25
tomcat2localhost80817.0.401.7.0_25

修改nginx.conf加上:

  1. upstream backend {  
  2.     server localhost:8080 max_fails=1 fail_timeout=10s;  
  3.     server localhost:8081 max_fails=1 fail_timeout=10s;  
  4. }  
    upstream backend {
        server localhost:8080 max_fails=1 fail_timeout=10s;
        server localhost:8081 max_fails=1 fail_timeout=10s;
    }
修改nginx.conf的location成
  1. location / {  
  2.     root   html;  
  3.     index  index.html index.htm;  
  4.     proxy_pass http://backend;  
  5.  }  
    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:

  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=“localhost”  
  20.    port=“6379”  
  21.    database=“0”  
  22.    maxInactiveInterval=“60” />  
  23. </Context>  
<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

  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>  
  18. sessionID:<%=session.getId()%>   
  19. <br>   
  20. SessionIP:<%=request.getServerName()%>   
  21. <br>   
  22. SessionPort:<%=request.getServerPort()%>   
  23. <%   
  24. //为了区分,第二个可以是222  
  25. out.println(“This is Tomcat Server 1111”);   
  26. %>    
<%@ 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://localhost/test/


刷新:

可以看到虽然Server从1111变为2222,但session的创建时间没有变化,这就完成了session共享。

jar包下载:http://download.csdn.net/detail/u010326711/9718119 (包含tomcat nginx redis jar jdk自行下载)
转载地址:http://blog.csdn.net/grhlove123/article/details/48047735

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值