网站首页 > 技术教程 正文
如果有这么一个场景:实现控制单IP在10秒内(一定时间周期内)只能访问10次(一定次数)的限流功能,该如何来实现?下面介绍两种实现方式
实现一:Nginx Lua实现分布式计数器限流
- 使用Redis存储分布式访问计数;
- Nginx Lua编程完成计数器累加及逻辑判断
首先,在Nginx的配置文件中添加location配置块,拦截需要限流的接口,匹配到该请求后,将其转给一个Lua脚本处理。
location = /access/demo/nginx/lua {
set $count 0;
access_by_lua_file luaScript/module/ratelimit/access_auth_nginx.lua;
content_by_lua_block {
ngx.say("目前访问总数: ", ngx.var.count, "<br>");
ngx.say("Hello World");
}
}
定义access_auth_nginx.lua限流脚本,该脚本会调用一个名为RedisKeyRateLimit.lua的限流计数器脚本,完成针对同一个IP的限流操作。
RedisKeyRateLimit限流计数器脚本代码如下:
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by xx.
--- DateTime: 2022/2/21 下午8:27
---
local basic = require("luaScript.module.common.basic");
local redisOp = require("luaScript.redis.RedisOperator");
--一个统一的模块对象
local _Module = {}
_Module.__index = _Module;
-- 创建一个新的实例
function _Module.new(self, key)
local object = {red = nil};
setmetatable(object, self);
--创建自定义的Redis操作对象
local red = redisOp:new();
basic.log("参数key = "..key);
red:open();
object.red = red;
--object.key = "count_rate_limit:" .. key;
object.key = key;
return object;
end
-- 判断是否能通过流量控制
-- true:未被限流;false:被限流
function _Module.acquire(self)
local redis = self.red;
basic.log("当前key = "..self.key)
local current = redis:getValue(self.key);
basic.log("value type is "..type(current));
basic.log("当前计数器值 value = "..tostring(current));
--判断是否大于限制次数
local limited = current and current ~= ngx.null and tonumber(current) > 10;
-- 被限流
if limited then
basic.log("限流成功,已经超过10次了,本次是第"..current.."次");
redis:incrValue(self.key);
return false;
end
if not current or current == ngx.null then
redis:setValue(self.key, 1);
redis:expire(self.key, 10);
else
redis:incrValue(self.key);
end
return true
end
-- 取得访问次数
function _Module.getCount(self)
local current = self.red:getValue(self.key);
if current and current ~= ngx.null then
return tonumber(current);
end
return 0;
end
-- 归还Redis
function _Module.close(self)
self.red:close();
end
return _Module;
access_auth_nginx限流脚本如下:
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by xx.
--- DateTime: 2022/2/21 下午8:44
---
-- 导入自定义模块
local basic = require("luaScript.module.common.basic");
local RedisKeyRateLimit = require("luaScript.module.ratelimit.RedisKeyRateLimit");
--定义出错的JSON输出对象
local errorOut = {errorCode = -1, errorMsg = "限流出错", data = {} };
--获取请求参数
local args = nil;
if "GET" == ngx.var.request_method then
args = ngx.req.get_uri_args();
elseif "POST" == ngx.var.request_method then
ngx.req.read_body();
args = ngx.req.get_post_args();
end
--ngx.say(args["a"]);
--ngx.say(ngx.var.remote_addr);
-- 获取用户IP
local shortKey = ngx.var.remote_addr;
if not shortKey or shortKey == ngx.null then
errorOut.errMsg = "shortKey 不能为空";
ngx.say(cjson.encode(errorOut));
return;
end
-- 拼接计数的Redis Key
local key = "count_rate_limit:ip:"..shortKey;
local limiter = RedisKeyRateLimit:new(key);
local pass = limiter:acquire();
if pass then
ngx.var.count = limiter:getCount();
basic.log("access_auth_nginx get count = "..ngx.var.count)
end
limiter:close();
if not pass then
errorOut.errorMsg = "您的操作太频繁了,请稍后再试.";
ngx.say(cjson.encode(errorOut));
ngx.say(ngx.HTTP_UNAUTHORIZED);
end
return;
浏览器访问http://localhost/access/demo/nginx/lua?a=111接口,10秒内多次刷新结果如下:
频繁刷新后的结果
某个时间点Redis中,存入的键-值如下
上述方案,如果是单网关,则不会有一致性问题。在多网关(Nginx集群)环境下,计数器的读取和自增由两次Redis远程操作完成,可能会出现数据一致性问题,且同一次限流需要多次访问Redis,存在多次网络传输,会降低限流的性能。
实现二:Redis Lua实现分布式计数器限流
该方案主要依赖Redis,使用Redis存储分布式访问计数,又通过Redis执行限流计数器的Lua脚本,减少了Redis远程操作次数,相对于Nginx网关,保证只有一次Redis操作即可完成限流操作,而Redis可以保证脚本的原子性。架构图如下:
具体实现:
首先,在Nginx的配置文件中添加location配置块,拦截需要限流的接口,匹配到该请求后,将其转给一个Lua脚本处理
location = /access/demo/redis/lua {
set $count 0;
access_by_lua_file luaScript/module/rateLimit/access_auth_redis.lua;
content_by_lua_block {
ngx.say("目前访问总数: ", ngx.var.count, "<br>");
ngx.say("Hello World");
}
}
再来看限流脚本:redis_rate_limit.lua
-- 限流计数器脚本,负责完成访问计数和限流结果判断。该脚本需要在Redis中加载和执行
-- 返回0表示需要限流,返回其他值表示访问次数
local cacheKey = KEYS[1];
local data = redis.call("incr", cacheKey);
local count = tonumber(data);
-- 首次访问,设置过期时间
if count == 1 then
redis.call("expire", cacheKey, 10);
end
if count > 10 then
return 0; -- 0表示需要限流
end
return count;
限流脚本要执行在Redis中,因此需要将其加载到Redis中,并且获取其加载后的sha1编码,供Nginx上的限流脚本access_auth_redis.lua使用。
将redis_rate_limit.lua脚本加载到Redis的命令如下:
# 进入到当前脚本目录
? rateLimit git:(main) ? cd ~/Work/QDBank/Idea-WorkSpace/About_Lua/src/luaScript/module/rateLimit
# 加载Lua脚本
? rateLimit git:(main) ? ~/Software/redis-6.2.6/src/redis-cli script load "$(cat redis_rate_limit.lua)"
"5f383977029cd430bd4c98547f3763c9684695c7"
? rateLimit git:(main) ?
最后看下access_auth_redis.lua脚本。该脚本使用Redis的evalsha操作指令,远程访问加载在Redis中的redis_rate_limit.lua脚本,完成针对同一个IP的限流。
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by xuexiao.
--- DateTime: 2022/2/22 下午10:15
---
local RedisKeyRateLimit = require("luaScript.module.rateLimit.RedisKeyRateLimit")
--定义出错的JSON输出对象
local errorOut = {errorCode = -1, errorMsg = "限流出错", data = {} };
--获取请求参数
local args = nil;
if "GET" == ngx.var.request_method then
args = ngx.req.get_uri_args();
elseif "POST" == ngx.var.request_method then
ngx.req.read_body();
args = ngx.req.get_post_args();
end
-- 获取用户IP
local shortKey = ngx.var.remote_addr;
if not shortKey or shortKey == ngx.null then
errorOut.errMsg = "shortKey 不能为空";
ngx.say(cjson.encode(errorOut));
return;
end
-- 拼接计数的Redis key
local key = "redis_count_rate_limit:ip:"..shortKey;
local limiter = RedisKeyRateLimit:new(key);
local passed = limiter:acquire();
-- 通过流量控制
if passed then
ngx.var.count = limiter:getCount()
end
limiter:close();
-- 未通过流量控制
if not passed then
errorOut.errorMsg = "您的操作太频繁了,请稍后再试.";
ngx.say(cjson.encode(errorOut));
ngx.exit(ngx.HTTP_UNAUTHORIZED);
end
return;
浏览器中访问http://localhost/access/demo/redis/lua?a=999,限流效果同案例一。
通过将Lua脚本加入Redis执行有以下优势:
- 减少网络开销:只需要一个脚本即可,不需要多次远程访问Redis;
- 原子操作:Redis将整个脚本作为一个原子执行,无须担心并发,也不用考虑事务;
- 复用:只要Redis不重启,脚本加载之后会一直缓存在Redis中,其他客户端可以通过sha1编码执行。
猜你喜欢
- 2024-10-09 openresty代替nginx并使用lua扩展功能
- 2024-10-09 Redis弱事务性与Lua脚本原子性分析
- 2024-09-08 基于Nginx+lua的蓝绿发布系统(lvs与nginx)
- 2024-09-08 Go Web 框架 Gin 实践17—用 Nginx 部署 Go 应用
- 2024-09-08 一文看懂灰度发布——基于Nginx+Lua+Redis
- 2024-09-08 Lua 基础入门(lua经典详细入门教程中文pdf)
- 2024-09-08 Redis中使用Lua脚本来实现并发下的原子操作
- 2024-09-08 运维篇—基于Nginx+Lua实现的灰度发布
- 2024-09-08 Nginx+Lua+Redis实现高性能缓存数据读取
- 2024-09-08 OpenResty实战-Lua入门-Lua模块(openresty教程)
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)