在一些項目中,可能有多個伺服器,每個伺服器都有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!
代碼下載下傳
參考 http://containsoft.iteye.com/blog/1728209
http://nanquan.iteye.com/blog/1533906