Tomcat cluster and session

Posted shenxm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat cluster and session相关的知识,希望对你有一定的参考价值。

Cluster and session

session consistency

        (1) session sticky
            source_ip:
                nginx: ip_hash
                haproxy: source
                lvs: sh
            cookie:
                nginx:hash 
                haproxy: cookie
        (2) session cluster:
            delta(增量)session manager
            tomcat delta manager
        (3) session server:
            redis(store)
            memcached(cache)

Tomcat Cluster

        (1) httpd + tomcat cluster
            httpd: mod_proxy, mod_proxy_http, mod_proxy_balancer
            tomcat cluster:http connector
        (2) httpd + tomcat cluster
            httpd: mod_proxy, mod_proxy_ajp, mod_proxy_balancer
            tomcat cluster:ajp connector
        (3) nginx + tomcat cluster
        保持会话的方式参考第一种方式。
    
    Tomcat Cluster的第一种方法实现
        <proxy balancer://tcsrvs>
            BalancerMember http://172.:8080
            BalancerMember http://172.:8080
            ProxySet lbmethod=byrequests
        </Proxy>
        <VirtualHost *:80>
            ServerName lb.magedu.com
            ProxyVia On
            ProxyRequests Off
            ProxyPreserveHost On
            <Proxy *>
                Require all granted
            </Proxy>
            ProxyPass / balancer://tcsrvs/
            ProxyPassReverse / balancer://tcsrvs/
            <Location />
                Require all granted
            </Location>
        </VirtualHost>
    
    Tomact Cluster的第二种实现方法  
        <proxy balancer://tcsrvs>
            BalancerMember ajp://172.:8009
            BalancerMember ajp://172.18.:8009
            ProxySet lbmethod=byrequests
        </Proxy>
        <VirtualHost *:80>
            ServerName lb.magedu.com
            ProxyVia On
            ProxyRequests Off
            ProxyPreserveHost On
            <Proxy *>
                Require all granted
            </Proxy>
            ProxyPass / balancer://tcsrvs/
            ProxyPassReverse / balancer://tcsrvs/
            <Location />
                Require all granted
            </Location>
            <Location /balancer-manager>
                SetHandler balancer-manager
                ProxyPass !
                Require all granted
            </Location>
        </VirtualHost>

BalancerMember

    BalancerMember [balancerurl] url [key=value [key=value ...]]    
        status: 
            D: Worker已停且不接受任何请求。    
            S: Worker已经有管理地停止。  
            I: Worker采用忽略错误的模式,并被认为总是可用。    
            H: Worker 采用双机热备模式并讲仅在没有其他可用worker时使用。  
            E: Worker 处于错误状态.   
            N: Worker 采用drain mode 并仅接收到达自身的粘性会话, 以及忽略其他所有清空。   
        loadfactor: 
            负载因子,即权重;   
        perform pending request balancing.lbmethod:Balancer load-balance method     
            选择负载平衡的调度算法。    
                byrequests  
                    进行权重请求的计算   
                bytraffic   
                    进行加权流量字节数平衡 
                bybuyness   
                    进行挂起的请求平衡   
                默认是byrequests   
        stickysession:Balancer sticky session name.     
            这个值常被设置为JSESSIONID 或者 phpSESSIONID之类的值,且它依赖于后端应用服务支持的会话。
            如果后端应用服务为cookies使用不同的名,以及url 编码id(如小程序容器)使用“|”来区分它们。
            第一部分用于cookie,第二部分用于路径,可用在iApache HTTP Server 2.4.4 和最新版本。   
        
    会话粘性的实现方法:
        Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
        <proxy balancer://tcsrvs>
            BalancerMember http://172.:8080 route=TomcatA loadfactor=1
            BalancerMember http://172.:8080 route=TomcatB loadfactor=2
            ProxySet lbmethod=byrequests
            ProxySet stickysession=ROUTEID
        </Proxy>
        <VirtualHost *:80>
            ServerName lb.magedu.com
            ProxyVia On
            ProxyRequests Off
            ProxyPreserveHost On
            <Proxy *>
                Require all granted
            </Proxy>
            ProxyPass / balancer://tcsrvs/
            ProxyPassReverse / balancer://tcsrvs/
            <Location />
                Require all granted
            </Location>
        </VirtualHost>  
    启用管理接口:
        <Location /balancer-manager>
            SetHandler balancer-manager
            ProxyPass !
            Require all granted
        </Location> 
    
    示例程序:
        演示效果,在TomcatA上某context中(如/test),提供如下页面
            <%@ page language="java" %>
            <html>
                <head><title>TomcatA</title></head>
                <body>
                    <h1><font color="red">TomcatA.magedu.com</font></h1>
                    <table align="centre" border="1">
                        <tr>
                            <td>Session ID</td>
                        <% session.setAttribute("magedu.com","magedu.com"); %>
                            <td><%= session.getId() %></td>
                        </tr>
                        <tr>
                            <td>Created on</td>
                            <td><%= session.getCreationTime() %></td>
                        </tr>
                    </table>
                </body>
            </html>
    
        演示效果,在TomcatB上某context中(如/test),提供如下页面
            <%@ page language="java" %>
            <html>
                <head><title>TomcatB</title></head>
                    <body>
                    <h1><font color="blue">TomcatB.magedu.com</font></h1>
                    <table align="centre" border="1">
                        <tr>
                            <td>Session ID</td>
                        <% session.setAttribute("magedu.com","magedu.com"); %>
                            <td><%= session.getId() %></td>
                        </tr>
                        <tr>
                            <td>Created on</td>
                            <td><%= session.getCreationTime() %></td>
                        </tr>
                    </table>
                    </body>
            </html> 

Tomcat Session Replication Cluster

    (1) 配置启用集群,将下列配置放置于<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"
                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>  
        确保Engine的jvmRoute属性配置正确。
    
    (2) 配置webapps
        编辑WEB-INF/web.xml,添加<distributable/>元素;
        注意:CentOS 7上的tomcat自带的文档中的配置示例有语法错误;
        <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
        <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        绑定的地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址。

Tomcat Session Server (memcached)

    https://github.com/magro/memcached-session-manager
    支持的session server类型:
        memcached:
        couchbase:
        redis:
       示例方案:
           Tomcat7:
               memcached
                javolution

以上是关于Tomcat cluster and session的主要内容,如果未能解决你的问题,请参考以下文章

通过Nginx_upstream_jvm_route插件实现Tomcat集群的session同步

nginx + tomcat cluster

Tomcat Cluster

tomcat cluster session同步时保存数据的问题

Tomcat 负责均衡及cluster session

Tomcat Cluster