session共享问题及解决方案
tomcat的安装与配置
tomcat的安装
官网下载
Apache Tomcat官网
解压两份
tomcat集群的配置
官网提供的配置
官网配置地址
我的配置步骤
<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.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.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>
1、将上面的复制到两个tomcat的server.xml配置文件中
2、改端口
3、在tomcat1和tomcat2中的webapps\ROOT目录下删除页面然后加上这三个页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>
tomcat2
</title>
</head>
<body>
SessionID:<%=session.getId()%>
<br>
SessionValue:<%=session.getAttribute("session")%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
out.println("this is tomcat 2");
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>
tomcat2
</title>
</head>
<body>
<%
session.setAttribute("session","libo");
%>
<%
out.println("this is set SessionValue success");
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>
tomcat2
</title>
</head>
<body>
<%
session.removeAttribute("session");
%>
<%
out.println("this is remove SessionValue success");
%>
</body>
</html>
session问题解决方法
前端存储
原理
优缺点
session的复制
操作步骤
都没有登录
tomcat1登录
tomcat2查看是否可以获取数据
tomcat1退出
tomcat2查看是否可以获取数据
原理
优缺点
session粘性
操作步骤
下载安装nginx
配置nginx.conf
把刚才tomcat里面的web.xml去掉distributable标签
访问localhost然后就会一直是一个tomcat
注意
原理
优缺点
后端存储(Mysql、Redis等)
操作步骤
访问我仓库下载代码
点击进入