Home  >  Article  >  Backend Development  >  Nginx+Tomcat7 (tomcat6)+Memcached cluster Session sharing

Nginx+Tomcat7 (tomcat6)+Memcached cluster Session sharing

WBOY
WBOYOriginal
2016-08-08 09:30:59990browse

Nginx+Tomcat7+Memcached cluster session sharing

Principle:

Mainly uses the memcached-session-manager (hereinafter referred to as msm) open source tomcat plug-in to change tomcat’s original session storage mechanism and put the session storage into the distributed cache memcache , thereby realizing sharing of sessions.

Download

1. Download nginx

2. Download tomcat7 (This article introduces the session sharing of tomcat7. If it is tomcat6, the configuration is unsuccessful and the jar package needs to be replaced, memcached-session-manager-tc6)

3. Download memcached

The above files are included in the compressed package.

Explanation

1. The two tomcats are copied, so they are basically the same, so I won’t go into details here.

2. apache-tomcat-7.0.57-2Access port 8081

3. apache-tomcat-7.0.57-3Access port 8082

4. In test.jsp in webapps->ROOT under 8081, there is a value put into the session, and the key is name. On the contrary, it is only obtained under 8082. If this value can be obtained under 8082, then the session sharing configuration is successful. (This step does not require configuring the nginx cluster)

Configuration

1. Put the following jar package into the tomcat lib directory

2. Add the following configuration to the conf-> context.xml file

herememcachedNodes is to fill in the memcached node , multiple nodes can be separated by "," , such as :n1:localhost:11211,n2:localhost:11212

Manager tag The attribute description is written below, you can understand it.

3. Tomcat -> webapps I won’t say much here, it’s very simple.

4. nginx is fully configured in nginx.conf, you can take a look, it is very simple.

Start access

1. Start two tomcats

2. Start nginx. The disk where nginx is placed here is E drive, you can decide by yourself.

3. Visit http://localhost/test.jsp

4. Refresh several times in succession

If both 8081 and 8082 can print out the words Nginx+Tomcat..., then congratulations, the configuration is successful.

Manager label attribute description

1.className Required class name: de.javakaffee.web.msm.MemcachedBackupSessionManager

2.memcachedNodes Required

memcached nodes

3.sticky Optional S Define the Session method is sticky or non -sticky, defaults to True, when multiple tomcat needs to use non -viscosity

4.LockingMode

options Only used for non-sticky session, the default value is nonenone:
Never lock
sessionall: session
will always be locked until requested End
auto:
For read-only requests,
session will not be locked. If it is a non-read-only request, session will be lockeduriPattern::
Use regular expressions to match the request
uri and the query string. Only those that match will be locked.

5.requestUriIgnorePattern

Optional

This attribute is the regular expression for requests that cannot be changed back up

Session. If static files like css, javascript, images, etc. are served by the same Tomcat and the same application context, these requests will also go through memcached-session-manager. But these requests rarely change within a httpsession, so they don't need to trigger Session backups. So those static files do not need to trigger Session backup, you can use this attribute definition. This attribute must conform to the javaregex regular specification.
Such as:
".*.(png|gif|jpg|css|js)$"

6.sessionBackupAsync Optional specifies whether Session should be asynchronously saved to Memcached. If set to true, the backupThreadCount setting takes effect, and if set false, the expiration time set by sessionBackupTimeout takes effect.

7.backupThreadCount Optional The number of threads used to save Session asynchronously, (if sessionBackupAsync="true "). Default value is the number of cores of cpu.

8.sessionBackupTimeout Optional Set the time it takes to back up a Session. If the operation exceeds the time, the save will fail. This property only works when sessionBackupAsync="false". Default 100ms

Extended

session valid time setting

Session valid time setting is the same as that of ordinary web programs. Add the following code to web.xml:

< session-config> t;

session-config>The part in red is the timeout, in minutes. Note: Multiple web programs share the session in memcached. Depending on the timeout configuration, the following situations will occur: 1. The session validity time settings in multiple web programs are consistent and the expiration time is consistent;

2. Multiple If the session validity time settings in a web program are inconsistent, the shortest one among the set times shall prevail. Session monitoringAdd the following code in web. .csdn. net/detail/kevon_sun/8371071 The above introduces Nginx+Tomcat7 (tomcat6)+Memcached cluster session sharing, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php udp stress testNext article:php udp stress test