Distributed Session sharing: tomcat + redis enables session sharing and tomcatredis
I. Preface
This article mainly tests the implementation of session sharing in redis and does not discuss how to involve nginx in load balancing.
II. Environment Configuration
This test is performed in windows.
| 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 |
- |
3. Install the tomcat-redis-session-manager plug-in
1. Download source code:
https://github.com/jcoleman/tomcat-redis-session-manager
The source code of the latest version has requirements on the jdk version. It must be JDk1.7. Otherwise, the source code cannot be compiled.
Previously, I used the combination of Tomcat 7 and JDK1.6, and the result failed to run. The following exception is thrown.
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)
If you want to be compatible with Tomcat or jdk of a lower version, you can download the source code of another branch. I have not tested the source code of other branches and do not know how compatible the source code is.
2. or download the Jar package:
https://github.com/jcoleman/tomcat-redis-session-manager/downloads
Many people say that the Jar package has bugs and cannot really run. I tried it for a moment. It does not work, and the code directory structure in the Jar package is inconsistent with the source code. It may be that the package version is too old, so I tried to re-compile the Jar package with the source code.
3. recompile:
The source code is built based on gradle, so you must first build the gradle environment, which is very simple.
3.1 download Gradle and directly download the Complete distribution version.
3.2 decompress the package and set the environment variables.
GRADLE_HOME: E: \ java \ gradle-2.12-all \ gradle-2.12 (extract directory)
PATH: % GRADLE_HOME % \ bin
3.3 cmd Command Prompt: EnterGradle-vTest whether the installation is successful
The output indicates that the installation is successful.
C:\Users\Administrator>gradle -v------------------------------------------------------------Gradle 2.12------------------------------------------------------------Build time: 2016-03-14 08:32:03 UTCBuild number: noneRevision: b29fbb64ad6b068cb3f05f7e40dc670472129bc0Groovy: 2.4.4Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013JVM: 1.7.0_79 (Oracle Corporation 24.79-b02)OS: Windows 7 6.1 amd64
3.4 modify the source code build. gradle File
Because the author uses a third-party repository (sonatype), it is too troublesome to register an account. After commenting, he directly uses the maven central repository, at the same time, annotate the signature script, add the dependency package output script copyJars (dist directory), and the modified build. the gradle file is as follows (the red font is changed ):
Apply plugin: 'java' apply plugin: 'maven 'apply plugin: 'signing' group = 'com. orangefunction 'version = '2. 0.0 'repositories {mavenLocal () mavenCentral ()} compileJava {// java version sourceCompatibility = 1.7 targetCompatibility = 1.7} dependencies {// here, the tomcat version is changed to the actual version number 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. + 'testcompuer' org. hamcrest: hamcrest-core: 1.3 'testcompuer' org. hamcrest: hamcrest-library: 1.3 'testcompuer' 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. allSourceclassifier = 'Sources '} artifacts {archives jararchives javadocJararchives sourcesJar} // signing {// sign configurations. archives // Add this configuration section: task copyJars (type: Copy) {from deployments. runtimeinto '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: // register {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: // github.com/jcoleman/atat-redis-session-manager.git'{connection' scm: git: git@github.com: jcoleman/tomcat-redis-session-manager.git '} licenses {license {name 'mit' url' http: // delete'repo'} developers {developer {id 'jprotocol' name' James cooler' email 'jtc331 @ gmail.com 'url 'https: // github.com/jcoleman '}}}}}}
After the modification, run cmd to enter the source code root directory and execute the build command to re-build the project.
gradle build -x test copyJars
View the output jar package under the source code root directory dist, and add the Jar package related to tomcat-redis-session-manager.
4. Import the jar package and modify context. xml
Put the jar generated above into the lib directory of tomcat, and delete duplicate packages directly.
Open Context. xml and add
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
If multiple tomcat servers exist, repeat the preceding settings.
Iv. Test Results
Start Redis, tomcat1, and tomcat2 in sequence
Open them in the browser:
http://localhost:8081/examples/servlets/servlet/SessionExample
Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB
http://localhost:8082/examples/servlets/servlet/SessionExample
Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB
Use redis-cli to connect to the redis server and enter Keys * to view
We can see that all three session IDs are consistent.