Nginx+tomcat7+memcached Cluster session Sharing
Principle:
The main use of Memcached-session-manager (hereinafter referred to as MSM) Open source Tomcat plugin changes Tomcat original session storage mechanism, the session storage into the distributed cache memcache, Thus, the session can be shared.
Download
1. Download Nginx
2. Download TOMCAT7 (This article describes the TOMCAT7 session sharing, if it is TOMCAT6, the configuration is unsuccessful, need to replace the jar package, MEMCACHED-SESSION-MANAGER-TC6)
3. Download memcached
The above files are contained within the compressed package.
Description
1. This two Tomcat is a copy, so it's basically exactly the same, and there's not much to say here.
2. apache-tomcat-7.0.57-2 access Port 8081
3. apache-tomcat-7.0.57-3 access Port 8082
4. In the 8081 webapps->root the test.jsp in the session to the value, key is name. Conversely, 8082 is only obtained, if the value can be taken out at 8082, then the session sharing calculation is configured successfully. (This step eliminates the need to configure the Nginx cluster)
Configuration
1. Place the following jar packages into the Tomcat Lib directory
2. Add the following configuration to the conf-> context.xml file
Here's Memcachednodes is to fill in memcached node , multiple nodes can be "," separate, as : n1:localhost:11211,n2:localhost:11212
The manager tag attribute description is written below and can be understood below.
3. Tomcat---WebApps here is not much to say, very simple.
4. Nginx in the nginx.conf complete configuration, you can go to see, very simple.
Start Access
1. Start two Tomcat
2. Start Nginx, I here Nginx put disk is e disk, can decide for themselves.
3. Visit http://localhost/test.jsp
4. Continuous refresh of a few
If both 8081 and 8082 can print out nginx+tomcat ... And so on, congratulations, the configuration is successful.
Manager Tag Property Description
1.className must
Class Name: De.javakaffee.web.msm.MemcachedBackupSessionManager
2.memcachedNodes must be
memcached Node
3.sticky options available
definition Session The method is sticky or non-viscous, the default is true , multiple Tomcat use non-viscous
4.lockingMode options available
only non-viscousSessionis used, the default value isNone
None:never toSessionto lock
All:sessionwill always be locked, knowing the end of the request
Auto:for read-only requests,Sessionwill not be locked, if it is a non-read-only request,Sessionwill be locked
Uripattern:<regexp>:by means of regular expressions to the requestURIand a query string to match, only the match will be locked.
5.requestUriIgnorePattern options available
This property is those that cannot be changed backSessionthe regular expression of the request. If likeCss,javascript,pictures and other static files are the sameTomcatand the same applicationcontext to provide, these requests are alsoMemcached-session-manager. But these requests are in ahttpthere was little change in the conversation, so they didn't needtriggeringSessionBackup. So those static files don't need to be triggered.SessionBackup, you can use this property definition. This property must conform to theJavaregexRegular specification.
such as:".*\. (PNG|GIF|JPG|CSS|JS) $ "
6.sessionbackupasync optional
specify Session should be asynchronously saved to memcached true backupthreadcount The setting works if you set the false sessionbackuptimeout The expiration time of the setting is in effect.
7.backupthreadcount optional
to save asynchronously session The number of threads, ( if Sessionbackupasync= "true") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CPU
8.sessionBackupTimeout options available
Set the time it takes to back up a Session , and if the operation exceeds the time then the save fails. This property only works on sessionbackupasync= "false" . Default in milliseconds
Expand
Session Effective Time setting
The session effective time setting is the same as the normal Web program, and the following code is added on Web. xml:
< Session-config >
< Session-timeout > Ten </ Session-timeout >
</ Session-config >
The red Word part is the time-out, in minutes.
Attention:
Multiple Web programs share the session in Memcached, depending on the time-out configuration, the following conditions occur:
1, multiple Web programs in the session effective time set consistent, the expiration time is consistent;
2, multiple Web programs in the session effective time set inconsistent, to set the shortest one of the time prevail.
Session Monitoring
Add the following code to Web. XML:
<listener> <listener-class> listener. Sessionlistener (configured according to your own package name) </listener-class> </listener>
Example code:
public class Sessionlistener implements Httpsessionlistener {@Overridepublic void sessioncreated (httpsessionevent Event) {System.out.println ("--------------------------create"); HttpSession session = Event.getsession ();} @Overridepublic void Sessiondestroyed (Httpsessionevent event) {System.out.println ("-------------------------- Destroy ");}}
: http://download.csdn.net/detail/kevon_sun/8371071
NGINX+TOMCAT7 (TOMCAT6) +memcached cluster session sharing