随笔-179  评论-666  文章-29  trackbacks-0
花了一个上午的时间研究nginx tomcat的负载均衡测试,集群环境搭建比较顺利,但是session同步的问题折腾了几个小时才搞定,现把我的过程贴上来,以备用。
软件及环境是:
虚拟机上装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,我是参照 配了一个nginx php(fastcgi)环境,然后再加上tomcat的转发。
我的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;#转向tomcat处理       
        }


        location 
~ (/dwr/)? {
         proxy_set_header  host $host;
         proxy_set_header  x
-real-ip  $remote_addr;
         proxy_pass http:
//tserver;#转向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
8005" shutdown="shutdown">

8080" maxhttpheadersize="8192"
   maxthreads="150" minsparethreads="25" maxsparethreads="75"
   enablelookups="false" redirectport="8443" acceptcount="100"
   connectiontimeout="20000" disableuploadtimeout="true" />

8009" enablelookups="false" redirectport="8443" protocol="ajp/1.3" />

找到
tomcat1下的修改为
tomcat2下的修改为

tomcat1找到

修改为
<cluster classname="org.apache.catalina.ha.tcp.simpletcpcluster" 
            channelsendoptions
="6">

            
<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.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" 
            channelsendoptions
="6">

            
<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.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里面前面加入以下这句话

session同步到这里设置完毕

服务器的启动顺序如下:
tomcat1 --> |tomcat2 --> |nginx

先启动tocmat1,tomcat1启动完全的时候再启动tomcat2,等两个tocmat全启动之后,再启动nginx

负载的时候可以用apachenginx,如果什么都不用,两台一接防问了话,可能会出错极session不同步的问题,网上也很多人介意不要做seeeion同步,这样会降低机器的性能,有一个方法可以,就是nginxip_hash,这样至少可以保证客户端去防问同一台tocmat,除非那台tocmat挂了



posted on 2011-06-21 15:38 alpha 阅读(20337) 评论(5)     所属分类: java j2ee jsplinux nginx

评论:
# re: linux nginx tomcat负载均衡,实现session同步 2011-06-21 22:05 |
不错,还可以考虑使用memcache存储session  回复  
  
# re: linux nginx tomcat负载均衡,实现session同步 2011-06-22 15:33 | alpha
一、下载下列几个 jar包






将以下几个jar包copy到 $tomcat_home/lib目下,每个tomcat节点都需要有。

二、配置tomcat的server.xml配置文件,样本可以点击这里下载,主要内容如下:



memcachednodes="n1:localhost:11211"
requesturiignorepattern=".*\.(png|gif|jpg|css|js)$"
sessionbackupasync="false"
sessionbackuptimeout="100"
transcoderfactoryclass="de.javakaffee.web.msm.serializer.javolution.javolutiontranscoderfactory"
copycollectionsforserialization="false"
/>


每个tomcat的节点都需要修改。

  回复  
  
# re: linux nginx tomcat负载均衡,实现session同步[未登录] 2011-06-23 15:49 |
alpha回答的是什么?  回复  
  
# re: linux nginx tomcat负载均衡,实现session同步 2015-09-08 20:23 |
按要求来了,不行嘛。session总是在变。  回复  
  
# re: linux nginx tomcat负载均衡,实现session同步 2015-09-09 15:09 |
@aceslup
位置果然很重要。  回复  
  

只有注册用户后才能发表评论。


网站导航:
              
 
网站地图