nginx+tomcat session 共享

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat session 共享相关的知识,希望对你有一定的参考价值。

* tomcat1   192.168.10.153

* tomcat2   192.168.10.154

Tomcat 工作模式必须为Nio 模式。

##添加如下内容,         注意更换   address="192.168.10.154"  为本机IP
vim /usr/local/tomcat/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="192.168.10.154"
                      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>
##  修改 web文件,在</web-app>  上面  添加一行内容
vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml

<distributable/>
##添加测试文件
vim      index.jsp
<%@ page contentType="text/html; charset=GBK" %>   
<%@ page import="java.util.*" %>    
<html>  
    <head>  
        <title>Cluster App Test</title>  
    </head>   
    <body>  
    Server Info: <%  out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>   
    <%      
    out.println("
ID " + session.getId()+"
");   // 如果有新的 Session 属性设置      
    String dataName = request.getParameter("dataName");     
        if (dataName != null && dataName.length() > 0) {   
            String dataValue = request.getParameter("dataValue");  
            session.setAttribute(dataName, dataValue);     
        }      
     %>
     </body>   
</html>
##配置 nginx负责均衡,进行测试

        upstream tomcatserver {

        server 192.168.10.153:8080 weight=5;
        server  192.168.10.154:8080  weight=5;

        }     

        location    / {

            proxy_pass http://tomcatserver;  #来自jsp请求交给tomcat处理
        }

技术分享图片

技术分享图片

以上是关于nginx+tomcat session 共享的主要内容,如果未能解决你的问题,请参考以下文章

Nginx+tomcat+memcached缓存共享session

nginx+tomcat session 共享

Nginx+tomcat+memcached实现session共享

nginx+tomcat+redis的集群+session共享

nginx+tomcat集群+redis(memcache)session共享!

Nginx + tomcat + Memcached(session共享)