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   session 共享

转载于:https://blog.51cto.com/hequan/2066415

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值