基于redis的tomcat 7.X session同步解决方案

来源:互联网 发布:梦里花落知多少阅读 编辑:程序博客网 时间:2024/06/10 19:43

1.配置参数(tomcat7/conf/context.xml文件节点下增加如下内容)

<Valve className="com.whosenet.tomcat.redissessions.RedisSessionHandlerValve" /><Manager className="com.whosenet.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 --> />

– 单点配置

<!-- Jedis save session--><ValveclassName="com.whosenet.tomcat.redissessions.RedisSessionHandlerValve" /><ManagerclassName="com.whosenet.tomcat.redissessions.RedisSessionManager"  host="localhost"  port="6379"  database="0"  maxInactiveInterval="60"/>

– Sentinel集群配置

<!-- Sentinel 配置 --><ValveclassName="com.whosenet.tomcat.redissessions.RedisSessionHandlerValve" /><ManagerclassName="com.whosenet.tomcat.redissessions.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.添加依赖包

* 将编译后target/tomcat-redis-session-manager-1.0.zip/lib内以下jar拷贝到tomcat/lib下- commons-pool2-2.2.jar- jedis-2.5.2.jar- tomcat-redis-session-manager-1.0.jar

3.项目使用

- session.removeAttribute(key)- session.setAttribute(key, newAttributeValue)

4.注意事项

* 从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分钟。

5.其他说明

* Gradle 构建:https://github.com/jcoleman/tomcat-redis-session-manager* Maven 分支:https://github.com/ruoo/tomcat-redis-session-manager* PS项目说明:截止到2015-05-12前是不支持Tomcat8的

6.问题收集

>> 启动 Tomcat 报错,信息如下:- 八月 12, 2014 12:43:16 下午 org.apache.tomcat.util.digester.Digester startElement- 严重: Begin event threw error- java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool$Config- ......- 八月 12, 2014 12:43:16 下午 org.apache.catalina.startup.HostConfig deployDirectory- 严重: Error deploying web application directory C:\Apache Software\Foundation\tomcat7\tomcat3\apache-tomcat-7.0.55\webapps\docs - java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool$Config触发原因:tomcat环境和jedis版本不匹配解决方案:使用官方推荐版本(commons-pool2-2.2.jar + jedis-2.5.2.jar + tomcat-redis-session-manager-1.0.jar)
>> 启动 Tomcat 报错,报版本不统一,错误信息如下:- java.lang.UnsupportedClassVersionError: com/radiadesign/catalina/session/RedisSessionHandlerValve : Unsupported major.minor version 51.0触发原因:JDK版本不匹配问题解决方案:换成JDK 1.7
>> 启动 Tomcat 成功,操作报错信息如下:- java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute userInfo触发原因:登陆用户对象,不能够序列化解决方案:userInfo对象的实体类AdminUserEntity实现对象序列化
>> 启动 Tomcat 成功,操作报错信息如下:- Caused by: java.lang.ClassNotFoundException:   com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve触发原因:context.xml 配置信息和引入的jar版本不一致解决方案:按照文档重新配置
0 0
原创粉丝点击