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 |
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加上:
- 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共享。
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共享的更多相关文章
- Nginx+tomcat+redis实现session共享
Nginx+tomcat+redis实现session共享 1,安装nginx,使用yum -y install nginx 这是epel源中的,需要安装epel源. 2,配置nginx. 在ngin ...
- nginx+tomcat+redis完成session共享(转载)
转载:http://blog.csdn.net/grhlove123/article/details/48047735 tomcat7下基于redis的session共享所需jar包: http:// ...
- nginx+tpmcat+redis实现session共享
nginx+tpmcat+redis实现session共享 版本:nginx nginx-1.8.0.tar.gztomcat apache-tomcat-7.0.78.tar.gzredis re ...
- Nginx+Tomcat集群+session共享
Nginx+Tomcat集群+session共享 1)安装Nginx 2)配置多个Tomcat,在server.xml中修改端口(端口不出现冲突即可) 3)在nginx.conf文件中配置负载均衡池, ...
- Nginx+Tomcat+Memcache 实现session共享
Nginx + Tomcat + Memcache 实现session共享 1. Nginx 部署 1.上传源码包到服务器,解压安装 下载地址:http://nginx.org/en/download ...
- 分布式Session共享(一):tomcat+redis实现session共享
一.前言 本文主要测试redis实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port Tomcat ...
- 160513、nginx+tomcat集群+session共享(linux)
第一步:linux中多个tomcat安装和jdk安装(略) 第二步:nginx安装,linux中安装nginx和windows上有点不同也容易出错,需要编译,这里做介绍 一.安装依赖 gcc open ...
- Nginx+IIS+Redis 处理Session共享问题 1
最近遇到一个棘手的问题,微信公众平台的前端站点session老是丢失,我们是走的微信网页授权,授权后获取用户openid,丢失后没有openid后续的操作全白搭了,因为没了openid只能判断为客户不 ...
- Nginx+Tomcat+Memcached实现session共享
实验环境: server1:nginx tomcat memcached server2:tomcat memcached Session是指一个终端用户与交互系统进行通信的时间间隔,通常指从注册进入 ...
随机推荐
- Javascript 中创建自定义对象的方法(设计模式)
Javascript 中创建对象,可以有很多种方法. Object构造函数/对象字面量: 抛开设计模式不谈,使用最基本的方法,就是先调用Object构造函数创建一个对象,然后给对象添加属性. var ...
- jquery $.post 返回json数据
$(function () { $("#prompt").hide(); $("#searchIpt").keyup(function () { var key ...
- Building Workspace速度慢的原因
今天把ext3.0部署到web project很慢很慢,查了一下,这个当笔记. 转自http://blog.163.com/jong_cai/blog/static/87028045201311178 ...
- Asp.Net MVC 控制器
原文链接:http://www.asp.net/learn/mvc/ 这篇教程探索了ASP.NET MVC控制器(controller).控制器动作(controller action)和动作结果(a ...
- Qt(QML)本地化
Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all Literial UI strings ...
- A - 数塔
A - 数塔 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- uva 10779 Collectors Problem 网络流
链接 一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的. 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, ...
- OC中没有实现NSCopying技术时的深复制技术
最近看书看到一种新的实现深复制对象的技术,使用Foundation的归档技术,下面是代码: NSData *data = [NSKeyedArchiver archivedDataWithRootOb ...
- springmvc定时器
用到的jar包: aopalliance-1.0.jar commons-logging-1.1.3.jar spring-aop-3.2.4.RELEASE.jar spring-beans-3.2 ...
- android小知识之中如何获取当前时间
百度整理过来的 [java] view plaincopyprint? import java.text.SimpleDateFormat; SimpleDateFormat format ...