nginx+tomcat+session共享

1:写一个测试项目(项目名:Test)
    web.xml
        添加<distributable/>(distributable元素告诉servlet/JSP容器,编写的应用将在分布式Web容器中部署)
    创建index.jsp文件
        <%
            Object o = session.getAttribute("test");
            if(o == null){
                session.setAttribute("test", "test11111");
                out.write("nulllllllll");
            } else {
                out.write(o.toString());
            }
            out.write("<br/>"+session.getServletContext().getRealPath("/"));
        %>
2:搭建两个tomcat服务器(建议使用eclipse:这样比较容易修端口)
    修改其中一个tomcat的所有使用端口值。使两个tomcat的端口没有一个重复
    在两个Tomcat的tomcat/conf/server.xml文件中添加
      <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">      
      <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"
                        mcastBindAddress="127.0.0.1"
                        address="224.0.0.0"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      tcpListenAddress="127.0.0.1"
                      port="4001"
                      autoBind="100"
                      selectorTimeout="100"
                      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.MessageDispatch15Interceptor"/>
          </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.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
3:搭建nginx
    从nginx官网下载相应的nginx(我使用:nginx-1.9.1),并配置(解压--->使用cmd命令进入到此目录下--->执行(start nginx))
    修改nginx/conf/nginx.conf:        
        worker_processes  1;
        events {
            worker_connections  1024;
        }
        http {
            include       mime.types;
            default_type  application/octet-stream;   
            sendfile        on;   
            keepalive_timeout  65;
            upstream local_tomcat {  
                server 127.0.0.1:8080;  
                server 127.0.0.1:8888;
            }      
            server {        
                location / {  
                    proxy_pass http://local_tomcat/;
                }
            }
        }
    执行(nginx -s reload)

4:测试
    在浏览器中访问http://localhost/Test/index.jsp,然后刷新;


你可能感兴趣的:(nginx+tomcat+session共享)