首页 > 其他 > 详细

nginx负载均衡搭建及测试

时间:2017-01-12 18:29:42      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:日志格式   int   ber   tcp   ubunt   blog   明显   response   lis   

ok 首先你得有一个Ubuntu或者什么其他的linux.
安装jdk  tomcat  nginx 等.. 记得配置环境变量

1配置tomcat

Linux系统下怎样配置多个Tomcat同时运行呢,首先修改变量为第一个tomcat,然后修改第二个tomcat启动的脚本
如何在同一系统里同时启动多个Tomcat.
配置环境变量vi /etc/profile
在最后加上配置, 比如这里加了3台tomcat 
  1. #--------------------------------------------------------------------
  2. #set tomcat path @olddoor
  3. ### first tomcat ###
  4. CATALINA_BASE=/usr/apache/tomcat-7-1
  5. CATALINA_HOME=/usr/apache/tomcat-7-1
  6. TOMCAT_HOME=/usr/apache/tomcat-7-1
  7. export CATALINA_BASE CATALINA_HOME TOMCAT_HOME
  8. ### second tomcat ###
  9. CATALINA_2_BASE=/usr/apache/tomcat-7-2
  10. CATALINA_2_HOME=/usr/apache/tomcat-7-2
  11. TOMCAT_2_HOME=/usr/apache/tomcat_7-2
  12. export CATALINA_2_BASE CATALINA_2_HOME TOMCAT_2_HOME
  13. ### third tomcat ###
  14. CATALINA_3_BASE=/usr/apache/tomcat-7-3
  15. CATALINA_3_HOME=/usr/apache/tomcat-7-3
  16. TOMCAT_HOME=/usr/apache/tomcat-7-3
  17. export CATALINA_3_BASE CATALINA_3_HOME TOMCAT_3_HOME
  18. #set java path--------------------------------------------------------
  19. JAVA_HOME=/usr/java/jdk1.7
  20. export JRE_HOME=$JAVA_HOME/jre
  21. export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
  22. export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
3台tomcat的端口号记得在server.xml修改.
然后修改tomcat的bin目录下的catalina.sh, 加入代码
以tomcat2为例
  1.   export JAVA_HOME=
  2.   export PATH=
  3.   export CLASSPATH=
  4.   ###以上三个环境变量就是JAVA的环境变量可以不写###
  5.   export CATALINA_BASE=$CATALINA_2_BASE #配置对应的tomcat变量即可
  6.   export CATALINA_HOME=$CATALINA_2_HOME
搞定. 启动3个tomcat试一下效果.
默认访问的tomcat-1的index.jsp如下
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  4. <title>Insert title here</title>
  5. </head>
  6. <body>
  7. hello<br/>
  8. <img id="img1" src=/test/static/404-1.jpg />
  9. <img id="img1" src=/test/jpgs/123.jpg />
  10. </body>
  11. </html>
tomcat-2 和tomcat-3的index.jsp 中的404图片名字分别为404-2.jsp 和404-3.jpg
最后访问效果
tomcat-1:
技术分享
 
tomcat-2
技术分享
 
tomcat3
技术分享
tomcat2和tomcat3的webapps目录下并未放置jpgs/123.jpg图片所以找不到.

2配置nginx

修改nginx.conf
  1. #user nobody; # user root root #Nginx所在的用户和用户组
  2. user root root; #Nginx所在的用户和用户组
  3. worker_processes 1;# 启动的工作进程数量
  4. #错误日志存放路径
  5. #error_log logs/error.log;
  6. #error_log logs/error.log notice;
  7. #error_log logs/error.log info;
  8. error_log logs/error.log info; #add by olddoor-----------
  9. #pid logs/nginx.pid;
  10. #add by olddoor-----------
  11. #add end------------------
  12. events {
  13. worker_connections 1024;
  14. }
  15. http {
  16. include mime.types;
  17. default_type application/octet-stream;
  18. #定义日志格式
  19. #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
  20. # ‘$status $body_bytes_sent "$http_referer" ‘
  21. # ‘"$http_user_agent" "$http_x_forwarded_for"‘;
  22. #access_log logs/access.log main;
  23. sendfile on;
  24. #tcp_nopush on;
  25. #keepalive_timeout 0;
  26. keepalive_timeout 65;
  27. #gzip on;
  28. #add by olddor---------------------
  29. upstream localhost {
  30. #发到localhost上的请求,通过Nginx转发到实际处理请求的服务器
  31. server 192.168.64.132:8080 weight=1;
  32. server 192.168.64.132:8081 weight=1;
  33. server 192.168.64.132:8082 weight=1;
  34. }
  35. #add by olddor over----------------
  36. server {
  37. listen 80;
  38. server_name localhost;
  39. #charset koi8-r;
  40. #access_log logs/host.access.log main;
  41. location / {
  42. proxy_connect_timeout 3; #add by olddoor
  43. proxy_send_timeout 30; #add by olddoor
  44. proxy_read_timeout 30; #add by olddoor
  45. proxy_pass http://localhost; #add by olddoor
  46. #root html; #ignore by olddoor
  47. #index index.html index.htm; #ignore by olddoor
  48. }
  49. location /test/jpgs/ {
  50. alias /data/imgs/;
  51. }
  52. #location /jpgs
  53. #error_page 404 /404.html;
  54. # redirect server error pages to the static page /50x.html
  55. #
  56. error_page 500 502 503 504 /50x.html;
  57. location = /50x.html {
  58. root html;
  59. }
  60. # deny access to .htaccess files, if Apache‘s document root
  61. # concurs with nginx‘s one
  62. #
  63. #location ~ /\.ht {
  64. # deny all;
  65. #}
  66. }
  67. # another virtual host using mix of IP-, name-, and port-based configuration
  68. #
  69. #server {
  70. # listen 8000;
  71. # listen somename:8080;
  72. # server_name somename alias another.alias;
  73. # location / {
  74. # root html;
  75. # index index.html index.htm;
  76. # }
  77. #}
  78. # HTTPS server
  79. #
  80. #server {
  81. # listen 443;
  82. # server_name localhost;
  83. # ssl on;
  84. # ssl_certificate cert.pem;
  85. # ssl_certificate_key cert.key;
  86. # ssl_session_timeout 5m;
  87. # ssl_protocols SSLv2 SSLv3 TLSv1;
  88. # ssl_ciphers HIGH:!aNULL:!MD5;
  89. # ssl_prefer_server_ciphers on;
  90. # location / {
  91. # root html;
  92. # index index.html index.htm;
  93. # }
  94. #}
  95. }
通过
 server 192.168.64.132:8080 weight=1;
 server 192.168.64.132:8081 weight=1;
 server 192.168.64.132:8082 weight=1;
的配置完成3台tomcat的轮询负载均衡.
访问时候使用 192.168.64.132/test 取代192.168.64.132:8080/test , 192.168.64.132:8081/test , 192.168.64.132:8082/test

3 使用nginx管理部分静态数据

配置方式可以直接拦截指定路径的 jpg之类的文件.
这里只拦截目录
location  /test/jpgs/ {
            alias /data/imgs/;  #即拦截  /test/jpgs/请求->匹配物理路径:    /data/imgs/
        }
注意alias 和root 的拦截逻辑略有不同.

在linux服务器上创建对应的文件夹并上传图片123.jpg

继续通过192.168.64.132:8080/test访问可以看到效果是:
技术分享
 
两张图片的来源都是tomcat-1
技术分享
 
而访问192.168.64.132:80/test 使用nginx服务则
随机显示第一张图片. 而第二张123.jpg 则显示的是nginx指定目录的图片而非tomcat上的图片.
技术分享
 
或者是
技术分享
 
2张图片来源都是nginx, 只是第一个是nginx到tomcat上获取. 第二张图片被拦截到/data/imgs上获取.
技术分享
 

4 性能测试对比

使用ab工具的压力测试方法和结果,ab是针对apache的性能测试工具,可以只安装ab工具。
ubuntu安装ab
  1. apt-get install apache2-utils
centos安装ab
  1. yum install httpd-tools
通过命令测试:
ab -kc 1000 -n 1000 http://192.168.64.132:8080/test/
这个指令会使用1000个并发,进行连接1000次测试tomcat-1
ab -kc 1000 -n 1000 http://192.168.64.132:80/test/
这个指令会使用1000个并发,进行连接1000次测试nginx
对比结果:
8080
  1. #用于描述每个请求处理时间的分布情况,例如:50% 28 50%请求处理时间不超过28毫秒 (这里所指的处理时间是指:Time per request )
  2. Percentage of the requests served within a certain time (ms)
  3. 50% 28
  4. 66% 32
  5. 75% 34
  6. 80% 35
  7. 90% 53
  8. 95% 62
  9. 98% 65
  10. 99% 66
  11. 100% 1823 (longest request)
  12. second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。 Requests per second: 547.04 [#/sec] (mean) Time per request: 1828.016 [ms] (mean) Time per request: 1.828 [ms] (mean, across all concurrent requests) Transfer rate: 76.93 [Kbytes/sec] received
80   nginx在小规模请求下响应速度慢于直接请求tomcat, 而处理能力nginx明显强于tomcat
  1. Percentage of the requests served within a certain time (ms)
  2. 50% 107
  3. 66% 201
  4. 75% 243
  5. 80% 257
  6. 90% 334
  7. 95% 360
  8. 98% 1116
  9. 99% 1117
  10. 100% 1118 (longest request)
  11. second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。 Requests per second: 873.38 [#/sec] (mean) Time per request: 1144.983 [ms] (mean) Time per request: 1.145 [ms] (mean, across all concurrent requests) Transfer rate: 216.83 [Kbytes/sec] received

当请求改为1000个并发,进行连接1000次后
8080已经无法及时响应
  1. This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
  2. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  3. Licensed to The Apache Software Foundation, http://www.apache.org/
  4. Benchmarking localhost (be patient)
  5. Total of 9967 requests completed
而80还是稳稳的
  1. Server Software: nginx/1.4.2
  2. Server Hostname: localhost
  3. Server Port: 80
  4. Concurrency Level: 1000
  5. Time taken for tests: 1.145 seconds
  6. Complete requests: 1000
  7. Failed requests: 551
  8. (Connect: 0, Receive: 0, Length: 551, Exceptions: 0)
  9. Non-2xx responses: 1000
  10. Keep-Alive requests: 0
  11. Total transferred: 254220 bytes
  12. HTML transferred: 105792 bytes
  13. second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。
  14. Requests per second: 873.38 [#/sec] (mean)
  15. Time per request: 1144.983 [ms] (mean)
  16. Time per request: 1.145 [ms] (mean, across all concurrent requests)
  17. Transfer rate: 216.83 [Kbytes/sec] received
  18. Connection Times (ms)
  19. min mean[+/-sd] median max
  20. Connect: 16 26 4.6 27 33
  21. Processing: 16 155 174.5 88 1101
  22. Waiting: 13 155 174.6 88 1101
  23. Total: 44 181 172.5 107 1118
  24. Percentage of the requests served within a certain time (ms)
  25. 50% 107
  26. 66% 201
  27. 75% 243
  28. 80% 257
  29. 90% 334
  30. 95% 360
  31. 98% 1116
  32. 99% 1117
  33. 100% 1118 (longest request)

tips 如需将屏幕上的测试结果生成为文本文件, 可以在测试时候配合|tee 命令
如:
  1. ab -kc 1000 -n 1000 http://192.168.64.132:80/test/ |tee /usr/111.txt #将结果保存到/usr/111.txt中






 







nginx负载均衡搭建及测试

标签:日志格式   int   ber   tcp   ubunt   blog   明显   response   lis   

原文:http://www.cnblogs.com/redcoatjk/p/6279225.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号