本文主要测试redis实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等。
本测试在Window下进行
name version port
Tomcat1 7.0.61 127.0.0.1:8081
Tomcat1 7.0.61 127.0.0.1:8082
Redis 2.4.5 127.0.0.1:6379
jdk 1.7 -
1.源码下载:
https://github.com/jcoleman/tomcat-redis-session-manager
最新版源码对jdk版本有要求,必须是JDk1.7,否则编译通不过。
之前我用的是Tomcat7与JDK1.6的组合,结果一直运行不了,抛出如下异常。
java.lang.UnsupportedClassVersionError: com/orangefunction/tomcat/redissessions/RedisSessionHandlerValve : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
如果你想要兼容版本较低的Tomcat或者jdk,可以尝试下载其他分支源码,其他分支我没有进行测试,不知道兼容情况如何。
2.或者Jar包下载:
https://github.com/jcoleman/tomcat-redis-session-manager/downloads
很多人都说Jar包有Bug,并不能真正的运行。我特意试了一下,确实不行,而且Jar包中的代码目录结构都与源码不一致,可能是包的版本太过老旧了,所以我还是尝试用源码重新编译生成Jar包。
3.重新编译:
源码构建基于 gradle,所以先要搭建gradle 环境,搭建很简单。
3.1 下载Gradle,直接下载Complete distribution版就可以。
3.2 下载后解压,并且设置环境变量。
GRADLE_HOME:E:\java\gradle-2.12-all\gradle-2.12(解压目录)
PATH:%GRADLE_HOME%\bin
3.3 cmd命令提示符中:输入gradle -v测试安装成功与否
输出表明安装成功。
C:\Users\Administrator>gradle -v ------------------------------------------------------------ Gradle 2.12 ------------------------------------------------------------ Build time: 2016-03-14 08:32:03 UTC Build number: none Revision: b29fbb64ad6b068cb3f05f7e40dc670472129bc0 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.7.0_79 (Oracle Corporation 24.79-b02) OS: Windows 7 6.1 amd64
3.4 修改源码的build.gradle文件
由于作者使用了第三方仓库(sonatype),需要注册帐号,太麻烦,注释后直接使用maven中央仓库,同时注释签名相关脚本并增加依赖包的输出脚本 copyJars(dist目录),修改后的build.gradle 文件如下(红色字体为修改处):
apply plugin: 'java' apply plugin: 'maven' apply plugin: 'signing' group = 'com.orangefunction' version = '2.0.0' repositories { mavenLocal mavenCentral } compileJava { // java版本 sourceCompatibility = 1.7 targetCompatibility = 1.7 } dependencies { // 此处tomcat版本修改为自己使用的实际版本号 compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.61' compile group: 'redis.clients', name: 'jedis', version: '2.5.2' compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2' //compile group: 'commons-codec', name: 'commons-codec', version: '1.9' testCompile group: 'junit', name: 'junit', version: '4.+' testCompile 'org.hamcrest:hamcrest-core:1.3' testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-all:1.9.5' testCompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.61' } task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from 'build/docs/javadoc' } task sourcesJar(type: Jar) { from sourceSets.main.allSource classifier = 'sources' } artifacts { archives jar archives javadocJar archives sourcesJar } //signing { // sign configurations.archives //} // 新增这段配置 task copyJars(type: Copy) { from configurations.runtime into 'dist' } uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } // repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { // authentication(userName: sonatypeUsername, password: sonatypePassword) // } //repository(url: "https://oss.sonatype.org/content/repositories/snapshots") { // authentication(userName: sonatypeUsername, password: sonatypePassword) //} pom.project { name 'tomcat-redis-session-manager' packaging 'jar' description 'Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis' url 'https://github.com/jcoleman/tomcat-redis-session-manager' issueManagement { url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues' system 'GitHub Issues' } scm { url 'https://github.com:jcoleman/tomcat-redis-session-manager' connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git' developerConnection 'scm:git:[email protected]:jcoleman/tomcat-redis-session-manager.git' } licenses { license { name 'MIT' url 'http://opensource.org/licenses/MIT' distribution 'repo' } } developers { developer { id 'jcoleman' name 'James Coleman' email '[email protected]' url 'https://github.com/jcoleman' } } } } } }
修改完成后,cmd进入源码根目录,执行构建命令重新构建项目
gradle build -x test copyJars
在源码根目录dist下查看输出jar包,另附下载地址:tomcat-redis-session-manager的相关Jar包
4 导入jar包并修改context.xml
把以上生成的jar放入tomcat的lib目录下,有重复的包直接删除。
打开Context.xml,添加
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
如果有多个tomcat,重复以上设置
依次启动Redis、tomcat1、tomcat2
在浏览器中分别打开:
http://localhost:8081/examples/servlets/servlet/SessionExample
Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB
http://localhost:8082/examples/servlets/servlet/SessionExample
Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB
使用 redis-cli 连接 redis 服务器,输入Keys * 查看
可以看到,三处SessionID都是一致的。