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

 

目录(?)[+]html

 

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

 

基本思路

 

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

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

假设,咱们如今有三台机器,各台服务器以下所示:ubuntu

[html]  view plain copy
 
  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代理响应的搜索结果。浏览器

 

配置资源

 

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

 

服务器IP:端口服务器

软件配置session

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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  1. /home/shirdrn/servers/nginx-1.0.8/sbin/nginx  

另外一种是,nginx.conf配置能够放到其余目录下面,启动时经过-c选项指定配置文件路径便可,启动命令以下所示:

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

能够查看启动进程:

[html]  view plain copy
 
  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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  1. http://192.168.0.184:8888/solr/core0/search/?q=普吉岛&start=0&rows=10  

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

[html]  view plain copy
 
  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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  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

相关文章
相关标签/搜索