百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

OpenResty+Lua限流实战_openresty 限流

off999 2025-02-16 22:27 36 浏览 0 评论

OpenResty+Lua限流实战

当业务量越来越大的时候,为了能保证服务的运行,限流是必不可少的!OpenResty是一个高性能网关

OpenResty? is a dynamic web platform based on NGINX and LuaJIT.

OpenResty = Nginx + Lua,Lua是高性能脚本语言,有着C语言的执行效率但是又比C简单,能很方便的扩展OpenResty 的功能。

Lua 是由巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组于1993年开发的一种轻量、小巧的脚本语言,用标准 C 语言编写,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。

官网:http://www.lua.org/

实战环境

docker + CentOS8 + Openresty 1.17.8.2

Lua限流模块

https://github.com/openresty/lua-resty-limit-traffic

Lua的库一般都是小巧轻便且功能都具备,这个限流库核心文件一共就四个,几百行代码就能实现限流功能,Lua的其他库也是这样,比如redis的库还是Http的库,麻雀虽小五脏俱全!

环境准备

docker run -dit --name gw ?--privileged centos /usr/sbin/init
docker exec -it gw bash 

在gw中

# 安装openresty
yum install -y yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install -y openresty

# 安装工具等
yum install -y net-tools vim telnet git httpd

# Openresty自带了lua-resty-limit-traffic组件,如果没有带,下载到/usr/local/openresty/lualib/resty/limit/文件夹即可
# 下载lua-resty-limit-traffic组件
[ `ls /usr/local/openresty/lualib/resty/limit/ | wc -l` = 0 ] && ?echo '请安装限速组件' || echo '已经安装限速组件'
# 安装了请忽略
cd ~ && git clone https://github.com/openresty/lua-resty-limit-traffic.git
mkdir -p /usr/local/openresty/lualib/resty/limit/
cp  lua-resty-limit-traffic/lib/resty/limit/*.lua /usr/local/openresty/lualib/resty/limit/

# 启动openresy
openresty

限并发

场景:按照 ip 限制其并发连

参考:
https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/lua-limit.html
https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/conn.md
https://developer.aliyun.com/article/759299

原理:lua_share_dict是nginx所有woker和lua runtime共享的,当一个请求来,往lua_share_dict记录键值对ip地址:1,当请求完成时再-1,再来一个在+1,设置一个上限5,当超过5时则拒绝请求,一定要注意内部重定向的问题!

  • OpenResty执行阶段 tag:lua执行流程;执行阶段;openresty执行流程
  • 为啥access_by_lua执行两次

环境搭建

新建utils/limit_conn.lua模块

mkdir -p /usr/local/openresty/lualib/utils
cat > /usr/local/openresty/lualib/utils/limit_conn.lua <= 0.001 then
 ? ? ?  ngx.log(ngx.WARN, "delaying conn, excess ", delay,
 ? ? ? ? ? ? ? ?"s per binary_remote_addr by limit_conn_store")
 ? ? ?  ngx.sleep(delay)
 ?  end
end

function _M.leaving()
 ?  local ctx = ngx.ctx
 ?  local key = ctx.limit_conn_key
 ? ?if key then
 ? ? ?  local latency = tonumber(ngx.var.request_time) - ctx.limit_conn_delay
 ? ? ?  local conn, err = limit:leaving(key, latency)
 ? ? ? ?if not conn then
 ? ? ? ? ?  ngx.log(ngx.ERR,
 ? ? ? ? ? ?"failed to record the connection leaving ",
 ? ? ? ? ? ?"request: ", err)
 ? ? ?  end
 ?  end
end

return _M

EOF

重点在于这句话local limit, limit_err = limit_conn.new("limit_conn_store", 8, 2, 0.05),允许的最大并发为常规的8个,突发的2个,一共8+2=10个并发,详情参考
https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/conn.md#new

被拒绝的请求直接返回503

if err == "rejected" then
 ? ?return ngx.exit(503) -- 超过的请求直接返回503
end

修改nginx配置文件

# 备份一下配置文件
cd /usr/local/openresty/nginx/conf/ && \cp nginx.conf nginx.conf.bak

# 添加配置
echo '' > /usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf

添加如下内容

worker_processes  1;

events {
 ? ?worker_connections  1024;
}
http {
 ? ?include ? ? ? mime.types;
 ? ?default_type  application/octet-stream;
 ? ?sendfile ? ? ?  on;
 ? ?keepalive_timeout  65;
 ? ?lua_code_cache on; ? ?
 ? 
 ? ?# 注意 limit_conn_store 的大小需要足够放置限流所需的键值。
 ? ?# 每个 $binary_remote_addr 大小不会超过 16 字节(IPv6 情况下),算上 lua_shared_dict 的节点大小,总共不到 64 字节。
 ? ?# 100M 可以放 1.6M 个键值对
 ? ?lua_shared_dict limit_conn_store 100M;
 ? ?
 ? ?server {
 ? ? ? ?listen 80;
 ? ? ? ?location / {
 ? ? ? ? ? ?access_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_conn = require "utils.limit_conn"
 ? ? ? ? ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ? ? ? ? ?if ngx.req.is_internal() then
 ? ? ? ? ? ? ? ? ?  ngx.log(ngx.INFO,">> 内部重定向")
 ? ? ? ? ? ? ? ? ? ?return
 ? ? ? ? ? ? ? ?end
 ? ? ? ? ? ? ? ?limit_conn.incoming()
 ? ? ? ? ? ? ?  ngx.log(ngx.INFO,">>> 请求进来了!")
 ? ? ? ? ?  }
 ? ? ? ? ? ?content_by_lua_block {
 ? ? ? ? ? ? ? ?-- 模拟请求处理时间,很重要,不加可能测试不出效果
 ? ? ? ? ? ? ? ?-- 生产中没有请求是只返回一个静态的index.html的!
 ? ? ? ? ? ? ?  ngx.sleep(0.5)
 ? ? ? ? ?  }
 ? ? ? ? ? ?log_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_conn = require "utils.limit_conn"
 ? ? ? ? ? ? ? ?limit_conn.leaving()
 ? ? ? ? ? ? ?  ngx.log(ngx.INFO,">>> 请求离开了!")
 ? ? ? ? ?  }
 ? ? ? ? ? ?
 ? ? ?  }
 ?  }
}

重点在于这句话,模拟每个请求0.5秒处理完成

content_by_lua_block {
    ngx.sleep(0.5)
}

注意在限制连接的代码里面,我们用 ngx.ctx 来存储 limit_conn_key。这里有一个坑。内部重定向(比如调用了 ngx.exec)会销毁 ngx.ctx,导致 limit_conn:leaving() 无法正确调用。 如果需要限连业务里有用到 ngx.exec,可以考虑改用 ngx.var 而不是 ngx.ctx,或者另外设计一套存储方式。只要能保证请求结束时能及时调用 limit:leaving() 即可。

重新加载配置文件

openresty -s reload 

测试

上面的配置是每个请求处理0.5秒,并发是10

  • 10个请求,并发为1
ab -n 10 -c 1 ?127.0.0.1/

# 请求全部成功,用时5s左右
Concurrency Level: ? ? ?1
Time taken for tests: ? 5.012 seconds 
Complete requests: ? ? ?10 
Failed requests: ? ? ? ?0
  • 10个请求,并发为10
ab -n 10 -c 10 ?127.0.0.1/

# 请求全部成功,用时1.5s左右
Concurrency Level: ? ? ?10
Time taken for tests: ? 1.505 seconds
Complete requests: ? ? ?10
Failed requests: ? ? ? ?0
  • 20个请求,并发为10,并发为10并不会触发限制条件,所以能成功!注意和下面并发11的区别!
ab -n 20 -c 10 ?127.0.0.1/

# 请求全部成功,用时2s左右
Concurrency Level: ? ? ?10
Time taken for tests: ? 2.005 seconds
Complete requests: ? ? ?20
Failed requests: ? ? ? ?0
  • 22个请求,并发为11 重点解释一下:并发不是qps,并发11不是说第一秒发11个请求,然后第二秒再发送11个请求,而是发完第一波紧接着发第二波,每一波的间隔时间不一定是1秒,下面的1.506 seconds就能看出来,按理应该是2s但是并不是第一波11个请求发送过去了,但是只能处理10个,所以成功了10个,紧接着第二波11个请求发过去了,但是第一波大部分未处理完成所以第二波的都失败了,也有处理完成了的可以接着处理,所以至少会成功10个,下面显示的是11个此处的大量失败应该是并发超过了10,触发了限制条件让nginx worker线程睡眠了,所以导致后面的请求大量失败-- 触发限制条件
    if delay >= 0.001 then
    ngx.sleep(delay) -- ngx worker睡眠
    end
ab -n 22 -c 11 ?127.0.0.1/

# 11个成功,11个失败
Concurrency Level: ? ? ?11
Time taken for tests: ? 1.506 seconds
Complete requests: ? ? ?22
Failed requests: ? ? ? ?11
Non-2xx responses: ? ? ?11 # HTTP状态非2xx的有11个,说明限并发成功(只有有非2xx的返回才会显示这句话)

反向代理

上面测试的是content_by_lua,也就是内容直接在lua中生成,但是实际中内容有可能是后端服务器生成的,所以可以设置反向代理或者负载均衡,如下为反向代理配置

location / {
 ? ?access_by_lua_block {
 ? ? ? ?local limit_conn = require "utils.limit_conn"
 ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ?if ngx.req.is_internal() then
 ? ? ?      return
 ? ? ? ?end
 ? ? ? ?limit_conn.incoming()
 ?  }
 ? ?log_by_lua_block {
 ? ? ? ?local limit_conn = require "utils.limit_conn"
 ? ? ? ?limit_conn.leaving()
 ?  }
 ? ?
 ? ?# 反向代理
 ? ?proxy_pass http://172.17.0.3:8080;
 ? ?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 / {
 ?access_by_lua_block {...}
 ?content_by_lua_block {...}
 ?log_by_lua_block {...}
}

nginx是按照阶段来执行指令的,和配置文件顺序没有关系,nginx是先执行access_by_lua_block,再执行content_by_lua_block,最后执行log_by_lua_block的,当在访问curl 127.0.0.1/时,如果没有content_by_lua_block,这里有一个内部重定向,会将127.0.0.1/的请求重定向到127.0.0.1/index.html,所以会按顺序再次执行access_by_lua_block,所以access_by_lua_block执行了两次,log_by_lua_block却执行了一次,当时的我十分懵逼,而加上content_by_lua或者proxy_pass则不会导致重定向,总之有内容来源时不会重定向,没有则会去找index.html导致重定向!

测试

vim /usr/local/openresty/nginx/conf/nginx.conf

# 修改成如下内容
server {
  listen 80;
  location / {
 ? ?  access_by_lua_block {
 ? ? ? ?  ngx.log(ngx.ERR,">>> access")
 ? ?  }
 ? ?  log_by_lua_block {
 ? ? ? ?  ngx.log(ngx.ERR,">>> log")
 ? ?  }
  }
}

# 查看日志
tail -f /usr/local/openresty/nginx/logs/error.log
  • 测试curl 127.0.0.1日志输出如下 access_by_lua_block执行了两次,并且页面上的内容是index.html的内容,说明发生了重定向 如果加上index.html,即curl 127.0.0.1/index.html,则不会发生重定向
...[lua] access_by_lua(nginx.conf:24):2: >>> access, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
...[lua] access_by_lua(nginx.conf:24):2: >>> access, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
...[lua] log_by_lua(nginx.conf:27):2: >>> log while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
  • 加上content_by_lua则访问http://127.0.0.1不会发生重定向

lua初始化

这句话local limit_conn = require "utils.limit_conn"limit_conn中的local limit, limit_err = limit_conn.new("limit_conn_store", 8, 2, 0.05)只会初始化一次,之后都是用的都一个实例,不会每个请求进来都要new一个limit_conn有点浪费性能而且还把参数都重置了,是不可取的,所以封装到了utils.limit_conn中!

限制接口时间窗请求数(非平滑)

场景:限制 ip 每1s只能调用 10 次(允许在时间段开始的时候一次性放过10个请求)也就是说,速率不是固定的

也可以设置成别的,比如120/min,只需要修改个数和时间窗口(resty.limit.countresty.limit.req区别在于:前者传入的是个数,后者传入的是速率)

新建utils/limit_count.lua模块

mkdir -p /usr/local/openresty/lualib/utils
cat > /usr/local/openresty/lualib/utils/limit_count.lua <

修改nginx配置文件

echo '' > /usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf

添加如下内容

worker_processes  1;

events {
 ? ?worker_connections  1024;
}
http {
 ? ?include ? ? ? mime.types;
 ? ?default_type  application/octet-stream;
 ? ?sendfile ? ? ?  on;
 ? ?keepalive_timeout  65;
 ? ?lua_code_cache on; ? ?
 ? 
 ? ?lua_shared_dict my_limit_count_store 100M;
 ? ?
 ? ?# resty.limit.count 需要resty.core
 ? ?init_by_lua_block {
        require "resty.core"
    }
 ? ?
 ? ?server {
 ? ? ? ?listen 80;
 ? ? ? ?location / {
 ? ? ? ? ? ?access_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_count = require "utils.limit_count"
 ? ? ? ? ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ? ? ? ? ?if ngx.req.is_internal() then
 ? ? ? ? ? ? ? ? ? ?return
 ? ? ? ? ? ? ? ?end
 ? ? ? ? ? ? ?  limit_count.incoming()
 ? ? ? ? ?  } ? ? ? ? ? ?
 ? ? ? ? ? ?
 ? ? ? ? ? ?content_by_lua_block {
 ? ? ? ? ? ? ?  ngx.sleep(0.1)
 ? ? ? ? ? ? ?  ngx.say('Hello')
 ? ? ? ? ?  }
 ? ? ? ? ? ?# 如果内容源是反向代理
 ? ? ? ? ? ?#proxy_pass http://172.17.0.3:8080;
 ? ? ? ? ? ?#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;

 ? ? ?  }
 ?  }
}

重新加载配置文件

openresty -s reload 

测试

上面的配置是10/s,不叠加

  • 10个请求,并发为10,1s内完成
ab -n 10 -c 10 ?127.0.0.1/

# 请求全部成功
Concurrency Level: ? ? ?10
Time taken for tests: ? 0.202 seconds
Complete requests: ? ? ?10
Failed requests: ? ? ? ?0

  • 20个请求,并发为20,1s内完成
ab -n 20 -c 20 ?127.0.0.1/

# 请求成功10个,其余全部失败
Concurrency Level: ? ? ?20
Time taken for tests: ? 0.202 seconds
Complete requests: ? ? ?20
Failed requests: ? ? ? ?10
 ? (Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Non-2xx responses: ? ? ?10

  • 查看请求头curl -I 127.0.0.1,可以看到接口限流信息

HTTP/1.1 200 OK
Server: openresty/1.17.8.2
Date: Sat, 12 Sep 2020 09:46:06 GMT
Content-Type: application/octet-stream
Connection: keep-alive
X-RateLimit-Limit: 10 # 当前限制10个
X-RateLimit-Remaining: 9 # 剩余9个

限制接口时间窗请求数(平滑)

桶(无容量)

场景:限制 ip 每1min只能调用 120次(平滑处理请求,即每秒放过2个请求),速率是固定的,并且桶没有容量(容量为0)

新建utils/limit_req_bucket.lua模块

mkdir -p /usr/local/openresty/lualib/utils
cat > /usr/local/openresty/lualib/utils/limit_req_bucket.lua <

修改nginx配置文件

echo '' > /usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf

添加如下内容

worker_processes  1;

events {
 ? ?worker_connections  1024;
}
http {
 ? ?include ? ? ? mime.types;
 ? ?default_type  application/octet-stream;
 ? ?sendfile ? ? ?  on;
 ? ?keepalive_timeout  65;
 ? ?lua_code_cache on; ? ?
 ? 
 ? ?lua_shared_dict my_limit_req_store 100M;
 ? ?
 ? ?server {
 ? ? ? ?listen 80;
 ? ? ? ?location / {
 ? ? ? ? ? ?access_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_count = require "utils.limit_req_bucket"
 ? ? ? ? ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ? ? ? ? ?if ngx.req.is_internal() then
 ? ? ? ? ? ? ? ? ? ?return
 ? ? ? ? ? ? ? ?end
 ? ? ? ? ? ? ?  limit_count.incoming()
 ? ? ? ? ?  } ? ? ? ? ? ?
 ? ? ? ? ? ?
 ? ? ? ? ? ?content_by_lua_block {
 ? ? ? ? ? ? ?  ngx.sleep(0.1)
 ? ? ? ? ? ? ?  ngx.say('Hello')
 ? ? ? ? ?  }
 ? ? ? ? ? ?# 如果内容源是反向代理
 ? ? ? ? ? ?#proxy_pass http://172.17.0.3:8080;
 ? ? ? ? ? ?#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;

 ? ? ?  }
 ?  }
}

重新加载配置文件

openresty -s reload 

测试

上面的配置是2/s即为120/min

  • 请求时间限制为1s
ab -t 1 127.0.0.1/

# 实际请求1.1s,成功3个请求,符合预期
Time taken for tests: ? 1.100 seconds
Complete requests: ? ? ?8656
Failed requests: ? ? ? ?8653
 ? (Connect: 0, Receive: 0, Length: 8653, Exceptions: 0)
Non-2xx responses: ? ? ?8653


  • 请求时间限制为5s
ab -t 5 127.0.0.1/

# 实际请求5.1s,成功11个请求,符合预期
Concurrency Level: ? ? ?1
Time taken for tests: ? 5.100 seconds
Complete requests: ? ? ?40054
Failed requests: ? ? ? ?40043
 ? (Connect: 0, Receive: 0, Length: 40043, Exceptions: 0)
Non-2xx responses: ? ? ?40043

漏桶(有桶容量)

场景:限制 ip 每1min只能调用 120次(平滑处理请求,即每秒放过2个请求),速率是固定的,并且桶的容量有容量(设置burst)

新建utils/limit_req_leaky_bucket.lua模块

只需要在桶(无容量)的基础之上增加burst的值即可,并且增加delay的处理

mkdir -p /usr/local/openresty/lualib/utils
cat > /usr/local/openresty/lualib/utils/limit_req_leaky_bucket.lua <= 0.001 then
 ? ? ? ?ngx.sleep(delay)
 ? ?end
end

return _M

EOF

修改nginx配置文件

echo '' > /usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf

添加如下内容

worker_processes  1;

events {
 ? ?worker_connections  1024;
}
http {
 ? ?include ? ? ? mime.types;
 ? ?default_type  application/octet-stream;
 ? ?sendfile ? ? ?  on;
 ? ?keepalive_timeout  65;
 ? ?lua_code_cache on; ? ?
 ? 
 ? ?lua_shared_dict my_limit_req_store 100M;
 ? ?
 ? ?server {
 ? ? ? ?listen 80;
 ? ? ? ?location / {
 ? ? ? ? ? ?access_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_count = require "utils.limit_req_leaky_bucket"
 ? ? ? ? ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ? ? ? ? ?if ngx.req.is_internal() then
 ? ? ? ? ? ? ? ? ? ?return
 ? ? ? ? ? ? ? ?end
 ? ? ? ? ? ? ?  limit_count.incoming()
 ? ? ? ? ?  } ? ? ? ? ? ?
 ? ? ? ? ? ?
 ? ? ? ? ? ?content_by_lua_block {
 ? ? ? ? ? ? ? ?-- 模拟每个请求的耗时
 ? ? ? ? ? ? ?  ngx.sleep(0.1)
 ? ? ? ? ? ? ?  ngx.say('Hello')
 ? ? ? ? ?  }
 ? ? ? ? ? ?# 如果内容源是反向代理
 ? ? ? ? ? ?#proxy_pass http://172.17.0.3:8080;
 ? ? ? ? ? ?#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;

 ? ? ?  }
 ?  }
}

重新加载配置文件

openresty -s reload 

测试

上面的配置是2/s,漏桶容量为1/s,即总共3/s,模拟的每个请求耗时为0.1s,那么1s内能处理至少10个请求

  • 请求时间限制为1s
ab -t 1 127.0.0.1/

# 实际请求1.102s,成功3个请求,1s两个请求,一个是delay,符合预期
Time taken for tests: ? 1.103 seconds
Complete requests: ? ? ?3
Failed requests: ? ? ? ?0

令牌桶

场景:限制 ip 每1min只能调用 120次(平滑处理请求,即每秒放过2个请求),但是允许一定的突发流量(突发的流量,就是桶的容量(桶容量为60),超过桶容量直接拒绝

令牌桶其实可以看着是漏桶的逆操作,看我们对把超过请求速率而进入桶中的请求如何处理,如果是我们把这部分请求放入到等待队列中去,那么其实就是用了漏桶算法,但是如果我们允许直接处理这部分的突发请求,其实就是使用了令牌桶算法。

这边只要将上面漏桶算法关于桶中请求的延时处理的代码修改成直接送到后端服务就可以了,这样便是使用了令牌桶

新建utils/limit_req_token_bucket.lua模块

mkdir -p /usr/local/openresty/lualib/utils
cat > /usr/local/openresty/lualib/utils/limit_req_token_bucket.lua <= 0.001 then
 ? ? ? ?-- 不做任何操作,直接放行突发流量
 ? ? ? ?-- ngx.sleep(delay)
 ? ?end
end

return _M

EOF

修改nginx配置文件

echo '' > /usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf

添加如下内容

worker_processes  1;

events {
 ? ?worker_connections  1024;
}
http {
 ? ?include ? ? ? mime.types;
 ? ?default_type  application/octet-stream;
 ? ?sendfile ? ? ?  on;
 ? ?keepalive_timeout  65;
 ? ?lua_code_cache on; ? ?
 ? 
 ? ?lua_shared_dict my_limit_req_store 100M;
 ? ?
 ? ?server {
 ? ? ? ?listen 80;
 ? ? ? ?location / {
 ? ? ? ? ? ?access_by_lua_block {
 ? ? ? ? ? ? ? ?local limit_count = require "utils.limit_req_token_bucket"
 ? ? ? ? ? ? ? ?-- 对于内部重定向或子请求,不进行限制。因为这些并不是真正对外的请求。
 ? ? ? ? ? ? ? ?if ngx.req.is_internal() then
 ? ? ? ? ? ? ? ? ? ?return
 ? ? ? ? ? ? ? ?end
 ? ? ? ? ? ? ?  limit_count.incoming()
 ? ? ? ? ?  } ? ? ? ? ? ?
 ? ? ? ? ? ?
 ? ? ? ? ? ?content_by_lua_block {
 ? ? ? ? ? ? ? ?-- 模拟每个请求的耗时
 ? ? ? ? ? ? ?  ngx.sleep(0.1)
 ? ? ? ? ? ? ?  ngx.say('Hello')
 ? ? ? ? ?  }
 ? ? ? ? ? ?# 如果内容源是反向代理
 ? ? ? ? ? ?#proxy_pass http://172.17.0.3:8080;
 ? ? ? ? ? ?#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;

 ? ? ?  }
 ?  }
}

重新加载配置文件

openresty -s reload 

测试

上面模拟的每个请求耗时为0.1s,那么1s内能处理至少10个请求

  • 时间限制为1s
ab -n 10 -c 10 ?-t 1 127.0.0.1/

# 实际请求1s,成功13个请求,可以看到是远远超过2个请求的,多余就是在处理突发请求
Concurrency Level: ? ? ?10
Time taken for tests: ? 1.000 seconds
Complete requests: ? ? ?12756
Failed requests: ? ? ? ?12743
 ? (Connect: 0, Receive: 0, Length: 12743, Exceptions: 0)
Non-2xx responses: ? ? ?12743

组合各种limter

上面的三种限速器conn、count、req可以进行各种组合,比如一个限速器是限制主机名的,一个是限制ip的,可以组合起来使用

参考:
https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/traffic.md

相关推荐

大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍

“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...

电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器

在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...

设置了加密密码zip文件要如何打开?这几个方法可以试试~

Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...

大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...

高效处理 RAR 分卷压缩包:合并解压操作全攻略

在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...

2个方法教你如何删除ZIP压缩包密码

zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...

速转!漏洞预警丨压缩软件Winrar目录穿越漏洞

WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...

文件解压方法和工具分享_文件解压工具下载

压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...

[python]《Python编程快速上手:让繁琐工作自动化》学习笔记3

1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...

Python内置tarfile模块:读写 tar 归档文件详解

一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...

使用python展开tar包_python拓展

类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...

银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留

近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...

ZIP文件怎么打开?2个简单方法教你轻松搞定!

在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...

Ubuntu—解压多个zip压缩文件.zip .z01 .z02

方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...

如何使用7-Zip对文件进行加密压缩

7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...

取消回复欢迎 发表评论: