隨筆-179  評論-666  文章-29  trackbacks-0
          阿里云服務(wù)器優(yōu)惠券

          花了一個上午的時間研究nginx+tomcat的負載均衡測試,集群環(huán)境搭建比較順利,但是session同步的問題折騰了幾個小時才搞定,現(xiàn)把我的過程貼上來,以備用。
          軟件及環(huán)境是:
          虛擬機上裝centos 5.5
          IP為:192.168.0.51 裝上nginx和tomcat  6.0.32 命名為 Tomcat1
          一臺win7上裝tomcat  6.0.32  IP為:192.168.0.50  命名為 Tomcat2

          首先裝nginx,我是參照http://blog.s135.com/nginx_php_v6/ 配了一個Nginx + PHP(FastCGI)環(huán)境,然后再加上Tomcat的轉(zhuǎn)發(fā)。
          我的nginx.conf 修改如下
          #######################################################################
          #
          # This is the main Nginx configuration file.  
          #
          # More information about the configuration options is available on 
          #   
          * the English wiki - http://wiki.nginx.org/Main
          #   * the Russian documentation - http://sysoev.ru/nginx/
          #
          #######################################################################

          #
          ----------------------------------------------------------------------
          # Main Module 
          - directives that cover basic functionality
          #
          #   http:
          //wiki.nginx.org/NginxHttpMainModule
          #
          #
          ----------------------------------------------------------------------

          user              nginx;
          worker_processes  
          8;

          error_log  
          /var/log/nginx/error.log;
          #error_log  
          /var/log/nginx/error.log  notice;
          #error_log  
          /var/log/nginx/error.log  info;

          pid        
          /var/run/nginx.pid;


          #
          ----------------------------------------------------------------------
          # Events Module 
          #
          #   http:
          //wiki.nginx.org/NginxHttpEventsModule
          #
          #
          ----------------------------------------------------------------------

          events 
          {
              worker_connections  
          10240;
          }



          #
          ----------------------------------------------------------------------
          # HTTP Core Module
          #
          #   http:
          //wiki.nginx.org/NginxHttpCoreModule 
          #
          #
          ----------------------------------------------------------------------

          http 
          {
              include       
          /etc/nginx/mime.types;
              default_type  application
          /octet-stream;

              log_format  main  
          '$remote_addr - $remote_user [$time_local] "$request" '
                                
          '$status $body_bytes_sent "$http_referer" '
                                
          '"$http_user_agent" "$http_x_forwarded_for"';

              access_log  
          /var/log/nginx/access.log  main;

          server_names_hash_bucket_size  
          128;
          client_header_buffer_size  32k;
          large_client_header_buffers  
          4  32K;
          client_max_body_size 8m;


              sendfile        on;
              tcp_nopush     on;

              #keepalive_timeout  
          0;
              keepalive_timeout  
          65;

              #gzip  on;

          gzip_min_length 1k;
          gzip_buffers 
          4  16k;
          gzip_http_version 
          1.1;
          gzip_comp_level 
          2;
          gzip_types text
          /plain application/x-javascript text/css application/xml;
          gzip_vary  on;

          upstream  tserver  
          {
                  server   
          192.168.0.51:8080 weight=1;
                  server   
          192.168.0.50:8080 weight=1;
           }

              
              #
              # The 
          default server
              #
              server 
          {
                  listen       
          80;
                  server_name  _;

                  #charset koi8
          -r;

                  #access_log  logs
          /host.access.log  main;

                   location 
          ~ .*\.(php|php5)?$
                  
          {
                    root  
          /www/ROOT;      
                    #fastcgi_pass  unix:
          /tmp/php-cgi.sock;
                   fastcgi_pass  
          127.0.0.1:9000;
                   fastcgi_index index.php;
                   include fcgi.conf;
                  }


                  location 
          ~ \.(jsp|jspx|do|htm)?{
                   proxy_set_header  Host $host;  
                   proxy_set_header  X
          -Real-IP  $remote_addr;
                   proxy_pass http:
          //tserver;#轉(zhuǎn)向tomcat處理       
                  }


                  location 
          ~ (/dwr/)? {
                   proxy_set_header  Host $host;
                   proxy_set_header  X
          -Real-IP  $remote_addr;
                   proxy_pass http:
          //tserver;#轉(zhuǎn)向tomcat處理
                  }



                  location 
          / {
                      root   
          /www/ROOT;
                      index  index.html index.jsp 
          default.jsp index.do default.do;
                  }


                  error_page  
          404              /404.html;
                  location 
          = /404.html {
                      root   
          /www/ROOT;
                  }


                  # redirect server error pages to the 
          static page /50x.html
                  #
                  error_page   
          500 502 503 504  /50x.html;
                  location 
          = /50x.html {
                      root   
          /www/ROOT;
                  }


                  # proxy the PHP scripts to Apache listening on 
          127.0.0.1:80
                  #
                  #location 
          ~ \.php$ {
                  #    proxy_pass   http:
          //127.0.0.1;
                  #}


                  # pass the PHP scripts to FastCGI server listening on 
          127.0.0.1:9000
                  #
                  #location 
          ~ \.php$ {
                  #    root           html;
                  #    fastcgi_pass   
          127.0.0.1:9000;
                  #    fastcgi_index  index.php;
                  #    fastcgi_param  SCRIPT_FILENAME  
          /scripts$fastcgi_script_name;
                  #    include        fastcgi_params;
                  #}


                  # deny access to .htaccess files, 
          if Apache's document root
                  # concurs with nginx's one
                  #
                  #location 
          ~ /\.ht {
                  #    deny  all;
                  #}

              }


              # Load config files from the 
          /etc/nginx/conf.d directory
              include 
          /etc/nginx/conf.d/*.conf;

          }


          下一步是配置Tomcat集群。分別打開tomcat1和tomcat2下conf中server.xml
          <Server port="8005" shutdown="SHUTDOWN">
          <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
          <Connector port="8080" maxHttpHeaderSize="8192"
             maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
             enableLookups="false" redirectPort="8443" acceptCount="100"
             connectionTimeout="20000" disableUploadTimeout="true" />
          <!-- Define an AJP 1.3 Connector on port 8009 -->
          <Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

          找到<Engine name="Catalina" defaultHost="localhost">
          Tomcat1下的修改為<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
          Tomcat2下的修改為<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

          tomcat1找到
          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
          修改為
          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
                      channelSendOptions
          ="6">

                      
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                          expireSessionsOnShutdown
          ="false"
                          notifyListenersOnReplication
          ="true"/>
                      
          <!--
                      
          <Manager className="org.apache.catalina.ha.session.BackupManager"
                          expireSessionsOnShutdown
          ="false"
                          notifyListenersOnReplication
          ="true"
                          mapSendOptions
          ="6"/>
                      
          -->
                      
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                          
          <Membership className="org.apache.catalina.tribes.membership.McastService" 
                              address
          ="228.0.0.5" 
                              bind
          ="192.168.0.51" 
                              port
          ="45564" 
                              frequency
          ="500" 
                              dropTime
          ="3000"/>
                          
          <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
                              address
          ="192.168.0.51" 
                              autoBind
          ="100" 
                              port
          ="4001" 
                              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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
                      
                      
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
                      
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
                  
          </Cluster>


          tomcat2找到
          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
          修改為
          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
                      channelSendOptions
          ="6">

                      
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                          expireSessionsOnShutdown
          ="false"
                          notifyListenersOnReplication
          ="true"/>
                      
          <!--
                      
          <Manager className="org.apache.catalina.ha.session.BackupManager"
                          expireSessionsOnShutdown
          ="false"
                          notifyListenersOnReplication
          ="true"
                          mapSendOptions
          ="6"/>
                      
          -->
                      
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                          
          <Membership className="org.apache.catalina.tribes.membership.McastService" 
                              address
          ="228.0.0.5" 
                              bind
          ="192.168.0.50" 
                              port
          ="45564" 
                              frequency
          ="500" 
                              dropTime
          ="3000"/>
                          
          <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
                              address
          ="192.168.0.50" 
                              autoBind
          ="100" 
                              port
          ="4000" 
                              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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
                      
                      
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
                      
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
                  
          </Cluster>



          還需要在程序的web.xml里面</web-app>前面加入以下這句話
          <distributable/>
          session同步到這里設(shè)置完畢

          服務(wù)器的啟動順序如下:
          tomcat1 --> |tomcat2 --> |nginx

          先啟動tocmat1,tomcat1啟動完全的時候再啟動tomcat2,等兩個tocmat全啟動之后,再啟動nginx

          負載的時候可以用APACHENGINX,如果什么都不用,兩臺一接防問了話,可能會出錯極session不同步的問題,網(wǎng)上也很多人介意不要做seeeion同步,這樣會降低機器的性能,有一個方法可以,就是NGINXip_hash,這樣至少可以保證客戶端去防問同一臺TOCMAT,除非那臺TOCMAT掛了



          阿里云服務(wù)器優(yōu)惠券
          posted on 2011-06-21 15:38 Alpha 閱讀(20441) 評論(5)  編輯  收藏 所屬分類: Java J2EE JSPLinux Nginx

          評論:
          # re: linux+nginx+tomcat負載均衡,實現(xiàn)session同步 2011-06-21 22:05 | CodeMe
          不錯,還可以考慮使用memcache存儲Session  回復(fù)  更多評論
            
          # re: linux+nginx+tomcat負載均衡,實現(xiàn)session同步 2011-06-22 15:33 | Alpha
          一、下載下列幾個 Jar包
          http://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.3.0.jar
          http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-jodatime-1.3.0.jar
          http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-cglib-1.3.0.jar
          http://spymemcached.googlecode.com/files/memcached-2.4.2.jar
          http://memcached-session-manager.googlecode.com/files/javolution-5.4.3.1.jar

          將以下幾個Jar包Copy到 $TOMCAT_HOME/lib目下,每個Tomcat節(jié)點都需要有。

          二、配置Tomcat的Server.xml配置文件,樣本可以點擊這里下載,主要內(nèi)容如下:
          http://www.javabloger.com/images/2010-05/webcluster/server.xml.txt

          <Context docBase="D:\webapp" path="" reloadable="true" >
          <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
          memcachedNodes="n1:localhost:11211"
          requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
          sessionBackupAsync="false"
          sessionBackupTimeout="100"
          transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
          copyCollectionsForSerialization="false"
          />
          </Context>

          每個Tomcat的節(jié)點都需要修改。

            回復(fù)  更多評論
            
          # re: linux+nginx+tomcat負載均衡,實現(xiàn)session同步[未登錄] 2011-06-23 15:49 | chris
          Alpha回答的是什么?  回復(fù)  更多評論
            
          # re: linux+nginx+tomcat負載均衡,實現(xiàn)session同步 2015-09-08 20:23 | Aceslup
          按要求來了,不行嘛。session總是在變。  回復(fù)  更多評論
            
          # re: linux+nginx+tomcat負載均衡,實現(xiàn)session同步 2015-09-09 15:09 | Aceslup
          @Aceslup
          <distributable/> 位置果然很重要。  回復(fù)  更多評論
            
          主站蜘蛛池模板: 台北县| 特克斯县| 新津县| 内江市| 新乡市| 濉溪县| 泸西县| 长垣县| 行唐县| 化州市| 历史| 固始县| 双峰县| 富源县| 松桃| 丹阳市| 嫩江县| 宿迁市| 扬州市| 怀来县| 玉屏| 得荣县| 独山县| 旬邑县| 广州市| 翁牛特旗| 滦平县| 兴仁县| 永泰县| 文化| 罗江县| 仙游县| 永德县| 尤溪县| 防城港市| 巨野县| 雷波县| 乐都县| 江源县| 张家港市| 那坡县|