Memcached-session-manager Configuration

Source: Internet
Author: User
Tags define session failover serialization sessions

The article is based on the Memcached-session-manager official configuration Method wiki page translation collation,

About the introduction of Memcached-session-manager,

See official website: http://code.google.com/p/memcached-session-manager/, can also refer to: http://gong1208.iteye.com/blog/1596120

Introduction

For simple use, you only need to install one Tomcat (6 or 7) and memcached, there may be multiple Tomcat servers and multiple available memcached nodes in the production environment, and installed on different machines. We can use sticky session (sticky sessions) or non-sticky session (Non-sticky sessions), and Memcached-session-manager (MSM) supports both modes of operation.

Here is an example of setting up a sticky session pattern with 2 Tomcat and 2 memcached installed in this instance.

The first choice for Tomcat-1 (T1) is to store the session on Memcached-2 (m2) (M2 is a normal node for T1), while M2 is running on a different machine. The T1 stores the session to the Memcached-1 (M1,m1 is a T1 failover node) only if the M2 is unavailable (down or inaccessible). With this configuration, the session will not be lost even if the machine is 1 down. As shown in the following examples:

How do we set it up to be implemented?

Decide which serialization strategy to use

Starting with version 1.1, MSM offers a variety of optional session serialization strategies, with the default strategy of using Java for serialization, which is already integrated into the Memcached-session-manager.jar package, and other policies can be implemented through different jar packages. In the following sections, we can see what the jar packages are specific to each strategy.

Configure Tomcat

The configuration of Tomcat mainly consists of two aspects, first need to download the required packages, placed in the Tomcat installation directory under the Lib directory (strictly $catalina_home/lib/) and our application web-inf/lib/directory, but also need to modify $CATALINA the _home/conf/context.xml file and add memcached Session management configuration information to the <Context> element.

Add Memcached-session-manager Jars to Tomcat

No matter which serialization strategy you choose, you need Memcached-session-manager-${version}.jar, and if you're using TOMCAT6, you'll need to download the memcached-session-manager-tc6-$ {Version}.jar, if you are using TOMCAT7, download Memcached-session-manager-tc7-${version}.jar. You also need to download the Spymemcached-2.7.3.jar. After these jar packages are downloaded, put the jar package into the $CATALINA _home/lib/directory .

ADD custom serializers to your webapp (optional)

If you just use Java serialization, the jar packages you need are the ones listed above, but if you want to use a custom serialization strategy (usually better), we also need to download the appropriate jar package and put it in the web-inf/lib/directory under our WebApp.

If your app uses MAVEN for JAR package management, then you just need to include the corresponding serialization policy dependency definition in Pom.xml, and the specific Maven dependency definition is as follows (either option is OK):

Kryo-serializer:

XML code
  1. <dependency>
  2. <groupId>de.javakaffee.msm</groupId>
  3. <artifactid>msm-kryo-serializer</artifactid>
  4. <version>1.6.0</version>
  5. <scope>runtime</scope>
  6. </Dependency>

Javolution:

XML code
  1. <dependency>
  2. <groupId>de.javakaffee.msm</groupId>
  3. <artifactid>msm-javolution-serializer</artifactid>
  4. <version>1.6.0</version>
  5. <scope>runtime</scope>
  6. </Dependency>

XStream

XML code
  1. <dependency>
  2. <groupId>de.javakaffee.msm</groupId>
  3. <artifactid>msm-xstream-serializer</artifactid>
  4. <version>1.6.0</version>
  5. <scope>runtime</scope>
  6. </Dependency>

Flexjson:

XML code
  1. <dependency>
  2. <groupId>de.javakaffee.msm</groupId>
  3. <artifactid>msm-flexjson-serializer</artifactid>
  4. <version>1.6.0</version>
  5. <scope>runtime</scope>
  6. </Dependency>

If we are not using the MAVEN repository to manage dependencies, we need to download a separate jar package for each strategy, as follows:

    • Kryo-serializer:msm-kryo-serializer, Kryo-serializers, Kryo, Minlog, Reflectasm, asm-3.2
    • Javolution-serializer:msm-javolution-serializer, javolution-5.4.3.1
    • Xstream-serializer:msm-xstream-serializer, XStream, Xmlpull, xpp3_min
    • Flexjson-serializer:msm-flexjson-serializer, Flexjson

Configure Memcached-session-manager as<Context>Manager

After processing the jar package, we also need to modify the content under the context node in the $CATALINA _home/conf/context.xml file and add the Memcached-session-manager configuration.

Specific examples of Tomcat configuration are explained below, mainly including the use of memcached to manage sticky sessions and non-sticky sessions and the use of membase to manage non-sticky sessions. The example is based on the assumption that we have 2 memcached instances, one running on the Host1 host and the other running on the Host2 host, and the example uses a serialization method of Kryo.

Below we give the configuration of TOMCAT1, assuming that TOMCAT1 and memcached node N1 are running on the Host1 host, where the role of failovernodes= "N1" is to tell the MSM is best to save the session in Memcached " N2 node, the session is saved on the N1 node only if the N2 node is unavailable. This way, even if the host1 host is down, the session stored in the memcached "N2" node can still be accessed through the TOMCAT2 on the host2.

TOMCAT1 configuration:

XML code
  1. <Context>
  2. ...
  3. <Manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"
  4. memcachednodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
  5. failovernodes="N1"
  6. requesturiignorepattern=". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ "
  7. transcoderfactoryclass="De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  8. />
  9. </Context>

The above is TOMCAT1 configuration information, for TOMCAT2, we only need to modify the value of Failovernodes property is "N2", so TOMCAT2 will prioritize the session to memcached "N1" node, the rest of the configuration information is the same.

Here is an example of a non-sticky session management configuration where we do not need to configure the Failovernodes property for non-sticky session management because all sessions are loop-visible in the Tomcat cluster and are not bound to a single tomcat , for non-sticky session management, all Tomcat in the cluster is configured with the same configuration, with the following details:

XML code
  1. <Context>
  2. ...
  3. <Manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"
  4. memcachednodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
  5. sticky="false"
  6. sessionbackupasync="false"
  7. lockingmode="Uripattern:/path1|/path2"
  8. requesturiignorepattern=". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ "
  9. transcoderfactoryclass="De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  10. />
  11. </Context>

If you are using Membase to manage the session, then one of the nodes is configured as follows:

XML code
  1. <Context>
  2. ...
  3. <Manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"
  4. memcachednodes="Http://host1.yourdomain.com:8091/pools"
  5. username="Bucket1"
  6. password="Topsecret"
  7. memcachedprotocol="binary"
  8. sticky="false"
  9. sessionbackupasync="false"
  10. requesturiignorepattern=". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ "
  11. transcoderfactoryclass="De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  12. />
  13. </Context>

After the MSM is configured in Context.xml, we can start our application so that all sessions will be stored in the specified memcached node or membase based on the system configuration.

Overview over Memcached-session-manager configuration attributes

ClassName (required)

Class Name: De.javakaffee.web.msm.MemcachedBackupSessionManager

Memcachednodes (required)

Memcached node: This property should contain all running memcached nodes or membase bucket URI addresses, and each memcached node has a property definition format of <id>:

If we are configuring Membase, we can configure one or more membase buckets URIs, starting with version 1.6.0, as follows: Http://host1:8091/pools,http://host2:8091/pools. Bucket names and passwords are defined by attribute Username,password. The Membase buckets connection is subject to the Memcached protocol, which transmits the data through the binary stream mode.

Failovernodes (optional, must not being used for non-sticky sessions)

Failover node: Optional, non-sticky session unavailable, attribute must contain all IDs of the memcached node cluster. The node IDs are separated by a space or a comma.

username (since 1.6.0, optional)

Starting with version 1.6.0, and is optional. Used for membase buckets or SASL verification.

Password (since 1.6.0, optional)

Starting with version 1.6.0, and is optional. Used for membase buckets or SASL authentication, the password can be empty.

Memcachedprotocol (since 1.3, optional, default text)

Define the Memcached protocol and use text text by default

Sticky (since 1.4.0, optional, default true)

Define session mode as sticky or non-sticky, default to True

Lockingmode (since 1.4.0, optional, for Non-sticky sessions only, default none)

Only the non-sticky session is used, the default value is None

    • None: Never Lock the session
    • All: The session will remain locked until the request is over
    • Auto: The session will not be locked for read-only requests, and the session will be locked if it is a non-read-only request
    • uripattern:<regexp>: The request URI and the query string are matched by a regular expression, and only the match is locked.

Requesturiignorepattern (optional)

Sessionbackupasync (optional, default true)

Backupthreadcount (since 1.3, optional, default number-of-cpu-cores)

Sessionbackuptimeout (optional, default )

Operationtimeout (since 1.6.0, optional, default )

Sessionattributefilter (since 1.5.0, optional)

Transcoderfactoryclass (since 1.1, optional, default de.javakaffee.web.msm.JavaSerializationTranscoderFactory)

Serialization Interface implementations:

    • Java Serialization: de.javakaffee.web.msm.JavaSerializationTranscoderFactory
    • Kryo based serialization: de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory
    • Javolution based serialization: de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
    • XStream based serialization: de.javakaffee.web.msm.serializer.xstream.XStreamTranscoderFactory

copycollectionsforserialization (since 1.1, optional, default false)

Customconverter (since 1.2, optional)

Enablestatistics (since 1.2, optional, default true)

enabled (since 1.4.0, optional, default true)

Memcached-session-manager Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.