Implement session sharing through the memcached-session-manager tutorial

Source: Internet
Author: User
Tags couchbase failover

1 Overview
1.1 determine the serialization policy to use.
1.2 configure tomcat
1.2.1 Add the memcached-session-manager jar package to tomcat.
1.2.2 Add the jar related to serialization to your project (optional ).
1.2.3 configure memcached-session-manager as the management of tomcat <Context>.
1.2.4memcached-session-manager Configuration option parameter introduction.
1.2.5 system parameter configuration (optional)
1.3 configuration log

1 Overview

In the simplest installation method, you need a tomcat (6, 7, or 8) and a memcached (or other memcached-supported protocols ), in the actual product environment, you may have multiple tomcat and multiple memcached instances on different PCs. Memcached-session-manager (msm) supports two working modes. Sticky sessions and no-sticky sessions );
The following is an example of sticky sessions installation. Two tomcat and two memcached;
Tomcat-1 (t1) stores his session mainly in memcached-2 (m2), Tomcat-2 is mainly stored in memcached-1 (m1), if m2 doesn't work normally, t1 stores session in the memcached-1 (m1, m1 is t1's failoverNod ). in this configuration, when one of the two servers (t1 and m1 running on machine1) goes down, the session will not be lost. The following very beautiful ASCII shows this setting;

<T1> <t2>
.\/.
. X.
./\.
<M1> <m2>

So what should we do? ...
1.1 determine the serialization policy to use.
In the serialization policy section, you can select several serialization policies. the default method is java serialization. This method has been supported in the memcached-session-manager jar package. other methods are used in different jar packages. In this section, you will see which jar packages are required for different methods.

1.2 configure tomcat
To configure tomcat, add some jar files to your $ CATALINA_HOME/lib/directory and the WEB-/lib/directory. 2. Configure your memcached-session-manager in the <Content> (conf/context in the tomcat installation directory. xml)
Add jar package to tomcat

1.2.1 according to the serialization policy you selected, you need to add memcached-session-manager-$ {version }. jar also has memcached-session-manager-tc6-$ {version} If tomcat6 needs }. jar, tomcat 7 (tomcat 7 7.0.23 +) requires memcached-session-manager-tc7-$ {version }. jar, tomcat8 requires memcached-session-manager-tc8-$ {version }. jar
Http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/
Http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc6/
Http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/
Http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/

If you're using memcached, you still need a spymemcached-2.10.2.jar.

If you use couchbase, you need to add couchbase-client-1.2.2.jar jettison-1.1.jar, commons-codec-1.5.jar, httpcore-4.1.1.jar, httpcore-nio-4.1.1.jar, netty-3.5.5.Final.jar.
Http://central.maven.org/maven2/com/couchbase/client/couchbase-client/1.2.2/couchbase-client-1.2.2.jar
Http://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.1/jettison-1.1.jar
Http://repo1.maven.org/maven2/commons-codec/commons-codec/1.5/commons-codec-1.5.jar
Http://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.1.1/httpcore-4.1.1.jar
Http://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore-nio/4.1.1/httpcore-nio-4.1.1.jar
Http://repo1.maven.org/maven2/io/netty/netty/3.5.5.Final/netty-3.5.5.Final.jar

Download the appropriate jar package and place it in the $ CATALINA_HOME/lib/directory.
Add a stylized serialization method to your project (optional)
If you want to use java self-built serialization methods, you don't need to do anything. If you want to use different serialization methods (you can achieve better performance ), you need to add some jar packages to your project/WEB-INF/lib.

Msm (in groupId de. javakaffee. (msm) can be obtained in the maven repository. you only need to add dependency management. In your project, you can use maven to define kryo-serializer:

<Dependency>
<GroupId> de. javakaffee. msm </groupId>
<ArtifactId> msm-kryo-serializer </artifactId>
<Version> 1.8.0 </version>
<Scope> runtime </scope>
</Dependency>

The artifactId of javolution is msm-javolution-serializer, xstream is msm-xstream-serializer, and flexjson is msm-flexjson-serializer ..

If you do not use maven, different serialization policies require different jar packages.
Serializers:
Kryo-serializer: msm-kryo-serializer, kryo-serializers-0.11 (0.11 is needed, as 0.20 + is for kryo2), kryo, minlog, reflectasm, asm-3.2
Http://repo1.maven.org/maven2/com/googlecode/kryo/
Http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.11/
Http://repo1.maven.org/maven2/com/googlecode/kryo/
Http://repo1.maven.org/maven2/com/googlecode/minlog/
Http://repo1.maven.org/maven2/com/googlecode/reflectasm/
Http://repo1.maven.org/maven2/asm/asm/3.2/
Javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1
Http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/
Http://memcached-session-manager.googlecode.com/svn/maven/javolution/javolution/5.4.3.1/
Xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min
Http://repo1.maven.org/maven2/de/javakaffee/msm/msm-xstream-serializer/
Http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/
Http://repo1.maven.org/maven2/xmlpull/xmlpull/
Http://repo1.maven.org/maven2/xpp3/xpp3_min/
Flexjson-serializer: msm-flexjson-serializer, flexjson
Http://repo1.maven.org/maven2/de/javakaffee/msm/msm-flexjson-serializer/
Http://repo1.maven.org/maven2/net/sf/flexjson/flexjson/
1.2.3 configure memcached-session-manager as the <Context> Administrator
Update <Context> (usually in the tomcat installation directory/conf/context. xml: If you have modified the location of the configuration file, modify it on your own.) Make the Manager contain memcached-session-manager. The following example is provided.
The following example shows sticky sessions, non-sticky sessions of memcached, and non-sticky sessions of membase;
This configuration assumes that you have two memcached instances installed on two machines, one on host1 and the other on host2. All examples use sticky sessions + kryo (serialization ).

Sticky sessions + kryo example
The following is the configuration of mongoat1. Assume that tomcat runs on mongoat1, and this machine also runs memcached "n1 ". attribute failoverNodes = "n1" tells msm to store the session mainly in memcached "n2". When there are no other memcached nodes (n2 here) if host1 fails, the session will continue to be stored in memcached "n2" tomcat2 ). For the second tomcat (on host2), you only need to change the failover node to "n2". Therefore, he prefers "n2" and does not need to change anything else.

<Context>
...
<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"
MemcachedNodes = "n1: host1.yourdomain.com: 11211, n2: host2.yourdomain.com: 11211"
FailoverNodes = "n1"
RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"
TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. kryo. KryoTranscoderFactory"
/>
</Context>

Example of non-sticky sessions + kryo

The configuration of the non-sticky sessions example does not need to be configured in this example. The session will loop between tomcat and will not only use one tomcat. The following is the non-sticky sessions configuration (all tomcat servers are like this );
<Context>
...
<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"
MemcachedNodes = "n1: host1.yourdomain.com: 11211, n2: host2.yourdomain.com: 11211"
Sticky = "false"
SessionBackupAsync = "false"
LockingMode = "uriPattern:/path1 |/path2"
RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"
TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. kryo. KryoTranscoderFactory"
/>
</Context>
Example of non-sticky sessions with couchbase + kryo

If membase is named "bucket1", the configuration is as follows;

<Context>
...
<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"
MemcachedNodes = "http://host1.yourdomain.com: 8091/pools"
Username = "bucket1"
Password = "topsecret"
MemcachedProtocol = "binary"
Sticky = "false"
SessionBackupAsync = "false"
RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"
TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. kryo. KryoTranscoderFactory"
/>
</Context>

Multiple contexts shared sessions run multiple webapps/contexts shared sessions (for example, sessionCookiePath = "/"-or emptySessionPath = "true" in tomcat6 ), when you use memcached to store sessions, you must tell memcached session manager to add a session prefix. You can use the storageKeyPrefix attribute to set the session prefix (the detailed settings are as follows ):
If you

<Context sessionCookiePath = "/">
...
<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"
MemcachedNodes = "n1: host1.yourdomain.com: 11211, n2: host2.yourdomain.com: 11211"
FailoverNodes = "n1"
StorageKeyPrefix = "context"
RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"
TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. kryo. KryoTranscoderFactory"
/>
</Context>

The following sections provide more detailed configurations.

When we configure msm in the Context/Manager attribute, the session of our program will be stored in memcached or membase. Now you can perform some tests to disable one or more of tomcat, or restart one of the memcached etc, have fun :-)!

1.2.4memcached-session-manager Configuration Attribute overview;
ClassName (required)
This setting allows de. javakaffee. web. msm. MemcachedBackupSessionManager to manage session storage in memcached. However, a de. javakaffee. web. msm. DummyMemcachedBackupSessionManager can be used in the development process after version 1.3.6. It simply serializes sessions in the memory Map. When a request arrives, the data is deserialized only to view the deserialization function. In this case, our program still uses the session. memcached-session-manager (or DummyMemcachedBackupSessionManager) will not work. Session deserialization will be synchronized. memcachedNodes and failoverNode will not create a memcached end, so the serialized data will not be stored in memcached. Memcaches is not required at this time.

MemcachedNodes (required)

This attribute must contain all memcached nodes or membase bucket uri (s), and all tomcat servers must be configured. For example, for a node, <id> is optional (e.g. memcachedNodes = "n1: app01: 11211, n2: app02: 11211"), so you can use Memcached nodes: Each memcached node is defined as <id >:< host >:< port>.

Membase bucket uris (since 1.6.0): one or more membase bucket uris must be configured to use membase, for example, http: // host1: 8091/pools, http: // host2: 8091/pools. the Bucket user name and password must be configured using username and password (as described below ). Connecting to membase requires some jar packages that support the memcached protocol. Put jettison. jar and netty. jar in CATALINA_HOME/lib /.

FailoverNodes (optional, required in non-sticky sessions)

If you have other memcache nodes that can be used, you should list memcached nodes on the same machine as your tomcat. For example, if you have installed ikeat1 (t1) on host1) memcached1 (m1) and host2 are installed with tomcat1 (t2) memcached1 (m2). You should set n1 as the failover node of tomcat1. therefore, when memcached2 (Tomcat 2 failoverNodes is set to n2) cannot be used, the session of tomcat1 is only stored in memcached1. when host1 is down, the session of tomcat1 can continue to work.

Non-sticky sessions do not depend on a single tomcat failoverNodes. This attribute can be omitted for membase buckets.

Multiple memcached nodes must be separated by space "" or comma.

Username (optional after 1.6.0)

The username used by membase bucket or SASL. If memcachedNodes contains one or more membase bucket Uris, This is the bucket name. If memcachedNodes uses username to authorize SASL. A memcached protocol package is also required.

Password (optional after 1.6.0)

Membase bucket or SASL Authorization password (if there is no password, it is null by default ).

MemcachedProtocol (optional after 1.3, default value: text)

Memcached protocol used, text or binary.

Sticky (optional after 1.4.0, default value: true)

Set sticky Session or non-sticky Session.

Check other optional configuration options.

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.