Session Change Tracking
As noted in the "Overview" section above, in order to prevent colliding writes, the Redis Session Manager only serializes the session object into Redis if the session object has changed (it always updates the expiration separately however.) This dirty tracking marks the session as needing serialization according to the following rules:
- Calling
session.removeAttribute(key)
always marks the session as dirty (needing serialization.) - Calling
session.setAttribute(key, newAttributeValue)
marks the session as dirty if any of the following are true:previousAttributeValue == null && newAttributeValue != null
previousAttributeValue != null && newAttributeValue == null
!newAttributeValue.getClass().isInstance(previousAttributeValue)
!newAttributeValue.equals(previousAttributeValue)
This feature can have the unintended consequence of hiding writes if you implicitly change a key in the session or if the object's equality does not change even though the key is updated. For example, assuming the session already contains the key "myArray"
with an Array instance as its corresponding value, and has been previously serialized, the following code would not cause the session to be serialized again:
List myArray = session.getAttribute("myArray");
myArray.add(additionalArrayValue);
If your code makes these kind of changes, then the RedisSession provides a mechanism by which you can mark the session as dirty in order to guarantee serialization at the end of the request. For example:
List myArray = session.getAttribute("myArray");
myArray.add(additionalArrayValue);
session.setAttribute("__changed__");
In order to not cause issues with an application that may already use the key "__changed__"
, this feature is disabled by default. To enable this feature, simple call the following code in your application's initialization:
RedisSession.setManualDirtyTrackingSupportEnabled(true);
This feature also allows the attribute key used to mark the session as dirty to be changed. For example, if you executed the following:
RedisSession.setManualDirtyTrackingAttributeKey("customDirtyFlag");
Then the example above would look like this:
List myArray = session.getAttribute("myArray");
myArray.add(additionalArrayValue);
session.setAttribute("customDirtyFlag");
简单说就是tomcat-redis-session-manager判断一个session是否是dirty时,如果自定义对象所在类未覆盖equals方法,针对每个属性做判断的话,仅修改某一属性时,tomcat-redis-session-manager无法监测到,因此也无法将修改后的session序列化到redis中,因此出现了session同步的问题。
因此,修改的方式就是在修改session自定义对象属性的地方,增加session.setAttribute("__changed__","");调用,前提是RedisSession.setManualDirtyTrackingSupportEnabled(true);
即开启了手工设置dirty标志。
具体配置方法:
Add the following into your Tomcat context.xml (or the context block of the server.xml if applicable.)
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.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) --> />
The Valve must be declared before the Manager.
Copy the tomcat-redis-session-manager.jar and jedis-2.0.0.jar files into the lib
directory of your Tomcat installation.
Reboot the server, and sessions should now be stored in Redis.
另外,tomcat-redis-session-manager的源码是2年前的,最高支持tomcat7,所以我修改了一下代码,增加了对tomcat8的支持。
github地址:
https://github.com/bsr1983/tomcat-redis-session-manager
在tomcat8下已测试通过。
相关推荐
tomcat-redis-session-manager-1.2-tomcat-7-java-7tomcat-redis-session-manager-1.2-tomcat-7-java-7tomcat-redis-session-manager-1.2-tomcat-7-java-7tomcat-redis-session-manager-1.2-tomcat-7-java-7tomcat-...
`tomcat-redis-session-manager`就是这样一款解决方案,它将Tomcat的session管理与Redis相结合,实现了跨服务器的session共享。 首先,让我们理解`tomcat-redis-session-manager`的核心概念。这是一个开源项目,它...
"tomcat9+tomcat-cluster-redis-session-manager_4.0.zip"这个文件组合涉及到的是在Tomcat 9上实现负载均衡以及使用Redis作为Session管理器的高级配置。 首先,Tomcat 9是Apache Tomcat服务器的一个版本,它是Java ...
tomcat-redis-session-manager是专门为Tomcat设计的一个插件,它实现了SessionManager接口,允许Tomcat将session数据序列化并存储到Redis中。这样,无论用户连接到哪个Tomcat实例,都可以从Redis中获取其session信息...
为了解决这些问题,开发者引入了Redis作为session存储的中间件,而Tomcat-Redis-Session-Manager就是这样一个将Tomcat与Redis结合的解决方案。 Tomcat-Redis-Session-Manager是一个开源项目,它允许Tomcat服务器将...
1. **Session创建与更新**:当用户请求到达服务器时,如果创建或更新Session,Tomcat-Redis-Session-Manager会将Session对象序列化为字节数组,然后存储到Redis中,键为服务器生成的唯一Session ID。 2. **Session...
【标题】"tomcat-redis-session-manager jar包"是一个用于集成Redis进行Session管理的Java库,特别设计用于Apache Tomcat服务器。这个库使得在多台Tomcat服务器之间共享和协调用户的Session数据成为可能,从而提高了...
nginx做请求转发,服务器tomcat解决session不同步问题;步骤:1、解压之后,将jar包放入tomcat的lib中(注意是tomcat/lib中,不是我们自己项目的lib);2、配置解压之后的redis-data-cache.properties(根据你的...
这里提到的"session 共享 tomcat-redis-session-manager"就是一种解决方案,它利用Redis作为中央存储来实现Tomcat容器中的Session共享。 首先,我们来看看标题所提及的"session 共享 tomcat-redis-session-manager...
压缩文件包括tomcat-redis-session-manager-master-2.0.0.jar、jedis-2.7.3.jar、commons-pool2-2.3.jar三个jar包使用方法请参照https://github.com/jcoleman/tomcat-redis-session-manager。apache-tomcat-8.5.33....
这个类实现了`org.apache.catalina.SessionManager`接口,这意味着它可以无缝集成到Tomcat中。它使用Redis的`Jedis`客户端库进行通信,通过`KeyPrefix`策略来区分不同应用的会话键,避免了键冲突。 `RedisSession`...
标题中的“Tomcat8亲测可用 tomcat-redis-session-manager的jar包”指的是一个专为Tomcat8设计的,用于管理session的扩展组件。这个组件实现了将Tomcat应用服务器中的用户session数据存储到Redis分布式缓存系统中,...
3. **配置灵活性**: `tomcat-redis-session-manager-2.0.0`提供了丰富的配置选项,允许开发者自定义Redis连接参数,如主机地址、端口、密码,以及Session过期策略等。 4. **性能优化**: 使用了高效的序列化和反序列...
"tomcat-redis-session-manager-1.2-tomcat-6.jar" 和 "tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar" 是针对Tomcat 6和Tomcat 7的Redis Session Manager实现,这两个jar文件提供了将Tomcat的session存储...
tomcat-redis-session-manager的jar包,适用tomcat7版本。jar构建来自GitHub上开源项目打包
用于配置 tomcat-redis-session-manager
tomcat-redis-session-manager-2.0.0.jar jedis-2.5.2.jar commons-pool2-2.2.jar 2.修改 conf 目录下的 context.xml 文件 <Valve className="com.orangefunction.tomcat.redissessions....
tomcat-redis-session-manager-2.0.0.jar包,不用自己打包了,tomcat共享session到redis中,解决分布式应用的状态问题。
支持tomcat8的sessionManager,kuanrf-tomcat-redis-session-manager-1.0.jar