Chinaunix首页 | 论坛 | 博客
  • 博客访问: 139628
  • 博文数量: 68
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-28 20:01
文章分类

全部博文(68)

文章存档

2015年(68)

我的朋友

分类: 系统运维

2015-08-31 19:39:32

一、简介

sticky sessions       :有点类似ip hash,简单来说就是前端的负载均衡会把用户在同一session期间的请求都发到后端的同一台tomcat,session存于一台tomcat。

replicated sessions:tomcat的SESSION状态被复制到集群中的其他所有服务器上,即使正在响应用户请求的tomcat关闭或重启,已登录的用户也不用再次登陆网站,重新输入所有存储在SESSION中的数据,如用户名密码之类的。


二、安装配置过程

结构如下:

                                     tomcat1

          /

用户请求--〉nginx          

                                  \

                                     tomcat2



sticky sessions,采用 nginx的nginx_upstream_jvm_route模块来实现,这是国人自已写的一个模块;replicated sessions 则配置tomcat的Cluster。


2.1 安装tomcat

这个简单,装好JDK,设好环境变量,一般不会有什么问题,略过了


2.2 安装nginx

生产环境已装好nginx且已在提供服务,采用无缝升级,过程如下

[root@localhost ]#cd /opt

[root@localhost ]#wget 

[root@localhost ]#svn co 

[root@localhost ]#tar zxvf nginx-1.4.1.tar.gz

[root@localhost ]#cd nginx-1.4.1


[root@localhost nginx-1.4.1]# patch -p0 < /opt/nginx-upstream-jvm-route/jvm_route.patch 

patching file src/http/ngx_http_upstream.c

Hunk #1 succeeded at 4435 (offset 593 lines).

Hunk #3 succeeded at 4567 (offset 593 lines).

Hunk #5 succeeded at 4637 (offset 593 lines).

patching file src/http/ngx_http_upstream.h

Hunk #1 succeeded at 90 (offset 5 lines).


[root@localhost]# /usr/local/nginx/sbin/nginx -V  ##这里是获得以前nginx的编译选项

nginx version: nginx/0.8.52

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --without-http_browser_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module


[root@localhost]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --without-http_browser_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module

 --add-module=/opt/nginx-upstream-jvm-route

 

 

 [root@localhost]#make

 

[root@localhost]# mv /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.version-0.8.52

      

[root@localhost]# cp objs/nginx /usr/local/nginx/sbin/

[root@localhost]# /usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.4.1


[root@localhost]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

[root@localhost]# /usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.4.1


[root@localhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


三、配置sticky sessions

3.1 tomcat

在tomcat的server.xml配置文件中找到这一行

<Engine name="Catalina" defaultHost="localhost" > 更改为:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"

##不同的tomcat jvmRoute设为不相同的就可以了,jvmRoute会使tomcat把"tomcat1"添加到用户请求的JSESSIONID后面:


C:\>curl.exe -I 

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Set-Cookie: JSESSIONID=4FB3201D893A030EB3121A9636C0A4A2.tomcat1; Path=/

Content-Type: text/html;charset=UTF-8

Content-Length: 178

Date: Tue, 28 May 2013 02:51:06 GMT


3.2 nginx

upstream backend {

       server 172.16.1.43:8040 srun_id=tomcat1;

       server 172.16.1.44:8041 srun_id=tomcat2;

       jvm_route $cookie_JSESSIONID reverse;

 }


nginx会检查用户请求的cookie中$cookie_JSESSIONID 字串,reverse表示从后面检查,如果JSESSIONID的值含有tomcat1字符串,则将请求发到后端172.16.1.43:8040,含有tomcat2字符串则转发请求到后端172.16.1.44:8041。没有cookie的请求则采用轮循机制。

style="font-size:14px;"> 有更详细的说明


3.3 测试

测试用的文件可到 style="font-size:14px;"> 上下载,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

  <head>

    </head>

      <body>

       $your_jvm_name

       <br />

       <%out.print(request.getSession()) ;%> <br />

       <%out.println(request.getHeader("Cookie")); %>

      </body>

 </html>


把文件到tomcat的webapps下的ROOT目录,就可以测出同一用户请求是否都被转发到同一个tomcat上。


配置replicated sessions

编辑tomcat的context.xml配置文件,内容如下:

<Context distributable="true"> ##允许tomcat分发内容

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

</Context>


编辑tomcat的server.xml配置文件,添加Cluster配置:

      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">

        <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>

        <Channel className="org.apache.catalina.tribes.group.GroupChannel">

          <Membership className="org.apache.catalina.tribes.membership.McastService"

            address="228.0.0.4"

            port="45564" frequency="500"

            dropTime="3000"/>


         <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

           <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

         </Sender>

      

         <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

           address="auto"

           port="4000"         ##集群中各tomcat的port不能相同

           autoBind="100"

           selectorTimeout="5000"

           maxThreads="6"/>


        <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

        <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

      </Channel>


      <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>

      <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

      <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

      <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

      <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 

        tempDir="/tmp/war-temp/"

        deployDir="/tmp/war-deploy/" 

        watchDir="/tmp/war-listen/" 

        watchEnabled="false"/>   

    </Cluster>


各配项的详细说明可看文档:


测试:

这个测试最好是把应用部署上去,用户登录后,把响应用户请求的tomcat关闭,刷新后看看需不需要重新登录就可以测试session有没有复制成功了


采用session复制时要留意的是无论何时,只要SESSION 被改变,SESSION数据都要重新被复制,如果并发比较高的话会相当耗系统资源,这时可以考虑把session放到memcache中。

阅读(1232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~