负载均衡 + session sticky 会话保持 二

配置文件说明
<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="230.100.100.8"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.MessageDispatchInterceptor"/>
	 </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.ClusterSessionListener"/>
</Cluster>

参数说明:
Cluster 		集群配置:添加到 <Engine> 所有虚拟主机都可以启用Session复制,  添加在 <Host> ,该虚拟主机可以启用Session复制
Manager 		会话管理器配置
Channel 		信道配置
Membership 		成员判定。使用什么多播地址、端口多少、间隔时长ms、超时时长ms。同一个多播地址和端口认为同属一个组。使用时修改这个多播地址,以防冲突
Receiver 		接收器,多线程接收多个其他节点的心跳、会话信息。默认会从4000到4100依次尝试可用端口。address="auto",auto可能绑定到127.0.0.1上,所以一定要改为可以用的IP上去
Sender 			多线程发送器,内部使用了tcp连接池。
Interceptor 	拦截器
Valve  			ReplicationValve 检测哪些请求需要检测Session,Session数据是否有了变化,需要启动复制过程
ClusterListener 集群session侦听器

  

 实验:tomcat中 session复制集群 

1.时间同步  其它说明https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

2.nginx设置
http {
    upstream tomcats {
	    #默认wrr
        server www.aaa.com:8080;
        server www.bbb.com:8080;
   }
    server {
        listen       80;
        server_name  www.magedu.net;

        location / {
           proxy_pass http://tomcats;
        }
    }
}


3.T1和T2的tomcat 配置文件设置 1.广播地址同一个2. 添加到 <Engine> 所有虚拟主机都可以启用Session复制,  添加在 <Host> ,该虚拟主机可以启用Session复制
<Engine name="Catalina" defaultHost="www.bbb.com" jvmRoute="Tomcat2" >
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="www.bbb.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        #从这时开始
	<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
         <Channel className="org.apache.catalina.tribes.group.GroupChannel">
             <Membership className="org.apache.catalina.tribes.membership.McastService" 
			address="230.100.100.8"  #广播地址
			port="45564" 			 #45564/UDP
			frequency="500" 
			dropTime="3000"/>
		<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
			address="192.168.80.140"  #此项建议修改为当前主机的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.MessageDispatchInterceptor"/>
         </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.ClusterSessionListener"/>
        </Cluster>
        #这里结束
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>
    </Engine>



4.T1和T2的在应用中增加WEB-INF,从全局复制一个web.xml过来,启用 应用程序的分布式(集群session复制 )
测试中发现全局web.xml中添加标签 <distributable/>没有用。
cp /usr/local/tomcat/conf/web.xml  /data/webapps/ROOT/WEB-INF/

cat /data/webapps/ROOT/WEB-INF/web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        ........
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
 <distributable/>  #增加子标签 <distributable/>
</web-app>



测试总结:同一浏览器 SessionID 不变。主机不同也是不变。
		  不同浏览器 SessionID 会变。

  

 

 

 

posted @ 2022-08-07 17:42  yuanbangchen  阅读(58)  评论(0编辑  收藏  举报