• Ubuntu下基于Nginx实现Tomcat集群负载均衡


     

    目录(?)[+]

     

    Nginx是一款HTTP和反向代理服务器,有关它的介绍可以到网上搜一下,很多很多,不再累述。这里,我们记录一下Nginx的安装过程,以及如何配置Nginx来实现Tomcat集群的负载均衡。

    基本思路

    假如现在我们有一个使用Java实现的Web搜索服务器,用户可以通过Web页面输入关键词,搜索服务器处理搜索请求并向用户展示搜索结果。如果用户访问量很大的话,我们的这台搜索服务器承受的压力会很大,很可能由于搜索服务器的处理能力达到上限,在某一个时刻无法再处理用户新到来的请求。所以,我们就考虑将用户请求的压力分散开,即在多台服务器上部署同一套搜索服务器程序,然后通过一个负载均衡策略,将请求的压力分摊在多台搜索服务器上,这样,在用户请求量很大的情况下,很好解决单台服务器的无法处理请求的问题。

    我们的想法就是,通过一台服务器做代理,使用负载均衡软件实现请求的代理转发,将用户的请求转发到多台搜索服务器上去处理,就能实现多台搜索服务器的负载均衡,而不致于单一服务器处理所有请求。

    假设,我们现在有三台机器,各台服务器如下所示:

    [html] view plaincopy
     
    1. 搜索服务器:192.168.0.174 RHEL 5  
    2. 搜索服务器:192.168.0.181 Win 7  
    3. 代理服务器:192.168.0.184 Ubuntu 11.04.1  

    通过使用Nginx做反向代理,安装在192.168.0.184上。另外两台服务器均为搜索服务器,并且都安装了Tomcat Web服务器软件,搜索服务器程序就部署在Tomcat中。服务器192.168.0.184接收搜索请求,并通过Nginx将请求转发到两台搜索服务器上进行处理,然后返回结果,通过Nginx代理响应的搜索结果。

    配置资源

    这里,说明一下我们各台服务器的软件配置,及其应用端口,如下所示:

     

    服务器IP:端口

    软件配置

    192.168.0.174:8080 OpenJDK 1.6.0_22, apache-tomcat-7.0.22.tar
    192.168.0.181:8080 Sun JDK 1.6.0_17, apache-tomcat-6.0.20.exe
    192.168.0.184:8888 nginx-1.0.8.targz, pcre-8.13.tar.gz

    安装配置过程

    • Nginx安装

     下载nginx-1.0.8.targz, pcre-8.13.tar.gz这两个安装包,并解压缩到目录/home/shirdrn/tools下面,然后安装过程如下所示:

    [html] view plaincopy
     
    1. cd /home/shirdrn/tools  
    2. tar -xvf pcre-8.13.tar.bz2  
    3. tar -xzvf nginx-1.0.8.tar.gz  
    4. cd /home/shirdrn/tools/nginx-1.0.8  
    5. ./configure --with-http_stub_status_module --prefix=/home/shirdrn/servers/nginx --with-pcre=/home/shirdrn/tools/pcre-8.13  
    6. make  
    7. make install  

    执行上述命令,需要使用超级用户权限,将我们的Nginx安装到/home/shirdrn/servers/nginx-1.0.8下面,由于指定了--with-pcre=/home/shirdrn/tools/pcre-8.13,即pcre的源码路径,在安装的过程首先编译pcre并安装,然后才开始配置安装Nginx。

    验证是否安装成功,只需要在浏览器输入http://192.168.0.184:8888/即可,默认Nginx使用80端口,这里我的80端口被占用了,所以修改为8888(有关Nginx的基本配置在下面说明)。

    • Ngin负载均衡配置

     下面,我们看一下我们实现Nginx负载均衡的配置。配置文件为conf/nginx.conf,由于我们进行的代理的配置,通过使用一个单独的代理配置文件conf/proxy.conf,在conf/nginx.conf中引入该代理配置即可。

    conf/proxy.conf的配置内容如下所示:

    [html] view plaincopy
     
    1. proxy_redirect          off;  
    2. proxy_set_header        Host $host;  
    3. proxy_set_header        X-Real-IP $remote_addr;  
    4. proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
    5. client_max_body_size    10m;  
    6. client_body_buffer_size 128k;  
    7. proxy_connect_timeout   300;  
    8. proxy_send_timeout      300;  
    9. proxy_read_timeout      300;  
    10. proxy_buffer_size       4k;  
    11. proxy_buffers           4 32k;  
    12. proxy_busy_buffers_size 64k;  
    13. proxy_temp_file_write_size 64k;  

    各配置项的含义,可以通过查阅相关文档了解。下面看conf/nginx.conf的配置,我们根据实践操作所做的修改,并对相关基本配来做适当的说明,如下所示:

    [html] view plaincopy
     
    1. user  root root; # Nginx所在的用户和用户组  
    2.   
    3. worker_processes  3; # 启动的工作进程数量  
    4.   
    5. #error_log  logs/error.log;  
    6. #error_log  logs/error.log  notice;  
    7. #error_log  logs/error.log  info;  
    8.   
    9. pid        logs/nginx.pid; # Nginx进程ID  
    10.   
    11.   
    12. events {  
    13.     worker_connections  1024;  
    14. }  
    15.   
    16.   
    17. http {  
    18.     include       mime.types;  
    19.     default_type  application/octet-stream;  
    20.   
    21.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
    22.     #                  '$status $body_bytes_sent "$http_referer" '  
    23.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
    24.   
    25.     #access_log  logs/access.log  main;  
    26.   
    27.     sendfile        on;  
    28.     #tcp_nopush     on;  
    29.   
    30.     #keepalive_timeout  0;  
    31.     keepalive_timeout  65;  
    32.   
    33.     #gzip  on;  
    34.     upstream localhost { # 发到localhost上的请求,通过Nginx转发到实际处理请求的服务器  
    35.            server 192.168.0.181:8080 weight=1;  
    36.            server 192.168.0.184:8080 weight=1;  
    37.     }  
    38.   
    39.     server {  
    40.         listen       8888; # Nginx监听的端口,默认为80  
    41.         server_name  localhost; # Nginx所在主机的名称  
    42.   
    43.         #charset koi8-r;  
    44.   
    45.         #access_log  logs/host.access.log  main;  
    46.   
    47.         location / {  
    48.             root   html/solr; # 请求资源的路径(代理:/home/shirdrn/servers/nginx/tml/solr/,该目录下没有任何数据)  
    49.             index  index.html index.htm;  
    50.             proxy_pass   http://localhost; # 代理:对发送到localhost上请求进行代理  
    51.             include proxy.conf; # 引入proxy.conf配置  
    52.         }  
    53.   
    54.         #error_page  404              /404.html;  
    55.   
    56.         # redirect server error pages to the static page /50x.html  
    57.         #  
    58.         error_page   500 502 503 504  /50x.html;  
    59.         location = /50x.html {  
    60.             root   html;  
    61.         }  
    62.   
    63.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
    64.         #  
    65.         #location ~ .php$ {  
    66.         #    proxy_pass   http://127.0.0.1;  
    67.         #}  
    68.   
    69.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
    70.         #  
    71.         #location ~ .php$ {  
    72.         #    root           html;  
    73.         #    fastcgi_pass   127.0.0.1:9000;  
    74.         #    fastcgi_index  index.php;  
    75.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    76.         #    include        fastcgi_params;  
    77.         #}  
    78.   
    79.         # deny access to .htaccess files, if Apache's document root  
    80.         # concurs with nginx's one  
    81.         #  
    82.         #location ~ /.ht {  
    83.         #    deny  all;  
    84.         #}  
    85.     }  
    86.   
    87.   
    88.     # another virtual host using mix of IP-, name-, and port-based configuration  
    89.     #  
    90.     #server {  
    91.     #    listen       8000;  
    92.     #    listen       somename:8080;  
    93.     #    server_name  somename  alias  another.alias;  
    94.   
    95.     #    location / {  
    96.     #        root   html;  
    97.     #        index  index.html index.htm;  
    98.     #    }  
    99.     #}  
    100.   
    101.   
    102.     # HTTPS server  
    103.     #  
    104.     #server {  
    105.     #    listen       443;  
    106.     #    server_name  localhost;  
    107.   
    108.     #    ssl                  on;  
    109.     #    ssl_certificate      cert.pem;  
    110.     #    ssl_certificate_key  cert.key;  
    111.   
    112.     #    ssl_session_timeout  5m;  
    113.   
    114.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
    115.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
    116.     #    ssl_prefer_server_ciphers   on;  
    117.   
    118.     #    location / {  
    119.     #        root   html;  
    120.     #        index  index.html index.htm;  
    121.     #    }  
    122.     #}  
    123.   
    124. }  
    • 启动Nginx

    启动Nginx比较简单,有两种方式:一种是直接使用默认安装路径下的nginx.conf,启动命令如下所示:

    [html] view plaincopy
     
    1. /home/shirdrn/servers/nginx-1.0.8/sbin/nginx  

    另一种是,nginx.conf配置可以放到其他目录下面,启动时通过-c选项指定配置文件路径即可,启动命令如下所示:

    [html] view plaincopy
     
    1. /home/shirdrn/servers/nginx-1.0.8/sbin/nginx -c /home/shirdrn/servers/nginx-1.0.8/conf/nginx.conf  

    可以查看启动进程:

    [html] view plaincopy
     
    1. root@dev2:~$ ps -ef | grep nginx  
    2. root     15952     1  0 18:56 ?        00:00:00 nginx: master process sbin/nginx  
    3. root     15953 15952  0 18:56 ?        00:00:00 nginx: worker process  
    4. root     15954 15952  0 18:56 ?        00:00:00 nginx: worker process  
    5. root     15955 15952  0 18:56 ?        00:00:00 nginx: worker process  
    6. ubuntu   22988 22887  0 22:19 pts/0    00:00:00 grep --color=auto nginx  

    查看Nginx监听的端口号:

    [html] view plaincopy
     
    1. root@dev2:/home/ubuntu# netstat -nap | grep '8888'  
    2. tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      15952/nginx   
    • Tomcat配置

     无论在Windows下还是Linux下,Tomcat的配置无需做额外的配置,只要能够发布你的Web应用即可。但是,我们通过Nginx进行反向代理请求,必须保证多个Tomcat下的Web应用的请求路径相一致,例如,我们配置的两台搜索服务器的单独请求路径,分别如下所示:

    [html] view plaincopy
     
    1. http://192.168.0.174:8080/solr/core0/search/?q=九寨沟&start=0&rows=10  
    2. http://192.168.0.181:8080/solr/core0/search/?q=马尔代夫&start=0&rows=10  

    我们将我们的搜索服务器程序,分别发布到这两台搜索服务器的Tomcat上,可以通过实际接口进行单独接收请求并处理。

    测试验证

    下面,来测试一下,验证我们上述配置的内容,是否能够按照开始设计的思路,实现负载均衡。由于在Nginx的配置中,我们设置了两台搜索服务器处理请求的权重为1:1,所以在测试的过程中很容易就能看到。由于所有的请求都是先到达代理服务器,通过代理服务器进行转发,所以对外部只有一个统一的接口:

    [html] view plaincopy
     
    1. http://192.168.0.184:8888/solr/core0/search/?q=普吉岛&start=0&rows=10  

    根据代理配置,代理服务器只是转发请求和相应,而不做请求的处理等工作,所以负荷会小一些。通过负载分摊,两个搜索服务器的负荷也会比单台的情况下有所好转。上面代理的请求链接,实际会转换为如下两者之一:

    [html] view plaincopy
     
    1. http://192.168.0.174:8080/solr/core0/search/?q=普吉岛&start=0&rows=10  
    2. http://192.168.0.181:8080/solr/core0/search/?q=普吉岛&start=0&rows=10  

    我们看一下,各台服务器实际执行时的日志记录。如下4个请求:

    [html] view plaincopy
     
    1. http://192.168.0.184:8888/solr/core0/search/?q=九寨沟&start=0&rows=10  
    2. http://192.168.0.184:8888/solr/core0/search/?q=普吉岛&start=0&rows=10  
    3. http://192.168.0.184:8888/solr/core0/search/?q=马尔代夫&start=0&rows=10  
    4. http://192.168.0.184:8888/solr/core0/search/?q=西双版纳&start=0&rows=10  

    Nginx日志,如下所示:

    [html] view plaincopy
     
    1. 192.168.0.181 - - [05/Oct/2011:21:42:05 +0800] "GET /solr/core0/search/?q=九寨沟&start=0&rows=10 HTTP/1.1" 200 329495 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30"  
    2. 192.168.0.181 - - [05/Oct/2011:21:42:12 +0800] "GET /solr/core0/search/?q=普吉岛&start=0&rows=10 HTTP/1.1" 200 110349 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30"  
    3. 192.168.0.181 - - [05/Oct/2011:21:42:25 +0800] "GET /solr/core0/search/?q=马尔代夫&start=0&rows=10 HTTP/1.1" 200 85572 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30"  
    4. 192.168.0.181 - - [05/Oct/2011:21:42:32 +0800] "GET /solr/core0/search/?q=西双版纳&start=0&rows=10 HTTP/1.1" 200 141030 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30"  

    我们看一下,各台搜索服务器接收请求处理的日志:
    搜索服务器192.168.0.174请求处理日志:

    [html] view plaincopy
     
    1. 2011-10-5 21:42:12 org.apache.solr.core.SolrCore execute  
    2. 信息: [core0] webapp=/solr path=/search/ params={bq=spiderName:baseSeSpider^1.5&start=0&q=普吉岛&rows=10} hits=16 status=QTime=48   
    3. 2011-10-5 21:42:32 org.apache.solr.core.SolrCore execute  
    4. 信息: [core0] webapp=/solr path=/search/ params={bq=spiderName:baseSeSpider^1.5&start=0&q=西双版纳&rows=10} hits=29 status=QTime=54  

    搜索服务器192.168.0.181请求处理日志:

    [html] view plaincopy
     
    1. 2011-10-5 21:42:06 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    2. 信息: create new Seg ...  
    3. 2011-10-5 21:42:06 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    4. 信息: use complex mode  
    5. 2011-10-5 21:42:08 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    6. 信息: create new Seg ...  
    7. 2011-10-5 21:42:08 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    8. 信息: use complex mode  
    9. 2011-10-5 21:42:08 org.apache.solr.core.SolrCore execute  
    10. 信息: [core0] webapp=/solr path=/search/ params={bq=spiderName:baseSeSpider^1.5&start=0&q=九寨沟0&rows=10} hits=54 status=QTime=2116   
    11. 2011-10-5 21:42:27 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    12. 信息: create new Seg ...  
    13. 2011-10-5 21:42:27 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    14. 信息: use complex mode  
    15. 2011-10-5 21:42:28 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    16. 信息: create new Seg ...  
    17. 2011-10-5 21:42:28 com.chenlb.mmseg4j.solr.MMSegTokenizerFactory newSeg  
    18. 信息: use complex mode  
    19. 2011-10-5 21:42:29 org.apache.solr.core.SolrCore execute  
    20. 信息: [core0] webapp=/solr path=/search/ params={bq=spiderName:baseSeSpider^1.5&start=0&q=马尔代夫0&rows=10} hits=57 status=QTime=1721   

    测试发现,虽然只有4个请求,但是在负载权重相等情况下,很好地分摊到两台搜索服务器上去进行请求的实际处理。通过上面日志可以看出,我们基本实现了一开始设计的思路。

    From:     http://blog.csdn.net/shirdrn/article/details/6845520

  • 相关阅读:
    ES集群性能调优链接汇总
    【转】dmesg 时间转换
    广师大笔记汉诺塔
    广师大python学习笔记求派的值
    155. 最小栈(c++)
    160. 相交链表(c++)
    论文 数据集总结
    论文阅读 总结 复习
    121. 买卖股票的最佳时机(c++)
    9. 回文数(c++)
  • 原文地址:https://www.cnblogs.com/herd/p/5021668.html
Copyright © 2020-2023  润新知