tomcat保持会话的三种方式

前言css

    前面的博客介绍了如何使用反向代理至tomcat,一方面不让tomcat直接响应客户端请求,一方面实现了动静分离。咱们知道在实际生产中后端不可能就只有一台tomcat server。那么如何实现tomcat的负载均衡呢?咱们先想象一下这个场景,你在家经过拨号上网登陆一个购物网站,通过反向代理后来到第一台tomcat server。而后你在购物网站逛了半天选中了一个物品加入了你的购物车。忽然网络闪断,你从新获取了新的ip,你刷新网页后,购物车的东西还在。你有没有想过为何还在,若是代理服务器把你的jsp请求调度到了另外一台tomcat server呢?实际经验告诉咱们,东西依旧会在购物车里并不会由于消失。在haproxy里能够经过cookie来会话绑定,这个cooki是记录在客户端上的。http一样也能够实现会话粘滞。html

TOM会话保持的三种方式java

    一、session stickygit

    二、session clustergithub

    三、session serverweb

环境准备apache

    当客户端请求jsp等动态资源的时候,反向代理至tomcat来处理。以下图所示。vim

    547255256d1e649b161a0c3199c91a09.png-wh_

第一种方式:会话粘滞后端

httpd服务器
]#vim /etc/httpd/conf.d/tomcat.conf  
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tcsrvs>
        BalancerMember http://192.168.32.111:8080  
        BalancerMember http://192.168.32.112:8080
        ProxySet lbmethod=byrequests
        ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
        ServerName www.a.com
        ProxyVia On                   <===响应报文中添加via首部 
        ProxyRequests Off             <===关闭正向代理
        ProxyPreserveHost On
        <Proxy *>
            Require all granted       <===centos7以前的版本不用写受权,包括下面两个
        </Proxy>
        ProxyPass / balancer://tcsrvs/
        ProxyPa***everse / balancer://tcsrvs/
        <Location />     
            Require all granted
        </Location>
</VirtualHost>
<Location /balancer-manager>          <===启用管理接口
        SetHandler balancer-manager   <===启用内建的处理器
        ProxyPass !                   <===不代理,本机提供服务
        Require all granted
</Location>
配置测试页面
]#mkdir -pv /usr/share/tomcat/webapps/myapp/WEB-INF
]#vim index.jsp            <===第一台tomcat
<%@ page language="java" %>
<html>
        <head><title>TomcatA</title></head>
        <body>
                <h1><font color="red">TomcatA</font></h1>
                <table align="centre" border="1">
                        <tr>
                                <td>Session ID</td>
                        <% session.setAttribute("tomcat.com","tomcat.com"); %>
                                <td><%= session.getId() %></td>
                        </tr>
                        <tr>
                                <td>Created on</td>
                                <td><%= session.getCreationTime() %></td>
                        </tr>
                </table>
        </body>
</html>
]#vim index.jsp            <===第二台tomcat
<%@ page language="java" %>
<html>
        <head><title>TomcatA</title></head>
        <body>
                <h1><font color="red">TomcatB</font></h1>
                <table align="centre" border="1">
                        <tr>
                                <td>Session ID</td>
                        <% session.setAttribute("tomcat.com","tomcat.com"); %>
                                <td><%= session.getId() %></td>
                        </tr>
                        <tr>
                                <td>Created on</td>
                                <td><%= session.getCreationTime() %></td>
                        </tr>
                </table>
        </body>
</html>

第二种方法:session cluster。简单来讲,后面的tomcat server构建了一个集群。会话第一次调度到服务器处理后,会话会同步到全部的tomcat server,从而无论后面再次请求多少次,会话一直保持不变。这里不涉及调度器的问题,反向代理只需正常提供正常的代理就行。centos

http只需提供正常的的反向代理
<proxy balancer://tcsrvs>
        BalancerMember http://192.168.32.111:8080  
        BalancerMember http://192.168.32.112:8080
        ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
        ServerName www.a.com
        ProxyVia On                   
        ProxyRequests Off             
        ProxyPreserveHost On
        <Proxy *>
            Require all granted       <===centos7以前的版本不用写受权,包括下面两个
        </Proxy>
        ProxyPass / balancer://tcsrvs/
        ProxyPa***everse / balancer://tcsrvs/
        <Location />     
            Require all granted
        </Location>
</VirtualHost>

配置tomcat

    一、启用集群,将下列配置放置于server.xml的<engine>或<host>中

<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"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"                   <===auto时,会自动解析本地主机名,并解析得出的IP地址做为使用的地址,通常改为对外提供tomcat服务的地址
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <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"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

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

    二、配置webapps

            编辑站点下WEB-INF/web.xml,添加<distributable/>元素,能够从默认的web.xml拷贝。

    三、测试

      26a254bd799196ee32ef3690f63f62e7.png-wh_

      5970a321684f9cff4e9acd17be942d72.png-wh_

第三种方法:session server,会话服务器简单来讲就是基于会话的缓存服务器,memcached就是其中之一。memcached是一种高性能、分布式的内存对象缓存系统。

]#yum install -y memcached
]#yum install -y libmemcached

    须要的工具类库:一、msm会话管理器

                                二、tomcat版本的另一部分msm,根据本身tomcat版本选择

                                三、可以让jsp程序驱动可以链接适配memcached的类库

    下载地址:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

        4b340fbb1adde85071c16403684f41b0.png-wh_


      四个流式化工具选一个就能够。

   c40fdcdc943c204512bd7c1b4dd3e9c9.png-wh_

     将下载的jar类库放到tomcat安装目录下的lib目录中,修改server.xml

<context path="/myapp" docBase="myapp" reloadable="true">
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="m1:192.168.32.111:11211,m2:192.168.32.112:11211"    <===mencached节点
    failoverNodes="m2"                                                  <===备用节点
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"    <===javolution.javolutionTranscoderFactory名字根据本身选择的工具更改
    />
</Context>
相关文章
相关标签/搜索