Tomcat集群 session复制

利用Tomcat集群自带的session共享模块。

第一步:修改tomcat/conf/server.xml
将配置文件中的Cluster 节点的注释放开


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

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->

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

第二步:修改项目的web.xml文件
添加节点distributable,这是因为tomcat集群必须要要带上这个标签,否则不会生效。


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <!--添加该节点-->
  <distributable/>

</web-app>

在服务器上面创建两个tomcat,分别为
tomcat-9091,

	<h1>9091</h1><h1>session id </h1>
		<%=  request.getSession().getId()  %> <span>-----<%=  System.currentTimeMillis()  %></span>
	<hr/>

tomcat-9092

	<h1>9092</h1><h1>session id </h1>
		<%=  request.getSession().getId()  %> <span>-----<%=  System.currentTimeMillis()  %></span>
	<hr/>

测试验证:
在这里插入图片描述

发布了95 篇原创文章 · 获赞 50 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/jc0803kevin/article/details/89407621