Overview
In Web projects, Tomcat's access is always limited, this time need to use the Tomcat cluster, multiple tomcat to consider the session sharing problem, here is a use of memcached to do session sharing solution
Environment
Operating system: Linux (CentOS 6..5 version)
Software: TOMCAT7
Memcached
Implementation principle
Tomcat + Memcached Implement session sharing flowchart
Configuration
- Install Tomcat 2 and memcached 1, refer to "Linux" Tomcat installation and one server configuration multiple tomcat and "Linux" memcached installation
- Configure Tomcat, into the Tomcat installation directory lib, join: Required jar Package
MAVEN relies on the following:
1 <Dependency>2 <groupId>Asm</groupId>3 <Artifactid>Asm</Artifactid>4 <version>3.2</version>5 </Dependency>6 7 <Dependency>8 <groupId>Com.couchbase.client</groupId>9 <Artifactid>Couchbase-client</Artifactid>Ten <version>1.4.11</version> One </Dependency> A - <Dependency> - <groupId>Com.googlecode</groupId> the <Artifactid>Kryo</Artifactid> - <version>1.04</version> - </Dependency> - + <Dependency> - <groupId>De.javakaffee</groupId> + <Artifactid>Kryo-serializers</Artifactid> A <version>0.11</version> at </Dependency> - - <Dependency> - <groupId>De.javakaffee.msm</groupId> - <Artifactid>Memcached-session-manager</Artifactid> - <version>1.8.2</version> in </Dependency> - to <Dependency> + <groupId>De.javakaffee.msm</groupId> - <Artifactid>Memcached-session-manager-tc7</Artifactid> the <version>1.8.2</version> * </Dependency> $ Panax Notoginseng <Dependency> - <groupId>Com.googlecode</groupId> the <Artifactid>Minlog</Artifactid> + <version>1.2</version> A </Dependency> the + <Dependency> - <groupId>De.javakaffee.msm</groupId> $ <Artifactid>Msm-kryo-serializer</Artifactid> $ <version>1.8.2</version> - </Dependency> - the <Dependency> - <groupId>Com.esotericsoftware</groupId>Wuyi <Artifactid>Reflectasm</Artifactid> the <version>1.01</version> - </Dependency> Wu - <Dependency> About <groupId>Net.spy</groupId> $ <Artifactid>Spymemcached</Artifactid> - <version>2.11.4</version> - </Dependency>
View Code
- In the context.xml file under the Tomcat installation directory, add:
1 <Manager2 ClassName= "De.javakaffee.web.msm.MemcachedBackupSessionManager" 3 Memcachednodes= "n1:127.0.0.1:11211" 4 Sticky= "false" 5 Sessionbackupasync= "false" 6 Requesturiignorepattern=".*\. (ico|png|gif|jpg|jpeg|bmp|css|js|html|htm) $ " 7 Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 8 />
- In the Server.xml file under the Tomcat installation directory, add:
1 < name= "Catalina" defaulthost= "localhost" jvmroute= " TOMCAT1 ">
The second tomcat is set jvmroute= "TOMCAT2" to distinguish Tomcat
- Add a test interface to Tomcat, session.jsp
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <BR>4SessionID:<%=Session.getid ()%>5 <BR>6Sessionip:<%=Request.getservername ()%>7 <BR>8Sessionport:<%=Request.getserverport ()%>9 <BR>
- In the same browser, open 2 Tomcat test interface, SessionID the same, indicating the session sharing through memcached, as follows
Note: This scheme and can be used in conjunction with Nginx, using Nginx distribution request to access different Tomcat, and the session is shared
"Tomcat" Tomcat + Memcached for session sharing