本文主要測試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都是一致的。