本文记录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
tomcat1 10.10.49.23 8080 7.0.40 1.7.0_25
tomcat2 10.10.49.15 8081 7.0.40 1.7.0_25

修改nginx.conf加上:

  1. upstream backend {
  2. server 10.10.49.23:8080 max_fails=1 fail_timeout=10s;
  3. server 10.10.49.15:8081 max_fails=1 fail_timeout=10s;
  4. }

修改nginx.conf的location成

  1. location / {
  2. root   html;
  3. index  index.html index.htm;
  4. proxy_pass http://backend;
  5. }

启动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. <!-- Default set of monitored resources -->
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  4. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  5. <!--
  6. <Manager pathname="" />
  7. -->
  8. <!-- Uncomment this to enable Comet connection tacking (provides events
  9. on session expiration as well as webapp lifecycle) -->
  10. <!--
  11. <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
  12. -->
  13. <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
  14. <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
  15. host="10.10.49.20"
  16. port="6379"
  17. database="0"
  18. maxInactiveInterval="60" />
  19. </Context>

在tomcat/webapps/test放一个index.jsp

  1. <%@ page language="java" %>
  2. <html>
  3. <head><title>TomcatA</title></head>
  4. <body>
  5. <table align="centre" border="1">
  6. <tr>
  7. <td>Session ID</td>
  8. <td><%= session.getId() %></td>
  9. </tr>
  10. <tr>
  11. <td>Created on</td>
  12. <td><%= session.getCreationTime() %></td>
  13. </tr>
  14. </table>
  15. </body>
  16. </html>
  17. sessionID:<%=session.getId()%>
  18. <br>
  19. SessionIP:<%=request.getServerName()%>
  20. <br>
  21. SessionPort:<%=request.getServerPort()%>
  22. <%
  23. //为了区分,第二个可以是222
  24. out.println("This is Tomcat Server 1111");
  25. %>

启动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共享。

 
 http://blog.csdn.net/grhlove123/article/details/48047735

1,安装redis并配置和启动, tomcat也做相就的下载,其他地方都有,可以在其他地方查阅。
2,  获取tomcat依赖包:
             Tomcat使用 从https://github.com/xetorthio/jedis/downloads下载jedis作为java的redis客户端,
              从https://github.com/jcoleman/tomcat-redis-session-manager/downloads下载tomcat的redis session manager插件
          从http://commons.apache.org/proper/commons-pool/download_pool.cgi下载apache的common pool2包,2.2,将这几个jar包拷贝到tomcat7的lib目录下
         包有: redis2.8、jedis.jar、common-pool2.2.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar
3 配置 tomcat
           在https://github.com/jcoleman/tomcat-redis-session-manager 这里面文章看到的配置为:
          <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
         <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost" <!-- optional: defaults to "localhost" -->
         port="6379" <!-- optional: defaults to "6379" -->
         database="0" <!-- optional: defaults to "0" -->
         maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
         sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional -->
         sentinelMaster="SentinelMasterName" <!-- optional -->
         sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
            而下载的包tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar或tomcat-redis-session-manager-1.2-tomcat-7.jar
 4 相关包的里面并没有类:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve
     所以从https://github.com/jcoleman/tomcat-redis-session-manager直接下载源码
     发现源码里面存在相应的类,同时源码(tomcat-redis-session-manager)依赖了tomcat其他的包:tomcat-juli.jar
     而tomcat默认是没有这些包的,从http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.57/bin/extras/ 下载tomcat-juli-adapters.jar和tomcat-juli.jar两个包,放在apache-tomcat-7.0.57\lib目录下,同时将tomcat-juli.jar放在apache-tomcat-7.0.57\bin目录下

同时将编译tomcat-redis-session-manager的源码,通过相应的依赖包common-pool2.2,jedis以及tomcat-juli.jar编译,

并打成自己的jar包,我已经上传在:

http://download.csdn.net/detail/qinxcb/8279761
   然后将这个依赖包放在apache-tomcat-7.0.57\lib目录下,删除网上下载的tomcat-redis-session-manager-1.2-tomcat-7.jar.

http://blog.csdn.net/qinxcb/article/details/42041023

nginx+tomcat+redis完成session共享的更多相关文章

  1. Nginx+tomcat+redis实现session共享

    Nginx+tomcat+redis实现session共享 1,安装nginx,使用yum -y install nginx 这是epel源中的,需要安装epel源. 2,配置nginx. 在ngin ...

  2. nginx+tomcat+redis完成session共享(转载)

    转载:http://blog.csdn.net/grhlove123/article/details/48047735 tomcat7下基于redis的session共享所需jar包: http:// ...

  3. nginx+tpmcat+redis实现session共享

    nginx+tpmcat+redis实现session共享 版本:nginx nginx-1.8.0.tar.gztomcat apache-tomcat-7.0.78.tar.gzredis  re ...

  4. Nginx+Tomcat集群+session共享

    Nginx+Tomcat集群+session共享 1)安装Nginx 2)配置多个Tomcat,在server.xml中修改端口(端口不出现冲突即可) 3)在nginx.conf文件中配置负载均衡池, ...

  5. Nginx+Tomcat+Memcache 实现session共享

    Nginx + Tomcat + Memcache 实现session共享 1. Nginx 部署 1.上传源码包到服务器,解压安装 下载地址:http://nginx.org/en/download ...

  6. 分布式Session共享(一):tomcat+redis实现session共享

    一.前言 本文主要测试redis实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port Tomcat ...

  7. 160513、nginx+tomcat集群+session共享(linux)

    第一步:linux中多个tomcat安装和jdk安装(略) 第二步:nginx安装,linux中安装nginx和windows上有点不同也容易出错,需要编译,这里做介绍 一.安装依赖 gcc open ...

  8. Nginx+IIS+Redis 处理Session共享问题 1

    最近遇到一个棘手的问题,微信公众平台的前端站点session老是丢失,我们是走的微信网页授权,授权后获取用户openid,丢失后没有openid后续的操作全白搭了,因为没了openid只能判断为客户不 ...

  9. Nginx+Tomcat+Memcached实现session共享

    实验环境: server1:nginx tomcat memcached server2:tomcat memcached Session是指一个终端用户与交互系统进行通信的时间间隔,通常指从注册进入 ...

随机推荐

  1. JavaScript之JS的执行环境和作用域

    一.执行环境是JavaScript中最为重要的一个概念.执行环境定义了变量或函数有权访问的其他数据,决定了他们各自的行为,每个执行环境都有一个与之关联的变量对象(variable object),环境 ...

  2. bootstrap2.3.2常用标签的使用

    <!DOCTYPE html> <html lang="zh_CN"> <head> <title>Bootstrap 101 Te ...

  3. MATLAB中求矩阵非零元的坐标

    MATLAB中求矩阵非零元的坐标: 方法1: index=find(a); [i,j]=ind2sub(size(a),index); disp([i,j]) 方法2: [i,j]=find(a> ...

  4. BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )

    二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...

  5. 《Pointers On C》读书笔记(第五章 操作符和表达式)

    1.C语言操作符优先级表 2.算术操作符中%(取模操作符)只适用于整型类型,其余几个操作符(+.-.*./)既适用于整型类型也适用于浮点类型.当/操作符的两个操作数都是整型时,它执行整除运算,其它情况 ...

  6. Delphi获取与设置系统时间格式,即GetLocaleInfo和SetLocaleInfo

    在Delphi中,特别是在写管理系统软件时,经常要用到 FormatDateTime 以将 TDateTime 格式的日期时间转换成字符串形式的值显示或保存起来,或者用 StrToDateTime将字 ...

  7. flume 日志采集工具

    Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...

  8. 10个必备的移动UI设计资源站

    http://www.uisdc.com/10-necessary-mobile-ui-design-resources# 交互设计中如何增加趣味性.提升愉悦http://www.uisdc.com/ ...

  9. SQL SERVER表不能修改表结构的处理方法

    SQL SERVER表提示不能修改表结构,这究竟是什么原因呢?下面就为您介绍处理该问题的方法,如果您在SQL SERVER表修改方面遇到过问题,不妨一看. 新装的SQL SERVER 2008,打开原 ...

  10. java课程设计(计算器)

    JAVA课程 设 计 报 告 1206401-18   瞿杰 一.设计时间 2013年6月 24日-----6月28日 二.设计地点 湖南城市学院实验楼计算机506机房 三.设计目的 1.巩固学习VB ...