03-tomcat session共享

1、环境约束

  • centos 7 64位
  • nginx-1.5.0
  • apache-tomcat-8.5.38

2、前提约束

  • 完成tomcat集群搭建以及nginx的负载均衡 https://www.jianshu.com/p/c8aa1a323e3a
  • 解压两次apache-tomcat-8.5.38压缩包,分比为tomcat8180和tomcat8280
  • 修改tomcat8180中server.xml的端口分别为8105、8180、8109
  • 修改tomcat8280中server.xml的端口分别为8205、8280、8209

3、分别修改tomcat8180/conf/server.xml以及tomcat8280/conf/server.xml

在 这句话的后面加入以下配置:

<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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;.*\.jsp"/>          
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster> 

4、修改分别修改两个文件夹下的webapps/ROOT/WEB-INF/web.xml

在web-app节点下加入:

<distributable/>

5、分别在tomcat8180/webapps/ROOT/以及tomcat8280/webapps/ROOT/下新增index1.jsp,内容如下:

${pageContext.session.id}

6、启动测试

在浏览器中分别输入:
http://localhost:8180/index1.jsp
http://localhost:8280/index1.jsp
我们会看到,两个的sessionid一样。至此,我们完成了tomcat自带的session共享。

posted @ 2020-03-27 15:14  张力的程序园  阅读(251)  评论(0)    收藏  举报