Tomcat session remains to achieve session (b)

Tomcat on the back end of the sharing session through multicasting

Experimental architecture diagram:

Tomcat session remains to achieve session (b)

Preparing the Environment

#清空防火墙规则
iptables -F
iptables -X

#临时设置关闭selinux
setenforce 0

#安装jdk,centos7的源默认最高支持jdk1.8
yum -y install java-1.8.0-openjdk-devel 

#更改主机名解析
vim /etc/hosts
192.168.8.161 tomcat2
192.168.8.160 tomcat1

Experimental deployment steps:

Nginx configuration

#安装并配置Nginx反向代理
[root@nginx ~]#yum -y instll nginx
#修改nginx配置文件
[root@nginx ~]#vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}
upstream web {
        server 192.168.8.160:8080;
        server 192.168.8.161:8080;
}
server {
        listen 80 default_server;
        index index.jsp;
        location / {
                proxy_pass http://web/;
        }
}
#检查语法
[root@nginx ~]#nginx -t

#检查无误后启动nginx
[root@nginx ~]#systemctl start nginx

Configuration tomcat1

#安装tomcat以及对应的其他管理工具
[root@tomcat1 ~]#yum -y install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp
#创建必要的文件夹
[root@tomcat1 ~]#mkdir -pv /data/app/ROOT/{WEB-INF,META-INF,classes,lib}
[root@tomcat1 ~]#vim /data/app/ROOT/index.jsp
<%@ 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("www.zd.com","www.zd.com"); %>
                                        <td><%= session.getId() %></td>
                                </tr>
                                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
                </body>
        </html>
#编辑tomcat配置文件
[root@tomcat1 ~]#vim /etc/tomcat/server.xml 
<Host name="localhost"  appBase="/data/app"
            unpackWARs="true" autoDeploy="true">
        <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.100.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                    #本机能够监听的IP地址
                      address="192.168.8.160" 
                      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>
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
      </Host>
#修改tomcat-users.xml配置文件
[root@tomcat1 ~]#vim /etc/tomcat/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="www.test.com" roles="manager-gui,manager-script,admin-gui,admin-script"/>
#拷贝web.xml文件
[root@tomcat1 ~]#cp /etc/tomcat/web.xml /data/app/ROOT/WEB-INF/web.xml 
<distributable/>
#启动tomcat服务
[root@tomcat1 ~]#systemctl restart tomcat

Configuration tomcat2

#创建必要的目录
[root@tomcat2 ~]#mkdir /data
#从tomcat1上拷贝相关文件
[root@tomcat1 ~]#scp -r /data/app/ 192.168.8.161:/data/ 
[root@tomcat1 ~]#scp /etc/tomcat/server.xml 192.168.8.161:/etc/tomcat/server.xml 
[root@tomcat1 ~]#scp /etc/tomcat/tomcat-users.xml 192.168.8.161:/etc/tomcat/tomcat-users.xml 
#编辑网页文件
[root@tomcat2 ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
        <html>
                <head><title>TomcatB</title></head>
                <body>
                        <h1><font color="blue">TomcatB</font></h1>
            table align="centre" border="1">
                                <tr>
                                        <td>Session ID</td>
                                <% session.setAttribute("www.test.com","www.test.com"); %>
                                        <td><%= session.getId() %></td>
                                </tr>
                                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
                </body>
        </html>
#更改tomcat配置文件
[root@tomcat2 ~]#vim /etc/tomcat/server.xml 
<Host name="localhost"  appBase="/data/app"
            unpackWARs="true" autoDeploy="true">
        <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.100.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      #本机能够监听的IP地址
                      address="192.168.8.161" 
                      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>
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
      </Host>
#启动tomcat服务
[root@tomcat2 ~]#systemctl restart tomcat

8080 tomcat1 test whether the start and tomcat2

tomcat1
Tomcat session remains to achieve session (b)
tomcat2
Tomcat session remains to achieve session (b)

Testing with Nginx proxy

# Refresh the page session information remains unchanged
Tomcat session remains to achieve session (b)
Tomcat session remains to achieve session (b)

Guess you like

Origin blog.51cto.com/14163901/2413472