Tomcat + memcached Session Manager shared session

Source: Internet
Author: User
Tags couchbase failover memcached session id

There are many articles on the web that enable memcached to share the session through MSM (memcached Session manager), but many are patchwork and misleading. There happened to be a place recently, so I'll summarize it.

MSM supports two modes of TOMCAT6,TOMCAT7,TOMCAT8,MSM support: Sticky sessions (sticky session) and Non-sticky sessions (non-sticky session). I use the sticky session, so the following are all followed by sticky session

To introduce. The cluster structure is a 2 Tomcat instance node, 2 memcached instance nodes

<tomcat1><tomcat2>
. \ / .
Machine1. X. Machine2
. / \ .
<memcached1><memcached2>

The following: Tomcat1 to the main to store its session on the MEMCACHED2, and Memcache2 is running on another host (in general, Memcached is the session to save TOMCAT1), Only when the MEMCACHED2 is not available, TOMCAT1 will be memcached1 on the session, that is, memcached1 is to TOMCAT1 do failover nodes. If this is the case, when the Machine1 hangs, the session will not be lost.


Then we consider which serialization of the session, by default, is serialized using Java, which is provided by the Memcached-session-manager.jar jar package, while the other serialization methods are provided by other jar packages.


The first is to install the JDK and Tomcat, which is not mentioned here, and of course Tomcat can choose to support the native function. Before modifying the Tomcat configuration file, put some jar packages in the $CATALINA _home/lib/(web-inf/lib) directory, and then modify $catalina_home/conf/(Meta-inf/context.xml) Configuration file.


We're using memcached, so we need spymemcached-2.11.1.jar.

Supplement: If you use couchbase, you need to add the following jar packages: Couchbase-client-1.4.0.jar jettison-1.3.jar, Commons-codec-1.5.jar , Httpcore-4.3.jar,httpcore-nio-4.3.jar, Netty-3.5.5.final.jar


It is important to note that if you use the built-in serialization of Java, put the jar in the $catalina_home/lib/. If you are using a custom serialization method for better performance, you will need to deploy the other jar packages in the web-inf/lib of the specific Java Project project. Here are the four kinds of jar packages required for the session serialization method


    • Kryo-serializer:msm-kryo-serializer, kryo-serializers-0.11 (0.11 is needed, as 0.20+ are for Kryo2), Kryo, Minlog, reflect ASM, 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


Although I am using the Kryo serialization method + non-sticky session, but also the sticky session together to introduce.

Sticky sessions Sticky session + Kryo serialization

On Machine1, there are TOMCAT1 and Memcached1,tomcat $catalina_home/conf/context.xml add the following configuration

<Context> ... <manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "N1 : host1.example.com:11211,n2:host2.example.com:11211 "failovernodes=" N1 "requesturiignorepattern=". *\.    (ICO|PNG|GIF|JPG|CSS|JS) $ "transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory " /></context>


Parameter failovernodes= "N1" is used to tell the MSM to store the session priority on the MEMCACHED2, only when the Memcached2 is hung, the session will exist memcache1 that is the configuration file N1.

If the whole machine1 hung, the session is still available, because the session is present Machine2 Memcache2, can be provided through the TOMCAT2 external services.

The TOMCAT2 configuration file on the Machine2 can only be changed to Failovernodes= "N2".


Non-sticky sessions non-sticky session + Kryo serialization

The non-sticky session is not required to configure Failovernodes, which is the failover node, so the session is served by all Tomcat nodes through rotation (round-robin), and the session is not bound to a single tomcat node, So tomcat all the nodes are the same, as follows:

<Context> ... <manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "N1 : host1.example.com:11211,n2:host2.example.com:11211 "sticky=" false "sessionbackupasync=" false "LockingMode=" UriPa Ttern:/path1|/path2 "requesturiignorepattern=". *\.    (ICO|PNG|GIF|JPG|CSS|JS) $ "transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory " /></context>

After the configuration is complete, restart the different Tomcat services on the two nodes.

Tail-f/usr/local/tomcat/logs/catalina.out

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4C/DA/wKiom1RGYIKACNTdAAo-3_MzksQ616.jpg "title=" Log.jpg "alt=" Wkiom1rgyikacntdaao-3_mzksq616.jpg "/>

The boot logs for two-node tomcat are normal.


The content of the JSP page tested is as follows

Sessionid:<%=session.getid ()%> <br>sessionip:<%=request.getservername ()%> <BR> Sessionport:<%=request.getserverport ()%>

Browser Access test page

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4C/DA/wKiom1RGaHSy14-BAALVRBJmvwE682.jpg "title=" se.jpg "alt=" Wkiom1rgahsy14-baalvrbjmvwe682.jpg "/>

The page gets the same session ID as the cookie intercepted by my Chrome browser, where the session ID is added to the cookie.

Refresh the page multiple times to see access logs for access to Tomcat, with multiple access records (added here, Tomcat turns off the access log by default, needs to be turned on, and then customizes the log format.) But the session remains the same.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4C/DB/wKioL1RGZcCTUVAqAAU_FvVswHg499.jpg "title=" Acce.png "alt=" Wkiol1rgzcctuvaqaau_fvvswhg499.jpg "/>

Since the session defaults to the presence of Tomcat in the JVM, we verify that memcached does not actually save the session record, query the contents of memcached and then export to the file.

echo "Stats cachedump 3 0" | NC 192.168.203.167 11211 > Session.txt
grep ' 6ede9d610f85926d0a5ad01a991dc84c-n1 ' session.txt

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4C/DB/wKioL1RGZ8-QnpF9AACI_MweLhM965.jpg "title=" Grep.png "alt=" Wkiol1rgz8-qnpf9aaci_mwelhm965.jpg "/>

Confirmation exists.

At this point Tomcat MSM, shared session work is done, good luck!

This article from "Lao Xu's Private Food" blog, declined to reprint!

Tomcat + memcached Session Manager shared session

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.