1、网上查找资料,大部分从下面网址下载java代码,因是几年前实现的(大概2,3年前吧),不支持tomcat8
https://github.com/jcoleman/tomcat-redis-session-manager
2、在myeclipse 新建一个maven项目【maven-archetype-quickstart】
源文件新建包名com.orangefunction.tomcat.redissessions
讲下载下来的java类拷贝到该包之下(这些java类只实现tomcat7,实现tomcat8需要做一些修改)
JavaSerializer.java
RedisSession.java
RedisSessionHandlerValve.java
RedisSessionManager.java
Serializer.java
SessionSerializationMetadata.java
3、tomcat8与tomcat7 有些实现代码不一样,需各自新建一个maven项目要各自打包,注意tomcat引得版本与jdk要保持一致
====tomcat8 maven pom.xml====
<dependencies>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>tomcat-catalinaartifactId>
<version>8.0.33version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.7.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.0version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
plugins>
build>
====tomcat7 maven pom.xml====
<dependencies>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>tomcat-catalinaartifactId>
<version>7.0.27version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.7.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.0version>
<configuration>
<source>1.7source>
<target>1.7target>
<encoding>UTF-8encoding>
configuration>
plugin>
plugins>
build>
将打包出来的tomcat-redis的jar包和
jedis-2.7.2.jar
commons-pool2-2.3.jar
拷贝到tomcat的lib文件夹下面
4、tomcat配置
====tomcat context.xml====
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
maxInactiveInterval="60" />
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.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" />
5、需要特别注意的是项目中要存入session的对象必须实现序列化,否知会出现序列化错误
public class Test implements Serializable {
private static final long serialVersionUID = 5021582410009851677L;
......
}
配置截图:
参考网址:
redis + Tomcat 8 的session共享解决
http://www.cnblogs.com/interdrp/p/4868740.html
Tomcat7+Redis存储Session
http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831
用Redis存储Tomcat集群的Session
http://blog.csdn.net/chszs/article/details/42610365
分布式集群系统下的高可用session解决方案
http://tendyming.iteye.com/blog/1815136