天天看點

Nginx+Tomcat+Session 高性能群集搭建(測試通過)

今天下午沒事,就做了一個nginx+tomcate+Session 的負載均衡的tomcat叢集,tomcat是用的6.0,nginx用的是nginx-1.1.11。測試的目的是看看通路的壓力分布和session共享。先看看測試效果。

Nginx+Tomcat+Session 高性能群集搭建(測試通過)
Nginx+Tomcat+Session 高性能群集搭建(測試通過)
我在nginx中的nginx.conf中配置的通路權重。是

upstream 127.0.0.1 { 
	
		 #weigth參數表示權值,權值越高被配置設定到的幾率越大 
	
		 server 127.0.0.1:8080 weight=1;
	
		 server 127.0.0.1:8081 weight=1;
	
	 }      

1:1的,是以基本上通路是一次到tomcat1,下次到tomcat2.是以可以看到負載被配置設定到兩個tomcat中去了。在兩個伺服器中,session也是一樣的, 是以session也是共享了。

下面看配置:

下載下傳tomcat就不用說了,主要說裡面的server.xml的配置

tomcat1:

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />


  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">



<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.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>

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

      </Host>
    </Engine>
  </Service>
</Server>
      

tomcat2:

<?xml version='1.0' encoding='utf-8'?>

<Server port="8006" shutdown="SHUTDOWN">

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    
    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
<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="4001" 
                      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.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>


      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

      </Host>
    </Engine>
  </Service>
</Server>
      

要注意上面紅色地方的不同。下面配置nginx.conf.在#gzip on;下配置

#gzip  on;

	#設定負載均衡的伺服器清單
	
	 upstream 127.0.0.1 { 
	
		 #weigth參數表示權值,權值越高被配置設定到的幾率越大 
	
		 server 127.0.0.1:8080 weight=1;
	
		 server 127.0.0.1:8081 weight=1;
	
	 }      

還有一個地方配置:

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
	   proxy_pass http://127.0.0.1;
        }      

listen 80;的端口也可以自定義,如果你機子上有占用了80,那就改一下。

上面綠色的地方是對應的。

還有一個比較重要的就是在工程的web.xml上加一個東西,<distributable/>,請看代碼:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>
      
<body>
    <%  

  //HttpSession session = request.getSession(true);  

  System.out.println(session.getId());  

  out.println("<br> SESSION ID:" + session.getId()+"<br>");    

  // 如果有新的請求,則添加session屬性  

  String name = request.getParameter("name");  

  if (name != null && name.length() > 0) {
     String value = request.getParameter("value");  
     session.setAttribute(name, value);  
  }    

    out.print("<b>Session List:</b>");    
    Enumeration<String> names = session.getAttributeNames();  
    while (names.hasMoreElements()) {  
       String sname = names.nextElement();   
        String value = session.getAttribute(sname).toString();
        out.println( sname + " = " + value+"<br>");
        System.out.println( sname + " = " + value);
   }
%>
  </body>      

繼續閱讀