经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
理解Nginx负载均衡
来源:cnblogs  作者:gcdd1993  时间:2021/5/6 17:39:54  对本文有异议

准备

服务器

搭建三台用于测试的虚拟机

名称 IP 服务
node01 192.168.198.131 Nginx、模拟业务(8080)
node02 192.168.198.130 模拟业务(8080)
node03 192.168.198.132 模拟业务(8080)

修改hostnamehosts

  1. $ vim /etc/hosts
  2. 192.168.198.131 node01
  3. $ vim /etc/hostname
  4. node01
  5. $ reboot
  6. ## 其余两台也改下,并重启使配置生效

在node01上安装Nginx服务

  1. $ echo -e "deb http://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx\ndeb-src http://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
  2. $ wget -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
  3. $ sudo apt-get update
  4. $ sudo apt-get install nginx

模拟业务

使用https://start.spring.io快速新建Spring Boot项目,添加Web模块,并编写以下代码:

  1. @RestController
  2. @RequestMapping("/test")
  3. public class DemoController {
  4. @GetMapping
  5. public String test() throws UnknownHostException {
  6. return "this is : " + Inet4Address.getLocalHost();
  7. }
  8. }

打包并部署到服务器,我使用的是The Application Plugin,部署完毕启动

测试下:

  1. ## node01
  2. $ curl 192.168.198.131:8080/test
  3. ...
  4. this is : node01/192.168.198.131
  5. ## node02
  6. $ curl 192.168.198.130:8080/test
  7. ...
  8. this is : node02/192.168.198.130
  9. ## node03
  10. $ curl 192.168.198.132:8080/test
  11. ...
  12. this is : node03/192.168.198.132

Nginx负载均衡

Round Robin(轮询)

请求在服务器之间均匀分布,可以设置服务器权重

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. server 192.168.198.131:8080;
  4. server 192.168.198.132:8080;
  5. server 192.168.198.130:8080;
  6. }
  7. server {
  8. listen 80;
  9. server_name 192.168.198.131;
  10. location / {
  11. proxy_pass http://backend;
  12. }
  13. }
  14. $ service nginx restart

测试下

  1. $ curl 192.168.198.131/test
  2. ...
  3. this is : node01/192.168.198.131 # node01
  4. $ curl 192.168.198.131/test
  5. ...
  6. this is : node03/192.168.198.132 # node03
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node03/192.168.198.130 # node02
  10. $ curl 192.168.198.131/test
  11. ...
  12. this is : node01/192.168.198.131 # node01

可以看到,每台服务器访问到的次数是相等的。

Least Connections

请求分配到连接数最少的服务器,可以设置服务器权重

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. least_conn;
  4. server 192.168.198.131:8080;
  5. server 192.168.198.132:8080;
  6. server 192.168.198.130:8080;
  7. }
  8. $ service nginx restart

这个不知道怎么模拟出连接数最少场景。

IP Hash

从客户端的IP地址来确定请求应该发送给哪台服务器。在这种情况下,使用IPv4地址的前三个八位字节或整个IPv6地址来计算散列值。该方法能保证来自同一地址的请求分配到同一台服务器,除非该服务器不可用。

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. ip_hash;
  4. server 192.168.198.131:8080;
  5. server 192.168.198.132:8080;
  6. server 192.168.198.130:8080;
  7. }
  8. $ service nginx restart

测试下

  1. $ curl 192.168.198.131/test
  2. ...
  3. this is : node01/192.168.198.131
  4. $ curl 192.168.198.131/test
  5. ...
  6. this is : node01/192.168.198.131
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node01/192.168.198.131

可以看到,请求都被分配到node01节点。

接下来,将node01节点关闭,看看会发生什么:

  1. $ ps -ef | grep demo
  2. root 3343 1764 0 11:52 pts/0 00:00:23 java -jar /home/demo/demo-boot-0.0.1-SNAPSHOT/lib/demo-0.0.1-SNAPSHOT.jar
  3. root 4529 1764 0 13:11 pts/0 00:00:00 grep --color=auto demo
  4. $ kill -9 3343
  5. $ ps -ef | grep demo
  6. root 4529 1764 0 13:11 pts/0 00:00:00 grep --color=auto demo
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node03/192.168.198.132
  10. $ curl 192.168.198.131/test
  11. ...
  12. this is : node03/192.168.198.132
  13. $ curl 192.168.198.131/test
  14. ...
  15. this is : node03/192.168.198.132

由于node01节点不可用,请求都被分配到node03节点。

Generic Hash

与上面的IP_HASH类似,通用HASH按照用户定义的参数来计算散列值,参数可以是文本字符串,变量或组合。例如,参数可以是远端地址:

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. hash $remote_addr consistent;
  4. server 192.168.198.131:8080;
  5. server 192.168.198.132:8080;
  6. server 192.168.198.130:8080;
  7. }
  8. $ service nginx restart

测试下

  1. $ curl 192.168.198.131/test
  2. ...
  3. this is : node02/192.168.198.130
  4. $ curl 192.168.198.131/test
  5. ...
  6. this is : node02/192.168.198.130
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node02/192.168.198.130

可以看到,请求都被分配到了node02节点。

??上面的consistent是可选参数,如果设置了,将采用Ketama一致性hash算法计算散列值。

关于一致性Hash,可以查看我的另一篇博客:理解一致性Hash算法

Random

请求会被随机分配到一台服务器,可以设置服务器权重

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. random;
  4. server 192.168.198.131:8080;
  5. server 192.168.198.132:8080;
  6. server 192.168.198.130:8080;
  7. }
  8. $ service nginx restart

测试下

  1. $ curl 192.168.198.131/test
  2. ...
  3. this is : node03/192.168.198.132
  4. $ curl 192.168.198.131/test
  5. ...
  6. this is : node01/192.168.198.131
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node02/192.168.198.130
  10. $ curl 192.168.198.131/test
  11. ...
  12. this is : node01/192.168.198.130

可以看到,请求是被随机分配到三台服务器的。

Weights

除了设置负载均衡算法,我们还可以为服务器设置权重,权重默认值是1

  1. $ vim /etc/nginx/conf/demo.conf
  2. upstream backend {
  3. server 192.168.198.131:8080 weight=5;
  4. server 192.168.198.132:8080 weight=10;
  5. server 192.168.198.130:8080;
  6. }
  7. $ service nginx restart

测试下

  1. $ curl 192.168.198.131/test
  2. ...
  3. this is : node03/192.168.198.132
  4. $ curl 192.168.198.131/test
  5. ...
  6. this is : node01/192.168.198.131
  7. $ curl 192.168.198.131/test
  8. ...
  9. this is : node03/192.168.198.132
  10. $ curl 192.168.198.131/test
  11. ...
  12. this is : node03/192.168.198.132
  13. $ curl 192.168.198.131/test
  14. ...
  15. this is : node01/192.168.198.131

可以看到,5次请求中,node03(weight=10)占了3次,node01(weight=5)占了2次,node02(weight=1)1次都没有。

理论上来说,上面的配置,访问16次,node03应被分配10次,node01应被分配5次,node02应被分配1次。

参考资料

http-load-balancer

原文链接:http://www.cnblogs.com/gcdd1993/p/12292443.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号