负载均衡与集群之nginx+tomcat+memcached

来源:互联网 发布:百度软件管家官方下载 编辑:程序博客网 时间:2024/06/08 07:50
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

         近年来nginx越来越流行,很多网站都开始用nginx来做web代理,以及原来的大公司也都在考虑用nginx来代替apache和resign。由 于我在上个公司时,一个电子商务网站开始用的是apache+tomcat,后来也开始考虑用nginx+tomcat+memcache来代替 apache+tomcat。

         通用架构如下:


其中用memcache来存储session,tomcat作为servlet容 器,nginx作为tomcat代理,对外接口。这个时候只要保证memcache服务器不停,tomcat之间可以任意热插拔。实现了session的 独立存储,需要的话,还可以存储到数据库或者文件中,也不用考虑是否使用黏性session。

        在本例中,使用到了一个memcache服务器,3个tomcat,一个nginx,rhel5.4 64位操作系统,配置如下。

 nginx配置:


nginx.conf配置如下:

    #user  nobody;  
    worker_processes  1;  
      
    #error_log  logs/error.log;  
    #error_log  logs/error.log  notice;  
    #error_log  logs/error.log  info;  
      
    #pid        logs/nginx.pid;  
      
      
    events {  
        worker_connections  1024;  
    }  
      
      
    http {  
        include       mime.types;  
        default_type  application/octet-stream;  
        server_tokens off;  
        #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  logs/access.log  main;  
      
        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;  
        gzip_disable "MSIE [1-6].";  
       
        proxy_temp_path proxy_temp_dir;  
        proxy_cache_path proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;  
      
          
        upstream shopcluster{  
        server 127.0.0.1:8180 weight=1;  
        server 127.0.0.1:8182 weight=1;  
        }  
      
        upstream hmfkcluster{  
        server 127.0.0.1:8080 weight=1;  
        server 127.0.0.1:8081 weight=1;  
        }  
      
        server {  
            listen       80 default;  
            server_name  localhost;  
      
            #charset utf-8;  
      
            #access_log  logs/host.access.log  main;  
      
            location / {  
               #root   /data/zwlsshop/www;  
            proxy_pass        http://shopcluster;         
                index  index.jsp index.html index.htm;  
            }  
              
            location ~* \.(gif|jpg|png|js|css)$ {  
            access_log off;  
             #  root   /data/zwlsshop/www;  
                expires 2h;  
            proxy_cache cache_one;  
            proxy_cache_key $host$uri$is_args$args;  
            proxy_cache_valid any 1h;  
            if ( !-f \$request_filename) {  
        #    access_log logs/imgaccess.log;  
                    proxy_pass http://shopcluster;  
                 }  
            }  
      
      
    #        location = /test/cachetest.jsp {  
    #            access_log logs/cachetestall.log;  
             #  root   /data/zwlsshop/www;  
    #            expires 2h;  
    #            proxy_cache cache_one;  
    #            proxy_cache_key $host$uri$is_args$args;  
    #            proxy_cache_valid any 1h;  
    #            if ( !-f \$request_filename) {  
    #                proxy_pass http://shopcluster;  
    #             }  
    #        }  
      
      
    #   location ~ /purge(/.*) {  
    #       proxy_cache_purge    cache_one   $host$1$is_args$args;  
    #     
    #  
    #   }  
      
      
        location /nginxs {  
           stub_status        on;   
               access_log         off;  
        }  
      
            #error_page  404              /404.html;  
      
            # redirect server error pages to the static page /50x.html  
            #  
            error_page   500 502 503 504  /50x.html;  
            location = /50x.html {  
                root   html;  
            }  
      
      
            # deny access to .htaccess files, if Apache's document root  
            # concurs with nginx's one  
            #  
            #location ~ /\.ht {  
            #    deny  all;  
            #}  
        }  
          
      
        server {  
            listen       80;  
            server_name localhost2 <a target="_blank" href="http://www.myroute.cn">www.myroute.cn  
      
    </a>        #charset utf-8;  
      
            #access_log  logs/host.access.log  main;  
      
            location / {  
               #root   /data/zwlsshop/www;  
                proxy_pass        http://hmfkcluster;  
                index  index.jsp index.html index.htm;  
            }  
      
    #   location ~* .+\.(gif|jpg|png|js|css)$ {  
    #          
    #       access_log off;  
             #  root   /data/zwlsshop/www;  
    #            expires 2h;  
     #           proxy_cache cache_one;  
    #       proxy_cache_key $host$uri$is_args$args;  
     #           proxy_cache_valid any 1h;  
      #          if ( !-f \$request_filename) {  
            #        access_log logs/imgaccess.log;  
       #             proxy_pass http://hmfkcluster;  
        #         }  
      
      
    #   }  
      
      
      
            #error_page  404              /404.html;  
      
            # redirect server error pages to the static page /50x.html  
            #  
            error_page   500 502 503 504  /50x.html;  
            location = /50x.html {  
                root   html;  
            }  
      
      
            # deny access to .htaccess files, if Apache's document root  
            # concurs with nginx's one  
            #  
            #location ~ /\.ht {  
            #    deny  all;  
            #}  
        }  
    }  

tomcat只需要改动一点,即在conf文件夹下的context.xml添加下面语句

在contex里面添加:
[html] view plaincopyprint?

    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
          memcachedNodes="m1:127.0.0.1:11211"  
          sticky="false"  
          lockingMode="auto"  
          sessionBackupAsync="false"  
          sessionBackupTimeout="1000"  
          transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  
    gt;  

其中memcachedNodes指定memcache的ip地址以及端口。sticky指定是否在tomcat中存储session做缓存。可以做两个memcach服务器,以备份session。

0 0
原创粉丝点击