在一些項目中,可能有多個服務器,每個服務器都有session,但,同一個用户,我們肯定是希望只存在同一個session。比如,用户的多端登錄。這樣的話,我們就需要使用session複製,或者redis、memcache等緩存來實現,這裏,介紹tomcat下的session複製。
1.部署兩個tomcat
因為我是單機試驗,所以就部署兩個不同端口的tomcat,8080端口和8081端口,不清楚的同學,可以參考我這篇文章。tomcat單機多實例部署
2.建立一個j2ee的項目
這個項目,主要有二個功能,
2.1 寫一個session
2.2 讀取這個session中的值
這裏為了簡單,就直接在jsp中操作session。
index.jsp 放session
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>這是index.jsp 插入session</title>
<%
session.setAttribute("001Session", "001Session_val");
out.print("設置session 001Session的值為:001Session_val");
%>
</head>
<body>
</body>
</html>
index2.jsp 取session的值(8080端口)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>001 獲取session的值 index2.jsp</title>
<%
String sessionVal = (String)session.getAttribute("001Session");
out.print("8080端口的tomcat獲取001Session的值:"+sessionVal);
%>
</head>
<body>
</body>
</html>
index3.jsp 取session的值(8081端口)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>001 獲取session的值 index3.jsp</title>
<%
String sessionVal = (String)session.getAttribute("001Session");
out.print("8081端口的tomcat獲取001Session的值:" + sessionVal);
%>
</head>
<body>
</body>
</html>
3.配置tomcat
1.配置8080端口,conf/server.xml文件,<server>節點,開啓<Engine>節點,並增加屬性 jvmRoute="jvm1"。在engine節點中,增加子節點內容
<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.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-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
2.配置8081端口,類似1步驟,只需要把jvmRoute="jvm2"
tips:因為這裏是本地開啓2台tomcat,所以address="auto"
4.測試效果
前面三步已經打好準備,現在我們可以來看下效果。
8080端口
1.在session中寫值,即訪問8080端口下的index.jsp界面
2.讀session的值,即訪問8080端口下的index.jsp界面
8081端口
3.讀取session的值
congratulation 8081端口順利取得8080端口下寫的session!