3

Nginx的负载均衡(调度算法)及容错处理

 2 years ago
source link: https://blog.51cto.com/phpme/5081309
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx的负载均衡(调度算法)及容错处理

原创

hgditren 2022-03-08 10:48:11 博主文章分类:服务架构 ©著作权

文章标签 服务器 php nginx 负载均衡 文章分类 软件架构 软件研发 阅读数775

upstream模块,将使nginx跨越单机的限制,完成网络数据的接收、处理和转发,具备了实现负载均衡的能力。

nginx常用的负载均衡调度算法

1. 轮询(请求被依次分配到上游服务器)
upstream swoole_server
{
	server 192.168.1.10:9501;
	server 192.168.1.11:9501;
	server 192.168.1.12:9501;
}
2. 加权轮询(轮询的同时,权重高一点的上游服务器被分配的请求多一些)
upstream swoole_server
{
	server 192.168.1.10:9501 weigth=10; 
	server 192.168.1.11:9501 weigth=6;
	server 192.168.1.12:9501 weigth=8;
}
3. ip hash(可以保证同一IP的请求被转发到同一台上游服务器)
upstream swoole_server
{
	ip_hash;
	server 192.168.1.10:9501; 
	server 192.168.1.11:9501;
	server 192.168.1.12:9501;
}
4. url hash(可以保证相同的url的请求被转发到同一台上游服务器)
upstream swoole_server
{
	url_hash;
	server 192.168.1.10:9501; 
	server 192.168.1.11:9501;
	server 192.168.1.12:9501;
}
5. 最少连接数(连接处理少的上游服务器会被优先分配请求进行处理)
upstream swoole_server
{
	least_conn;
	server 192.168.1.10:9501; 
	server 192.168.1.11:9501;
	server 192.168.1.12:9501;
}
6. upstream-fair第三方服务均衡调度模块

参见: https://github.com/gnosek/nginx-upstream-fair

upstream swoole_server
{
	fair;
	server 192.168.1.10:9501; 
	server 192.168.1.11:9501;
	server 192.168.1.12:9501;
}
......
7. 其他自定义调度算法(按照自定义规则评分)
常用规则如下:
1.cpu占用
2.内存占用
3.连接数
......

负载均衡的容错处理

通过upstream 模块中 server 指令的指令值参数 max_fails 及 fail_timeout 实现对被代理(上游)服务器的检测、熔断和重试。

upstream 失败重试机制
  • max_fails 最大失败次数
  • fail_timeout 在指定时间内失败

在fail_timeout时间内失败了max_fails次数,则认为upstream上游服务器不可用,然后在这段时间(fail_timeout秒)内请求不会被转发到此台服务器上(此服务器被熔断fail_timeout秒),过了fail_timeout时间后,会再次进行重试。

示例如下:

upstream swoole_server
{
	#默认轮询
	server 192.168.1.10:9501 max_fails=2 fails_timeout=20s; 
	server 192.168.1.11:9501 max_fails=2 fails_timeout=20s;
	server 192.168.1.12:9501 max_fails=2 fails_timeout=20s;
}

#server 192.168.1.12:9501 max_fails=2 fails_timeout=20s;
#在20s内出现两次失败,则该台上游服务器会被熔断20s,20s后会再次重试转发请求给该台服务器。
更精确的配置出错重试
  • proxy_next_upstream 指定什么情况下才进行重试
  • proxy_next_upstream_tries 指定失败后进行重试的累积次数(默认0表示不限制次数)
  • proxy_connect_timeout nginx连接后端的超时时间
  • proxy_read_timeout 连接成功后,后端服务器的响应时间(代理接收超时时间)
  • proxy_send_timeout #后端服务器的回传时间(代理发送超时时间)
location /server {
	
	#指定什么情况下才进行重试
	proxy_next_upstream error timeout http_500;
	
	#指定失败后进行重试的累积次数(0表示不限制)
	proxy_next_upstream_tries  0;
	

	#超时相关设置

	proxy_connect_timeout 60s; #nginx连接后端的超时时间
    proxy_read_timeout 60s; #连接成功后,后端服务器的响应时间(代理接收超时时间)
    proxy_send_timeout 60s; #后端服务器的回传时间(代理发送超时时间)

	proxy_pass http://swoole_server;
}

特定场景下负载均衡下存在的问题
#upstream负载均衡的系统默认配置
proxy_next_upstream error timeout; #默认是error错误和timeout超时
proxy_next_upstream_tries  0;

如果上游服务器的最大执行时间为100s,nginx的代理接收响应时间为60s。
此时进行的重试机制,则存在问题。

假如:张三进行充值操作,请求发给上游服务器A,A执行时间为100s,proxy_read_timeout为60s
于是负载均衡重试充值请求,将请求发给上游服务器B,再次进行充值。最后导致张三充值了两次。
  • 打赏
  • 1收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK