tomcat8的session共享實現方案


tomcat8的session共享實現

下載tomcat

版本:apache-tomcat-8.0.53.zip

實現步驟,只需要兩步

  1. 兩個tomcat的server.xml都增加一樣cluster配置
	<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.ClusterSessionListener" />
	</Cluster>
  1. 兩個tomcat的應用的web.xml下都增加

比如在:
/Users/Library/Tomcat/apache-tomcat-8.0.53-8081/webapps/ROOT/WEB-INF/web.xml
增加:

	<distributable/>
  1. 然后重啟兩台tomcat即可

新建jsp測試

session.jsp

<html>
<head>
<title>test2</title>
</head>
<body>
	SessionID is
	<%=session.getId()%>
	<BR> SessionIP is
	<%=request.getServerName()%>
	<BR> SessionPort is
	<%=request.getServerPort()%>
	<%
		out.println("Response from tomcat2");
	%>
</body>
</html>

比如放置在:
/Users/Library/Tomcat/apache-tomcat-8.0.53-8082/webapps/ROOT/session.jsp
另一台一樣。

訪問:
http://localhost:8081/session.jsp
http://localhost:8082/session.jsp

sessionid一致:

SessionID is 6E5D26E07FDE6FB5D01A59F457D64333 
SessionIP is tomcat.chinaunicom.tech 
SessionPort is 80 Response from tomcat1

SessionID is 6E5D26E07FDE6FB5D01A59F457D64333 
SessionIP is tomcat.chinaunicom.tech 
SessionPort is 80 Response from tomcat2

注意事項

  1. 需要使用tomcat8版本(上述測試在8.0.53上通過)。如果需要tomcat7,需要修改配置
  2. 不要漏了distributable的配置,漏了session也不能共享
  3. 這種方案只適用於並發量較少的應用,並發量大需使用redis等方案

參考官網:
http://localhost:8082/docs/cluster-howto.html
http://tomcat.apache.org/tomcat-8.0-doc/config/cluster.html
翻譯:
http://wiki.jikexueyuan.com/project/tomcat/clustering.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM