溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

tomcat8的session共享實(shí)現(xiàn)方案

發(fā)布時(shí)間:2020-07-28 00:52:48 來源:網(wǎng)絡(luò) 閱讀:2723 作者:ouyida3 欄目:軟件技術(shù)

tomcat8的session共享實(shí)現(xiàn)

下載tomcat

版本:apache-tomcat-8.0.53.zip

實(shí)現(xiàn)步驟,只需要兩步

  1. 兩個(gè)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.MessageDispatch25Interceptor" />
        </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>
  2. 兩個(gè)tomcat的應(yīng)用的web.xml下都增加<distributable/>
    比如在:
    /Users/da/Library/Tomcat/apache-tomcat-8.0.53-8081/webapps/ROOT/WEB-INF/web.xml
    增加:
    <distributable/>
  3. 然后重啟兩臺(tái)tomcat即可

新建jsp測(cè)試

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/da/Library/Tomcat/apache-tomcat-8.0.53-8082/webapps/ROOT/session.jsp
另一臺(tái)一樣。

訪問:
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

注意事項(xiàng)

  1. 需要使用tomcat8版本(上述測(cè)試在8.0.53上通過)。如果需要tomcat7,請(qǐng)告訴我,不是這種配置,我需要修改一下
  2. 不要漏了distributable的配置,漏了session也不能共享
  3. 這種方案只適用于并發(fā)量較少的應(yīng)用,目前可以先用著,明天我再嘗試并發(fā)量大的復(fù)雜一點(diǎn)的方案看行不行

參考官網(wǎng):
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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI