网站建设证有,做网站创业风险分析,福建省建设厅官方网站,怎么建设课程网站前言
其它项目组需要调用接口#xff0c;添加接口限流#xff0c;防止项目被狂掉宕机。生产用了openresty#xff0c;所以在openresty上添加按接口限流#xff0c;同时#xff0c;需按照不同接口有不同的限流规则#xff0c;使用openresty中内置的漏桶算法方式限流。
漏…前言
其它项目组需要调用接口添加接口限流防止项目被狂掉宕机。生产用了openresty所以在openresty上添加按接口限流同时需按照不同接口有不同的限流规则使用openresty中内置的漏桶算法方式限流。
漏桶算法
漏桶算法思路简单水请求先进入到漏桶里漏桶以一定的速度出水当水流入速度过大会直接溢出可以看出漏桶算法能强行限制数据的传输速率。
通俗解释。
比如设置rate为100burst为50即允许1s放进来100个请求桶大小为50。
那么1s内
第1-100个请求会访问成功。
第101-150个请求会进入burst。
第150个请求之后的会直接失败返回。
openresty的说明文档https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/req.md
限流配置
使用OpenResty的漏桶算法进行限流配置不同接口配置不同的标准所以测试了两个接口test1和test2
主要分两步
添加限流使用的lua脚本在反向代理中配置限流的lua脚本
ps: 因为大多数使用情况还是会反向代理所以直接在反向代理中配置lua
添加lua脚本01和02的区别仅限于漏桶的参数配置不同
添加lua脚本01
在 lualib\utils 路径下创建lua脚本
lua脚本内容
-- utils/limit_req_leaky_bucket.lua
local limit_req require resty.limit.req-- rate: 5/s即为每秒3个请求增加桶容量为1/s超过5/s不到(51)/s的delay排队等候
local lim, err limit_req.new(my_limit_req_store1, 5, 1)
if not lim thenngx.log(ngx.ERR, failed to instantiate a resty.limit.req object: , err)return ngx.exit(500)
endlocal _M {}function _M.incoming()local key ngx.var.binary_remote_addrlocal delay, err lim:incoming(key, true)if not delay thenif err rejected thenreturn ngx.exit(503) -- 超过的请求直接返回503endngx.log(ngx.ERR, failed to limit req: , err)return ngx.exit(500)end-- 此方法返回当前请求需要delay秒后才会被处理和他前面对请求数-- 所以此处对桶中请求进行延时处理让其排队等待就是应用了漏桶算法-- 此处也是与令牌桶的主要区别if delay 0.001 thenngx.sleep(delay)end
endreturn _M添加lua脚本02
在 lualib\utils 路径下创建lua脚本
lua脚本内容
-- utils/limit_req_leaky_bucket.lua
local limit_req require resty.limit.req-- rate: 3/s即为每秒3个请求增加桶容量为1/s超过3/s不到(31)/s的delay排队等候
local lim, err limit_req.new(my_limit_req_store2, 3, 1)
if not lim thenngx.log(ngx.ERR, failed to instantiate a resty.limit.req object: , err)return ngx.exit(500)
endlocal _M {}function _M.incoming()local key ngx.var.binary_remote_addrlocal delay, err lim:incoming(key, true)if not delay thenif err rejected thenreturn ngx.exit(503) -- 超过的请求直接返回503endngx.log(ngx.ERR, failed to limit req: , err)return ngx.exit(500)end-- 此方法返回当前请求需要delay秒后才会被处理和他前面对请求数-- 所以此处对桶中请求进行延时处理让其排队等待就是应用了漏桶算法-- 此处也是与令牌桶的主要区别if delay 0.001 thenngx.sleep(delay)end
endreturn _M在nginx.conf中添加配置文件
# --- 限流 ---
worker_processes 1;events {worker_connections 1024;
}
# ------------http {# --- 反向代理 ---include /etc/nginx/conf.d/*.conf;# -----------------------include mime.types;default_type application/octet-stream;sendfile on;#keepalive_timeout 0;keepalive_timeout 65;# --- 限流 ---lua_code_cache on;# 共享内存lua_shared_dict my_limit_req_store1 100M;lua_shared_dict my_limit_req_store2 100M;# -----------# --- 反向代理 ---upstream backend_server {server 127.0.0.1:8080;}# -----------------------server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /test1 {# --- 限流 ---access_by_lua_block {local limit_count require utils.limit_req_leaky_bucket1-- 对于内部重定向或子请求不进行限制。因为这些并不是真正对外的请求。if ngx.req.is_internal() thenreturnendlimit_count.incoming()}# ------------# --- 反向代理 ---# 如果内容源是反向代理proxy_pass http://backend_server;proxy_set_header Host $host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 60;proxy_read_timeout 600;proxy_send_timeout 600;# -----------------------}location /test2 {# --- 限流 ---access_by_lua_block {local limit_count require utils.limit_req_leaky_bucket2-- 对于内部重定向或子请求不进行限制。因为这些并不是真正对外的请求。if ngx.req.is_internal() thenreturnendlimit_count.incoming()}# ------------# --- 反向代理 ---# 如果内容源是反向代理proxy_pass http://backend_server;proxy_set_header Host $host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 60;proxy_read_timeout 600;proxy_send_timeout 600;# -----------------------}}}
参考
http://www.guanshanw.com/post/67951.html