1. 首页
  2. > 香港公司注册 >

tomcat集群是什么意思(tomcat分布式)

前言:


很多时候我们部署应用会发现点击其他页面总要重新登录,这种一般是会话问题,系统访问的时候总是无法保持会话,本文主要是通过配置tomcat集群来实现session共享!




1、配置tomcat8080和tomcat9090端口


修改tomcat9090配置文件server.xml


<Server port="9005" shutdown="SHUTDOWN"> <Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />


2、设置tomcat集群


修改tomcat的配置文件,打开conf下的server.xml文件,找到下面这一行


<Engine name="Catalina" defaultHost="localhost">

不需要做任何修改,在这一行的下面加入如下代码:


<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.tr是什么ibes.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-depltomcatoy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>

这个就是tomcat自带的集群配置了,在tomcat官方文档中的cluster-howto.html中看到相关注意事项,其中有一条需要注意一下:Make sure your web.xml has the <distributable/> element


很明显是说web项目的web.xml文件中需要有<distrib分布式utable/>这个元素,所以在引入的web项目中做修改。



3、修改项目


在 omcatwebapps下新建testcluster文件夹,testcluster下新建index.jsp获取SessionID


<html> <head> <title>title</title> <meta http-equiv="Content-Type"content="text/html; charset=gb2312"/> </head> <body> ​ SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <% out.println("This is Tomcat Server 8080"); %> ​ </body> </html>

testcluster下建立WEB-INF文件夹,在WEB-INF下新建web.xml指向index.jsp和添加<distributable/>元素


<?xml version="1.0" encoding="UTF-8"?> <!-- PublicCMS使用Servlet3.0技术,Web.xml不再是整个工程的入口,config.initializer.*Initializer为工程的入口类,config.*Config为Spring配置 --> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>elearning</display-name> <distributable/> ​ <welcome-file-list> <welcome-fi集群le>index.jsp</welcome-file> </welcome-file-list> </web-app>

注意:由于confcontent.xml配置了web.xml指向是要放在WEB-INF下,所以web.xml需要放在WEB-INF里面


D: omcat集群 omcat8080confcontent.xml


<Context> ​ <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> ​ <!-- Uncomment this to disatomcatble session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> ​ <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnecti意思onManagerValve" /> --> ​ </Context>


4、启动tomcat并测试


启动tomcat:


D: omcat集群 omcat8080binstartup.bat


D: omcat集群 omcat9090binstartup.bat




测试地址:


http://localhost:8080/testcluster/index.jsp


http://localhost:9090/testcluster/index.jsp


每个浏览器会有不同的SessionID,但同个浏览器访问不同端口所获取的SessionID一致





附:tomcat集群配置参数


以上关于tomcat集群的配置只要在 <Engine> 节点 或者 <Host> 是什么节点内部加上下面的代码即可支持集群化:


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

该配置将开启 all-to-all session 复制,并通过 DeltaManager 来复制 session 增量.分布式 采用 all-to-all 方式意味着 session 会被复制到此集群中其他的所有节点. 对于很小的集群,这种方式很适用, 但我们不推荐在较大的集群中使用(有很多 tomcat 节点的情况。例如,几十个节点及以上...). 另外,使用 delta 增量管理器时,即使 某些节点没有部署应用程序,也会复制 session 到所有节点上.


要在较大的集群上进行session复制,需要使用 BackupManager. 此 manager 只复制 session 数据到一个备份节点, 并且只复制到部署了对应应用程序的那些节点. BackupManager的缺意思点: 经过测试,性能不如 delta manager.


下面是一些重要的默认值:


1. 默认的 Multicast (组播)地址是: 228.0.0.4


2. 默认的 Multicast (组播)端口是: 45564 (端口号和地址组合以后就决定了 cluster 关系,被认为是同一个集群).


3. 默认广播的IP java.net.InetAddress.getLocalHost().getHostAddress() (确保你不是广播到 127.0.0.1, 这是一个常见的错误)


4. 默认的监听复制消息的 TCP 端口是在 4000-4100 范围内第一个可用的server socket。


5. 配置了两个监听器: ClusterSessionListener 和 JvmRouteSessionIDBinderListener


6. 配置了两个拦截器: TcpFailureDetector 和 MessageDispatch15Interceptor



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123456@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息