Three methods for session sharing in tomcat cluster

The first two need to use memcached or redis to store sessions, and the last one uses terracotta server sharing.

It is recommended to use redis, not only because it can persist cached content, but also because the single object it supports is relatively large and has rich data types.

Not only for caching sessions, but also for other purposes, serving several purposes with one stone.

 

1. Use the filter method to store

This method is recommended because it has a wide range of servers, not limited to tomcat, and the principle of implementation is relatively simple and easy to control.

You can use memcached-session-filter

Official URL: http://code.google.com/p/memcached-session-filter/

Official introduction: To solve the javaweb container session sharing in the cluster environment, use filter interceptor and memcached to achieve. It has passed the test on tomcat 6 and websphere 8. The current network has 2,000 concurrent transactions and 11 million daily PVs.

Currently does not support sessionevent including create destory and attribute change

The thing is very good, the size is small, but this stuff needs to be used with spring, and the objects stored in memcached are required to implement the serialization interface of java

As we all know, the serialization performance of java itself is also very general.

I simply extended it, no longer relying on spring, and using javolution to achieve serialization, the cached objects are no longer limited.

I haven't found the implementation of redis for the time being. Later, I will use redis to store my own implementation and use kyro for serialization. The details will be written out separately when there is time.

 

 

2. Use the tomcat sessionmanager method to store

In this method, the server can only use tomcat, but there are implementations for memcached and redis on the Internet, which can be configured directly.

memcached implementation:

URL: http://code.google.com/p/memcached-session-manager/

Modify the context.xml file in the conf directory of tomcat:

 <ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"   

 memcachedNodes="n1:localhost:11211n2: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 an example of version 1.3, the jar package that needs to be used:

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

同样修改 tomcat 的 conf目录下的 context.xml 文件:

&lt;ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve"/&gt;

&lt;ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"

        host="localhost"

        port="6379"

        database="0"

        maxInactiveInterval="60"/&gt;

以上是以 1.2 版为例子,需要用的jar 包:

tomcat-redis-session-manager-1.2-tomcat-6.jar

jedis-2.1.0.jar

commons-pool-1.6.jar

 

 

3、使用 terracotta 服务器共享

这种方式配置有点复杂,大家到网上搜索一下吧。

 

以上配置成功后,前端使用 nginx进行负载均衡就行了,同时使用 Gzip 压缩 和 静态文件缓存。

 

以下是实例:

一、nginx+tomcat+memcached  (依赖架包下载)

1.memcached配置:(v1.4.13)

节点1(192.168.159.131:11444)

节点2(192.168.159.131:11333)

2.tomcat配置

tomcat1(192.168.159.128:8081)

tomcat2(192.168.159.128:8082)

3.nginx安装在192.168.159.131。

      首先,是配置tomcat,使其将session保存到memcached上。有两种方法:

方法一:在server.xml中配置。

找到host节点,加入

<ContextdocBase="/var/www/html" path="">
        <ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
               memcachedNodes="n1:192.168.159.131:11444n2:192.168.159.131:11333"
                requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
                sessionBackupAsync="false"sessionBackupTimeout="3000" 
               transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
               copyCollectionsForSerialization="false"/>
</Context>

方法二:在context.xml中配置。

找到Context节点,在context中加入

        <ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:192.168.159.131:11444"
               requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
                sessionBackupAsync="false"sessionBackupTimeout="3000"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
               copyCollectionsForSerialization="false" />

      其次,配置nginx,用于测试session保持共享。

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  80;
      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;
}

最后,将你的应用放到两个tomcat中,并依次启动memcached、tomcat、nginx。访问你的nginx,可以发现两个tomcat中的session可以保持共享了。

二、nginx+tomcat+redis   (依赖架包下载)

1.redis配置(192.168.159.131:16300)(v2.8.3)

2.tomcat配置

tomcat1(192.168.159.130:8081)

tomcat2(192.168.159.130:8082)

3.nginx安装在192.168.159.131。

       首先,是配置tomcat,使其将session保存到redis上。有两种方法,也是在server.xml或context.xml中配置,不同的是memcached只需要添加一个manager标签,而redis需要增加的内容如下:(注意:valve标签一定要在manager前面。)

配置和memcached 一样 找到Context节点,在context中加入

<ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
<ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"
         host="192.168.159.131"
         port="16300"
         database="0"
         maxInactiveInterval="60"/>

其次,配置nginx,用于测试session保持共享。

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  80;
      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;
}

最后,将你的应用放到两个tomcat中,并依次启动redis、tomcat、nginx。访问你的nginx,可以发现两个tomcat中的session可以保持共享了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326728526&siteId=291194637