Tomcat session集群

author:JevonWei
版权声明:原创作品


环境

tomcatA    172.16.253.108
tomcatB    172.16.253.105
代理服务器 172.16.253.191

Tomcat session集群

  • Cluster配置文档http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

Tomcat A

[root@tomcatA ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps \\tomcat-admin-webapps为tomcat管理应用程序包 tomcat-docs-webapp为tomcat的在线文档软件包
[root@tomcatA ~]# iptables -F
[root@tomcatA ~]# setenforce 0
[root@tomcatA ~]# ls /usr/share/tomcat/webapps/
docs  examples  host-manager  manager  ROOT  sample
[root@tomcatA ~]# ls /var/lib/tomcat/webapps/
docs  examples  host-manager  manager  ROOT  sample
[root@tomcatA ~]# mkdir -pv /data/webapps/ROOT/{classes,lib,WEB-INF}
[root@tomcatA ~]# vim /data/webapps/ROOT/WEB-INF/index.jsp 
    <%@ 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>
[root@tomcatA ~]# vim /data/webapps/ROOT/index.jsp
    <%@ 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>


配置Tomcat 集群
[root@tomcatA ~]# vim /etc/tomcat/server.xml 

    <Engine name="Catalina" defaultHost="www.jevon1.com" jvmRoute="tcA"> \\定义默认host主机,且添加Route标识符区别集群中的各Tomcat节点
    
    将以下内容或http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html中的Cluster配置放置到Engine或host区域即可,以下放置在了Engine区域
    
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
             channelSendOptions="8">

      <Manager className="org.apache.catalina.ha.session.DeltaManager" \\使用Delta会话管理器
              expireSessionsOnShutdown="false" \\永不过期
               notifyListenersOnReplication="true"/>

      <Channel \\定义集群中的成员及通信 className="org.apache.catalina.tribes.group.GroupChannel">
        <Membership className="org.apache.catalina.tribes.membership.McastService" \\成员关系判定,使用多播地址通信
            address="228.13.5.5" \\定义通信的多播地址,224-239网段
            port="45564" \\发送多播信息的端口
            frequency="500" \\发送信号的间隔
            dropTime="3000"/> \\最长信号信号间隔超时时长
        <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" \\接收会话的机制,NioReceiver异步IO模式
            address="172.16.253.108" \\定义接收信息的监听地址,可为auto自动识别接收IP地址
            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>
    
    <Host name="www.jevon1.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" >
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="jevon_access_log" suffix=".log"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
[root@tomcatA ~]# cp /etc/tomcat/web.xml /data/webapps/ROOT/WEB-INF/
[root@tomcatA ~]# cd /data/webapps/ROOT/WEB-INF/
[root@tomcatA WEB-INF]# vim web.xml
    在<web-app区域中添加<distributable/>   元素,如下
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <distributable/>
[root@tomcatA tomcat]# systemctl start tomcat  

Tomcat B

[root@tomcatB ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps \\tomcat-admin-webapps为tomcat管理应用程序包 tomcat-docs-webapp为tomcat的在线文档软件包
[root@tomcatB ~]# iptables -F
[root@tomcatB ~]# setenforce 0
[root@tomcatB ~]# ls /usr/share/tomcat/webapps/
docs  examples  host-manager  manager  ROOT  sample
[root@tomcatB ~]# ls /var/lib/tomcat/webapps/
docs  examples  host-manager  manager  ROOT  sample
[root@tomcatB ~]# mkdir -pv /usr/share/tomcat/webapps/test/{classes,lib,WEB-INF,META-INF}
[root@tomcatB ~]# vim /data/webapps/ROOT/index.jsp
    <%@ page language="java" %> \\表示页面的编程语言
    <%@ page import="java.util.*" %> \\导入的java类库
    <html>
        <head>
            <title>Test Page</title> \\网页的标题
        </head>
        <body>
            <% out.println("hello world");
            %>
        </body>
    </html>	
[root@TomcatB ~]# vim /etc/tomcat/server.xml 
    <Engine name="Catalina" defaultHost="www.jevon2.com" jvmRoute="tcB"> \\定义默认host主机,且添加Route标识符区别集群中的各Tomcat节点
    
    将以下内容或http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html中的Cluster配置放置到Engine或host区域即可,以下放置在了Engine区域
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
             channelSendOptions="8">

      <Manager className="org.apache.catalina.ha.session.DeltaManager" \\使用Delta会话管理器
              expireSessionsOnShutdown="false" \\永不过期
               notifyListenersOnReplication="true"/>

      <Channel \\定义集群中的成员及通信 className="org.apache.catalina.tribes.group.GroupChannel">
        <Membership className="org.apache.catalina.tribes.membership.McastService" \\成员关系判定,使用多播地址通信
            address="228.13.5.5" \\定义通信的多播地址,224-239网段
            port="45564" \\发送多播信息的端口
            frequency="500" \\发送信号的间隔
            dropTime="3000"/> \\最长信号信号间隔超时时长
        <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" \\接收会话的机制,NioReceiver异步IO模式
            address="172.16.253.105" \\定义接收信息的监听地址,可为auto自动识别接收IP地址
            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>
    
    <Host name="www.jevon2.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" >
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="jevon_access_log" suffix=".log"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
[root@tomcatB ~]# cp /etc/tomcat/web.xml /data/webapps/ROOT/WEB-INF/
[root@tomcatB ~]# cd /data/webapps/ROOT/WEB-INF/
[root@tomcatB WEB-INF]# vim web.xml
    在<web-app区域中添加<distributable/>   元素,如下
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <distributable/>
[root@tomcatA tomcat]# systemctl start tomcat  

Nginx代理
代理服务器

[root@danran ~]# vim /etc/nginx/nginx.conf
    http {
        upstream appsrvs {
            server www.jevon1.com:8080;
            server www.jevon2.com:8080;
        }
        server {
            listen 80;
            server_name www.danran.com;
            index index.jsp index.html;
            root /usr/share/nginx/html;
            location / {
                proxy_pass http://appsrvs/;
            }
        }
    }    
[root@danran ~]# systemctl start nginx
posted @ 2017-09-03 22:30  JevonWei  阅读(273)  评论(0编辑  收藏  举报