redis + Tomcat 8 的session共享解决
来源:互联网 发布:js一个函数执行完成 编辑:程序博客网 时间:2024/06/05 20:00
如果英文不错的看,建议直接看官网吧,官网写的挺清楚。下面的内容是转载的一篇文章,自己补充了一些,供大家参考,也欢迎大家一起讨论
官方截止到2015-10-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager
锐洋智能修改的支持Tomcat8的 reyo.redis.session.manager.tomcat8
修改的源代码:RedisSessionManager.java
@SuppressWarnings("deprecation")
private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
log.info("Attempting to use serializer :" + serializationStrategyClass);
serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();
Loader loader = null;
if (getContainer() != null) {
loader = getContainer().getLoader();
}
ClassLoader classLoader = null;
if (loader != null) {
classLoader = loader.getClassLoader();
}
serializer.setClassLoader(classLoader);
}
修改后的内容
private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
log.info("Attempting to use serializer :" + serializationStrategyClass);
serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();
Loader loader = null;
Context context = this.getContext();
if (context != null) {
loader = context.getLoader();
}
ClassLoader classLoader = null;
if (loader != null) {
classLoader = loader.getClassLoader();
}
serializer.setClassLoader(classLoader);
}
前提:你已经部署了Redis,尚未学会的略过
其实很简单,就几个步骤:
1.配置Tomcat的conf目录下的context.xml文件:
1> 单点Reids配置
<Manager className="reyo.redis.session.manager.tomcat8.RedisSessionManager"
host="localhost"
port="6379"
database="0"
password="reyo"
maxInactiveInterval="60"/>
2> Sentinel集群配置:
<!-- Sentinel 配置 -->
<Valve className="reyo.redis.session.manager.tomcat8.RedisSessionHandlerValve" />
<Manager className="reyo.redis.session.manager.tomcat8.RedisSessionManager"
maxInactiveInterval="60"
sentinelMaster="mymaster"
sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />
2.添加jar
3.测试
1>
存储Session:
protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(
"hello"
);
//取得Session对象
HttpSession session=request.getSession();
//设置Session属性
for
(int i=
0
;i<
100000
;i++){
session.setAttribute(
"name"
+i,
"Magci_"
+i);
}
}
2>重启Tomcat:假如Session保存在tomcat下,重启后Session不存在;如果保存在Redis下,Tomcat重启对Session无影响
3>取出Session:
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
System.out.println(
"hello"
);
HttpSession session=request.getSession();
//取出Session属性
for
(
int
i=
0
;i<
100000
;i++){
System.out.println(session.getAttribute(
"name"
+i));
}
}
注意事项:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,其实也很简单,在Tomcat的conf目录下的context.xml文件中,取消注释下面那段配置即可:
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
需要注意的是:
web.xml中的配置是有效的,即使是context.xml总配置maxInactiveInterval默认60秒,只要web.xml中的sessionConfig配置30分钟,则session的失效时间还是30分钟。
运行效果图:
一:redis主从服务器
二:redis Sentinel集群(三台)
三:tomcat8.x 集群(两台)
Sentinel集群下的tomcat...
四:nginx作为前端服务器
五:网站运行效果图:
实例测试地址:http://sms.reyo.cn
用户名:aa 密码:123456
- redis + Tomcat 8 的session共享解决
- redis + Tomcat 8 的session共享解决
- Redis解决Tomcat共享Session
- tomcat redis session 共享
- tomcat-redis-session共享
- tomcat redis session共享遇到的问题
- nginx+tomcat+redis的集群+session共享
- 基于Redis的Tomcat集群Session共享
- tomcat集群-基于redis的session共享
- redis 与tomcat 的session 共享
- tomcat集群-基于redis的session共享
- Tomcat二级域名Session共享问题的解决
- 用Redis存储Tomcat集群的Session实现session共享
- redis+tomcat session共享实践
- tomcat+redis+nginx session共享
- tomcat+redis实现session共享
- tomcat + redis 实现session共享
- Tomcat-Session共享实现-Redis
- 使用WKWebView的一个问题:iOS中使用本地html的一个大坑
- android 使用viewpager实现广告轮播效果
- 4-6-Level-order Traversal
- extends Thread 与 implements Runnable 的区别
- 设计师应该关注的科技发展方向(五)
- redis + Tomcat 8 的session共享解决
- 微信HTML5小游戏之推箱子
- 使用FreeMarker生成静态HTML
- Maven POM 优化 - Scope
- 多屏互动——H5中级进阶
- String类
- C语言求最大公约数和最小公倍数的几种算法
- postgres数据恢复(windows)
- uabntu运行.run文件进行程序安装