Three ways to share session in a Tomcat cluster

Source: Internet
Author: User

The first two need to use memcached or Redis storage session, the last one to use terracotta server sharing. Redis is recommended, not only because it can persist cached content, but also because it supports a larger single object and has a rich data type, not just a cache session, but also other uses that can be used for a few clicks.
1, using the filter method to store this method is recommended, because its server uses a lot more, not limited to Tomcat, and the implementation of the principle is relatively simple and easy to control. can use Memcached-session-filter official website: http://code.google.com/p/memcached-session-filter/Official introduction: Solve the cluster environment under the Java Web container session sharing, implemented using the filter interceptor and memcached.  In Tomcat 6 and WebSphere 8 test pass, present network Concurrency 2000, daily PV volume 11 million. Session event not supported includes create destory and attribute change things are nice and small, but this stuff is used with spring and requires objects stored in memcached to implement the Java serialization interface As you can see, the serialization performance of Java itself is also very general. I've simply extended it, no longer relying on spring, and using Javolution to serialize, the cached object no longer has a limit. The implementation of Redis has not been discovered for the time being, which will be implemented using REDIS storage on its own and serialized using Kyro, in which case the details can be written separately.
2, using the Tomcat Session Manager method to store this method the server can only use Tomcat, but on the internet for memcached and Redis implementation, directly configured on the line. memcached implementation: URL: http://code.google.com/p/memcached-session-manager/Modify the Context.xml file under Tomcat's conf directory: < Manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "n1:localhost:11211 n2: localhost:11212 "failovernodes=" N2 "requesturiignorepattern=". *\. (PNG|GIF|JPG|CSS|JS) $ "sessionbackupasync=" false "sessionbackuptimeout=" "transcoderfactoryclass=" de. Javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory "copycollectionsforserialization=" false "/& AMP;GT;
The above is a 1.3 version for example, need to use the jar package: Memcached-session-manager-1.3.0.jar Msm-javolution-serializer-1.3.0.jar Javolution-5.4.3.1.jar Memcached-2.4.2.jar
Redis implementation: URL: Https://github.com/jcoleman/tomcat-redis-session-manager also modifies the context.xml file under Tomcat's conf directory: < Valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/> <          Manager classname= "Com.radiadesign.catalina.session.RedisSessionManager" host= "localhost" port= "6379" database= "0" maxinactiveinterval= "/>" The above is the 1.2 version for example, need to use the jar package: Tomcat-redis-session-manager-1.2-tomcat-6.jar Jedis-2.1.0.jar Commons-pool-1.6.jar
3, using Terracotta server sharing this way configuration is a bit complicated, everyone to search online.
Once the above configuration is successful, the front end uses nginx for load balancing, using both Gzip compression and static file caching.

Here is an example:

One, nginx+tomcat+memcached (dependent package download)

1.memcached configuration: (v1.4.13)

Node 1 (192.168.159.131:11444)

Node 2 (192.168.159.131:11333)

2.tomcat Configuration

TOMCAT1 (192.168.159.128:8081)

TOMCAT2 (192.168.159.128:8082)

3.nginx installed in 192.168.159.131.

First, you configure Tomcat so that it saves the session to memcached. There are two ways of doing this:

Method One: configured in Server.xml.

Find host node, join

<context docbase= "/var/www/html" Path= "" > & Lt Manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "n1:192.168.159.131:11444 n2:192.168.159.131:11333 "requesturiignorepattern=". *\. (PNG|GIF|JPG|CSS|JS) $ "sessionbackupasync=" false "sessionbackuptimeout=" "transcoderfactoryclass=" DE.J Avakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory "copycollectionsforserialization=" false "/& Gt;</context> 

Method Two: Configure in Context.xml.

Locate the context node and join

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"         memcachednodes= "N1 : 192.168.159.131:11444 "         requesturiignorepattern=". *\. ( PNG|GIF|JPG|CSS|JS) $ "         sessionbackupasync=" false "sessionbackuptimeout=" "         transcoderfactoryclass=" De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory "         copycollectionsforserialization=" False "/>

Next, configure Nginx to test the session to remain shared.

Upstream  xxy.com  {      server   192.168.159.128:8081;      Server   192.168.159.128:8082;} Log_format  www_xy_com  ' $remote _addr-$remote _user [$time _local] $request "               " $status "$body _bytes_sent" $http _referer "'                " $http _user_agent "" $http _x_forwarded_for "server{      listen  ;      server_name  xxy.com;      Location/{               proxy_pass        http://xxy.com;               Proxy_set_header   Host             $host;               Proxy_set_header   x-real-ip        $remote _addr;               Proxy_set_header   x-forwarded-for  $proxy _add_x_forwarded_for;      }      Access_log  /data/base_files/logs/www.xy.log  www_xy_com;}

Finally, put your app in two Tomcat and start memcached, Tomcat, and Nginx in turn. By visiting your nginx, you can find that two of the sessions in Tomcat can be shared.

Second, Nginx+tomcat+redis (dependent package download)

1.redis configuration (192.168.159.131:16300) (v2.8.3)

2.tomcat Configuration

TOMCAT1 (192.168.159.130:8081)

TOMCAT2 (192.168.159.130:8082)

3.nginx installed in 192.168.159.131.

First, you configure Tomcat so that it saves the session to Redis. There are two methods, which are also configured in Server.xml or context.xml, except that memcached only needs to add a manager tag, and Redis needs to add the following: ( Note: The valve tag must be in front of the manager.) )

<valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/><manager classname= " Com.radiadesign.catalina.session.RedisSessionManager "         host=" 192.168.159.131 "         port=" 16300 "          database = "0"          maxinactiveinterval= "/>"

Next, configure Nginx to test the session to remain shared.

Upstream  redis.xxy.com  {      server   192.168.159.130:8081;      Server   192.168.159.130:8082;} Log_format  www_xy_com  ' $remote _addr-$remote _user [$time _local] $request "               " $status "$body _bytes_sent" $http _referer "'                " $http _user_agent "" $http _x_forwarded_for "server{      listen  ;      server_name redis.xxy.com;       Location/{               proxy_pass        http://redis.xxy.com;               Proxy_set_header   Host             $host;               Proxy_set_header   x-real-ip        $remote _addr;               Proxy_set_header   x-forwarded-for  $proxy _add_x_forwarded_for;      }      Access_log  /data/base_files/logs/redis.xxy.log  www_xy_com;}

Finally, put your app in two Tomcat and start Redis, Tomcat, and Nginx in turn. By visiting your nginx, you can find that two of the sessions in Tomcat can be shared.

In the above article, one thing to note is:

If you put the manager in Server.xml in a tomcat configuration, a failure occurs when you use Maven to do a hot deployment. Therefore, the recommendation is put in context.xml.

Three ways to share session in a Tomcat cluster

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.