> Linux集群 > 服务器集群 >

Nginx+Tomcat+Session 高性能群集搭建(测试通过)

做了一个nginx+tomcate+Session 的负载均衡的tomcat集群,tomcat是用的6.0,nginx用的是nginx-1.1.11。测试的目的是看看访问的压力分布和session共享。先看看测试效果。

我在nginx中的nginx.conf中配置的访问权重。是

  1. upstream 127.0.0.1 {   
  2.       
  3.          #weigth参数表示权值,权值越高被分配到的几率越大   
  4.       
  5.          server 127.0.0.1:8080 weight=1;  
  6.       
  7.          server 127.0.0.1:8081 weight=1;  
  8.       
  9.      }  

1:1的,所以基本上访问是一次到tomcat1,下次到tomcat2.所以可以看到负载被分配到两个tomcat中去了。在两个服务器中,session也是一样的, 所以session也是共享了。

下面看配置:

下载tomcat就不用说了,主要说里面的server.xml的配置

tomcat1:

  1. <?xml version='1.0' encoding='utf-8'?>  
  2.   
  3. <Server port="8005" shutdown="SHUTDOWN">  
  4.   
  5.   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  
  6.   <Listener className="org.apache.catalina.core.JasperListener" />  
  7.   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  
  8.   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />  
  9.   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  
  10.   
  11.   
  12.   <GlobalNamingResources>  
  13.   
  14.     <Resource name="UserDatabase" auth="Container"  
  15.               type="org.apache.catalina.UserDatabase"  
  16.               description="User database that can be updated and saved"  
  17.               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  18.               pathname="conf/tomcat-users.xml" />  
  19.   </GlobalNamingResources>  
  20.   
  21.   
  22.   <Service name="Catalina">  
  23.   
  24.     <Connector port="8080" protocol="HTTP/1.1"   
  25.                connectionTimeout="20000"   
  26.                redirectPort="8443" />  
  27.   
  28.     <!-- Define an AJP 1.3 Connector on port 8009 -->  
  29.     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
  30.   
  31.   
  32.     <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  
  33.   
  34.   
  35.   
  36. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   channelSendOptions="8">    
  37.   
  38.           <Manager className="org.apache.catalina.ha.session.DeltaManager"    
  39.   
  40.                    expireSessionsOnShutdown="false"    
  41.   
  42.                    notifyListenersOnReplication="true"/>    
  43.   
  44.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">    
  45.   
  46.             <Membership className="org.apache.catalina.tribes.membership.McastService"    
  47.   
  48.                         address="228.0.0.4"    
  49.   
  50.                         port="45564"  
  51.   
  52.                         frequency="500"    
  53.   
  54.                         dropTime="3000"/>    
  55.   
  56.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"    
  57.   
  58.                       address="auto"   
  59.   
  60.                      <span style="font-size:16px;"><strong> </strong></span>port="4000"  
  61.   
  62.                       autoBind="100"    
  63.   
  64.                       selectorTimeout="5000"    
  65.   
  66.                       maxThreads="6"/>    
  67.   
  68.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">    
  69.   
  70.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>    
  71.   
  72.             </Sender>    
  73.   
  74.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>    
  75.   
  76.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>    
  77.   
  78.           </Channel>    
  79.   
  80.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"    
  81.   
  82.                  filter=""/>    
  83.   
  84.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>    
  85.   
  86.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"    
  87.   
  88.                     tempDir="/tmp/war-temp/"    
  89.   
  90.                     deployDir="/tmp/war-deploy/"    
  91.   
  92.                     watchDir="/tmp/war-listen/"    
  93.   
  94.                     watchEnabled="false"/>    
  95.   
  96.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>    
  97.   
  98.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>    
  99.   
  100.         </Cluster>  
  101.   
  102.       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
  103.              resourceName="UserDatabase"/>  
  104.   
  105.       <Host name="localhost"  appBase="webapps"  
  106.             unpackWARs="true" autoDeploy="true"  
  107.             xmlValidation="false" xmlNamespaceAware="false">  
  108.   
  109.       </Host>  
  110.     </Engine>  
  111.   </Service>  
  112. </Server>  





tomcat2:

  1. <?xml version='1.0' encoding='utf-8'?>  
  2.   
  3. <Server port="8006" shutdown="SHUTDOWN">  
  4.   
  5.   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  
  6.   <Listener className="org.apache.catalina.core.JasperListener" />  
  7.   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  
  8.   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />  
  9.   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  
  10.   
  11.   <GlobalNamingResources>  
  12.   
  13.     <Resource name="UserDatabase" auth="Container"  
  14.               type="org.apache.catalina.UserDatabase"  
  15.               description="User database that can be updated and saved"  
  16.               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  17.               pathname="conf/tomcat-users.xml" />  
  18.   </GlobalNamingResources>  
  19.   
  20.   
  21.   <Service name="Catalina">  
  22.   
  23.    <strong> </strong><Connector port="8081" protocol="HTTP/1.1"   
  24.                connectionTimeout="20000"   
  25.                redirectPort="8443" />  
  26.       
  27.     <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />  
  28.   
  29.     <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">  
  30. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"    
  31.   
  32.                  channelSendOptions="8">    
  33.   
  34.           <Manager className="org.apache.catalina.ha.session.DeltaManager"    
  35.   
  36.                    expireSessionsOnShutdown="false"    
  37.   
  38.                    notifyListenersOnReplication="true"/>    
  39.   
  40.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">    
  41.   
  42.             <Membership className="org.apache.catalina.tribes.membership.McastService"    
  43.   
  44.                         address="228.0.0.4"    
  45.   
  46.                         port="45564"    
  47.   
  48.                         frequency="500"    
  49.   
  50.                         dropTime="3000"/>    
  51.   
  52.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"    
  53.   
  54.                       address="auto"   
  55.   
  56.                       port="4001"   
  57. <span style="font-size:16px;">  
  58. </span>                      autoBind="100"    
  59.   
  60.                       selectorTimeout="5000"    
  61.   
  62.                       maxThreads="6"/>    
  63.   
  64.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">    
  65.   
  66.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>    
  67.   
  68.             </Sender>    
  69.   
  70.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>    
  71.   
  72.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>    
  73.   
  74.           </Channel>    
  75.   
  76.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"    
  77.   
  78.                  filter=""/>    
  79.   
  80.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>    
  81.   
  82.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"    
  83.   
  84.                     tempDir="/tmp/war-temp/"    
  85.   
  86.                     deployDir="/tmp/war-deploy/"    
  87.   
  88.                     watchDir="/tmp/war-listen/"    
  89.   
  90.                     watchEnabled="false"/>    
  91.   
  92.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>    
  93.   
  94.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>    
  95.   
  96.         </Cluster>  
  97.   
  98.   
  99.       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
  100.              resourceName="UserDatabase"/>  
  101.   
  102.       <!-- Define the default virtual host  
  103.            Note: XML Schema validation will not work with Xerces 2.2.  
  104.        -->  
  105.       <Host name="localhost"  appBase="webapps"  
  106.             unpackWARs="true" autoDeploy="true"  
  107.             xmlValidation="false" xmlNamespaceAware="false">  
  108.   
  109.       </Host>  
  110.     </Engine>  
  111.   </Service>  
  112. </Server>  

要注意上面红色地方的不同。





下面配置nginx.conf.在#gzip on;下配置

  1. #gzip  on;  
  2.   
  3.     #设定负载均衡的服务器列表  
  4.       
  5.      upstream 127.0.0.1 {   
  6.       
  7.          #weigth参数表示权值,权值越高被分配到的几率越大   
  8.       
  9.          server 127.0.0.1:8080 weight=1;  
  10.       
  11.          server 127.0.0.1:8081 weight=1;  
  12.       
  13.      }  

还有一个地方配置:

  1. server {  
  2.         listen       80;  
  3.         server_name  localhost;  
  4.   
  5.         #charset koi8-r;  
  6.   
  7.         #access_log  logs/host.access.log  main;  
  8.   
  9.         location / {  
  10.             root   html;  
  11.             index  index.html index.htm;  
  12.        proxy_pass http://127.0.0.1;  
  13.         }  

listen 80;的端口也可以自定义,如果你机子上有占用了80,那就改一下。
上面绿色的地方是对应的。

还有一个比较重要的就是在工程的web.xml上加一个东西,<distributable/>,请看代码:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <distributable/>  
  11. </web-app>  

测试一下,先建立一个jsp页面,看代码:

  1. <body>  
  2.     <%    
  3.   
  4.   //HttpSession session = request.getSession(true);    
  5.   
  6.   System.out.println(session.getId());    
  7.   
  8.   out.println("<br> SESSION ID:" + session.getId()+"<br>");      
  9.   
  10.   // 如果有新的请求,则添加session属性    
  11.   
  12.   String name = request.getParameter("name");    
  13.   
  14.   if (name != null && name.length() > 0) {  
  15.      String value = request.getParameter("value");    
  16.      session.setAttribute(name, value);    
  17.   }      
  18.   
  19.     out.print("<b>Session List:</b>");      
  20.     Enumeration<String> names = session.getAttributeNames();    
  21.     while (names.hasMoreElements()) {    
  22.        String sname = names.nextElement();     
  23.         String value = session.getAttribute(sname).toString();  
  24.         out.println( sname + " = " + value+"<br>");  
  25.         System.out.println( sname + " = " + value);  
  26.    }  
  27. %>  
  28.   </body>  

然后运行你http://127.0.0.1/你的web项目名称/index.jsp,就能看到上述效果。再闲了,再测试一下lvs+nginx+tomcat的集群配置和效率




 







(责任编辑:IT)