【愚公系列】2022年03月 .NET架构班 030-分布式中间件 Nginx多场景使用问题分析之限流
一、限流
限流可以认为服务降级的一种,限流就是限制系统的输入和输出流量已达到保护系统 的目的。一般来说系统的吞吐量是可以被测算的,为了保证系统的稳定运行,一旦达到的需要限制的阈值,就需要限制流量并采取一些措施以完成限制流量的目的。比如:延迟处理,拒绝处理,或者部分拒绝处理等等。
常用的限流算法有:计数法,滑动窗口计数法,漏桶算法和令牌桶算法。
二、Nginx多场景使用问题分析之限流
1.Nginx限流
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果转发到5001和5002,两个实例处理能力达到极限的时候,导致系统可能宕机的风险。所以,在有限资源的情况下。我们应如何保证系统不宕机?
解决方案:限流
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_conn_zone $server_name zone=perserver:10m;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_conn perserver 1;
proxy_pass http://YDT.EBusiness;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
2.Nginx客户端限流
缺陷:服务端被限制只能允许访问10个请求,那么就会限制请求数量进行访问,请求数可能来至于多个客户端,如果一个客户端都把请求数占用了。就会导致其他客户端无法进行请求,导致恶意攻击。
方案:客户端限流
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_conn_zone $binary_remote_addr zone=perserver:10m;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_conn perserver 1;
proxy_pass http://YDT.EBusiness;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
3.客户端IP平滑限流
缺陷:如果客户端非常多,每个IP都限制请求处理1次,如果出现了100万个客户端,那么就有非常多的客户端徐亚需要进行处理,那么就会导致系统被压垮。所以,这个时候,就需要使用平滑处理
方案:客户端IP平滑限流,意思是1000r/s,相当于1ms处理一个
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_req_zone $binary_remote_addr zone=addr:10m rate=2r/s;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_req zone=addr;
proxy_pass http://YDT.EBusiness;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
原理:令牌桶算法、漏桶算法
4.客户端IP平滑限流-突发
缺陷:如果客户端请求非常多,1s处理2个是 每500ms 处理一个,就会导致并发性能下降。这个时候把不能处理的,排除掉,然后再来处理。所以需要加缓存来处理。缓冲几个,以提升性能。
方案:客户端IP平滑限流-突发
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_req_zone $binary_remote_addr zone=addr:10m rate=2r/s;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_req zone=addr burst=5;
proxy_pass http://YDT.EBusiness;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
5.客户端IP平滑限流-立即突发
缺陷:如果客户端请求非常多,1s处理2个是 每500ms 处理一个,就会导致并发性能下降。这个时候把不能处理的,排除掉,然后再来处理。所以需要加缓存来处理。缓冲几个,以提升性能。
方案:客户端IP平滑限流-立即突发
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_req_zone $binary_remote_addr zone=addr:10m rate=2r/s;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
limit_req zone=addr burst=5 nodelay;
proxy_pass http://YDT.EBusiness;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
- 点赞
- 收藏
- 关注作者
评论(0)