nginx+tomcat+redis完成session共享

本文目的-解决负载均衡session失效问题。

所需文件版本
java-jdkjava-1.8.0_144暂未提供
负载均衡nginx-1.6.2.tar.gz
tomcatapache-tomcat-8.5.33.tar.gz
所需要的包
jedis-2.7.3.jar
commons-pool2-2.3.jar
tomcat-redis-session-manager-master-2.0.0.jar

1、安装nginx,redis

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

修改nginx.conf加上:

upstream backend {  
  server 127.0.0.1:8080 max_fails=1 fail_timeout=10s;  
  server 127.0.0.1:8081 max_fails=1 fail_timeout=10s;  
}  

修改nginx.conf的location成

location / {  
    root   html;  
    index  index.html index.htm;  
    proxy_pass http://backend;  
 }

启动nginx。

 

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

设置redis.windows.conf的密码****自行修改

requirepass ******

启动redis。

2、启动tomcat

下载tomcat-redis-session-manager相应的jar包,主要有三个:

jedis-2.7.3.jar

commons-pool2-2.3.jar

tomcat-redis-session-manager-master-2.0.0.jar

下载完成后拷贝到$TOMCAT_HOME/lib中

注意是tomcat的apache-tomcat-8.5.33\lib

修改两tomcat的context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
                     
    <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" /> 
    <Manager className="com.naritech.nicole.gump.RedisSessionManager"
    host="127.0.0.1"
    port="6379"
    database="1"
    password="*****"
    maxInactiveInterval="3600" />
</Context>d

<Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />

的路径在包tomcat-redis-session-manager-master-2.0.0.jar里。有的包是被修改过的,重新编译的。

 

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

名称IP端口tomcat版本JDK
tomcat1127.0.0.180808.5.331.8.0_144
tomcat2127.0.0.180818.5.331.8.0_144

这时的session就不能放对象了,可以转换Map<String,String>等使用

启动报错的话,请检查

tomcat-redis-session-manager-master-2.0.0.jar

是否在tomcat里。不是项目里切记。

在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");   
    %>    

通过访问http://127.0.0.1/test/

Session ID:E8BBB11EE37EDE0CE66C84C8D823A201

This is Tomcat Server 1111

刷新

Session ID:E8BBB11EE37EDE0CE66C84C8D823A201

This is Tomcat Server 2222

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

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值