tomcatセッションコピー

5556 ワード

いくつかのプロジェクトでは、複数のサーバがあり、各サーバにセッションがある可能性がありますが、同じユーザーは、同じセッションだけが存在することを望んでいるに違いありません.たとえば、ユーザーのマルチエンドログインです.これでは、sessionレプリケーション、またはredis、memcacheなどのキャッシュを使用して実現する必要があります.ここでは、tomcatの下のsessionレプリケーションについて説明します.
1.tomcatを2つ配備する
私は単機試験なので、2つの異なるポートのtomcat、8080ポートと8081ポートを配置して、はっきりしない学生は、私のこの文章を参考にすることができます.tomcatシングルマシンマルチインスタンス導入
2.j 2 eeのプロジェクトを作成する
このプロジェクトには、主に2つの機能があります.
2.1セッションを書く
2.2このセッションの値を読み込む
ここでは簡単にするために,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はセッションの値(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はセッションの値をとる(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ファイル,ノード,ノードを開き,属性jvmRoute="jvm 1"を追加する.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:ここはローカルオープンtomcat 2台なのでaddress=「auto」
4.テスト効果
前の3歩はもう準備ができています.今、効果を見ることができます.
8080ポート
  1.セッションに値を書き込む、すなわち8080ポート下のindexにアクセスする.jspインタフェース
  2.セッションの値、すなわち8080ポートのindexにアクセスする.jspインタフェース
8081ポート
3.セッションの値を読み込む
congratulation 8081ポートは8080ポートの下に書いたセッションを順調に取得しました!
コードのダウンロード
リファレンスhttp://containsoft.iteye.com/blog/1728209
http://nanquan.iteye.com/blog/1533906