基於tomcat叢集做session共享

wadeson發表於2017-08-11

前端代理伺服器nginx:192.168.223.136

tomcat伺服器:採用的一臺多例項192.168.223.146:8081,192.168.223.146:8082(如何構建多例項tomcat,請參考前面的文章)

首先檢視下tomcat的webapps目錄架構:

[root@wadeson tomcat-instance]# pwd
/usr/local/tomcat-instance
[root@wadeson tomcat-instance]# ll
總用量 24
-rwxr-xr-x. 1 root root 165 8月 9 15:31 start-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 start-tomcat2.sh
-rwxr-xr-x. 1 root root 165 8月 9 15:38 stop-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 stop-tomcat2.sh
drwxr-xr-x. 7 root root 4096 8月 9 15:45 tomcat1
drwxr-xr-x. 7 root root 4096 8月 9 16:12 tomcat2
[root@wadeson tomcat-instance]# cd tomcat1/webapps/
[root@wadeson webapps]# tree .
.
├── myapp
│   └── test.jsp
└── ROOT
  ├── classes
  ├── index.jsp
  ├── lib
  ├── META-INF
  └── WEB-INF

 由於安裝的tomcat是8.5版本的,所以檢視下官方文件:https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

構建tomcat叢集只需要如下幾步:

1、新增叢集元件資訊(在host或者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.MessageDispatchInterceptor"/>
          </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>
以上是直接摘自官網,需要稍微做下修改address="auto",address="228.0.0.4"
2、需要在各自的應用程式目錄下面為web.xml新增一行<distributable/>元素
  • Make sure your web.xml has the <distributable/> element

現在直接貼自己的配置tomcat1:

<Host name="localhost" appBase="/usr/local/tomcat-instance/tomcat1/webapps"  unpackWARs="true" autoDeploy="true">

   <Context path="" docBase="ROOT" reloadable="true"/>
  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/usr/local/tomcat-instance/tomcat1/logs"
  prefix="localhost_access_log" suffix=".txt"
  pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  <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="192.168.223.146"                 由於是一臺主機上的兩個tomcat例項,所以這裡的cluster新增的內容都一樣
    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.MessageDispatchInterceptor"/>
  </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>
</Host>

然後在應用程式ROOT下面的WEB-INF的下面新增自己的web.xml:

[root@wadeson tomcat-instance]# ll tomcat1/webapps/ROOT/WEB-INF/web.xml
-rw-------. 1 root root 168271 8月 10 16:50 tomcat1/webapps/ROOT/WEB-INF/web.xml

  <distributable/>         這就是需要新增的一行內容
</web-app>

於是配置tomcat1例項完成,tomcat2的配置和tomcat1一致(由於是同一主機的不同例項)

 

於是進行訪問:

可以看見session的id值兩者相同,只是後面的tomcat不一致,於是session共享的功能就達成了,然後配置nginx進行代理轉發:

upstream backserver {
  server 192.168.223.146:8081 weight=1;
  server 192.168.223.146:8082 weight=1;
}

server {
  listen 80;
  server_name 192.168.223.136;

  location / {
    root html;
    index index.html index.htm;
    proxy_pass http://backserver/;
  }

測試效果:

 

相關文章