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 only the cache session, but also can do other purposes, a few at a stroke.

1. Use the filter method to store
This method is recommended, because it uses a wide range of servers, not limited to Tomcat, and the implementation of the principle is relatively simple and easy to control.
You can use Memcached-session-filter
Official website: http://code.google.com/p/memcached-session-filter/
Official introduction: To solve the cluster Environment Java Web container session sharing, using the filter interceptor and memcached implementation. 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
It's nice and small, but this stuff is going to be 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. Use the Tomcat Session Manager method to store
This way the server can only use Tomcat, but the web has a memcached and Redis implementation, directly configured on the line.
Memcached implementation:
Website: 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= "100"
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
Copycollectionsforserialization= "false"/>

The above is a 1.3 version for the 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 implementations:
Website: Https://github.com/jcoleman/tomcat-redis-session-manager
Also modify 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 a 1.2 version for the 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, let's search the Internet.

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.

Transfer from http://www.cnblogs.com/interdrp/p/4056525.html

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.