步骤:
1、打开tomcat/conf/server.xml文件,增加如下内容到<Engine></Engine>标记对里:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastBindAddress="127.0.0.1"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="192.168.0.253" //配置IP地址
tcpListenPort="4001" //配置端口,不能与其它tomcat的server.xml端口相同
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
</Cluster>
2、修改WEB-INF/web.xml文件,添加distributable属性,表示tomcat要为此web应用复制session。配置如下:
<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_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<distributable/>
</web-app>
经上两个步骤,可以实现在同一主机上tomcat之间的session共享,没错,是同一机上,在网上查了相关资料,sessionid与域名有关系,http://192.168.1.1/index.jsp与http://192.168.1.2/index.jsp光是配置以上信息是达不到效果的,需要在两个tomcat上加一个负载均衡设备或者负载均衡软件(apache、nginx等),以后再说吧