当前位置:网站首页>redis + lua实现分布式接口限流实现方案
redis + lua实现分布式接口限流实现方案
2022-08-11 05:36:00 【CSDN云计算】

作者 | 步尔斯特
来源 | 步尔斯特
前言
redis + lua脚本已然成为了单体项目主流的限流方案。
redis凭借其特性成为了中间件的佼佼者,最新官方测试数据:
读的速度是110000次/s
写的速度是81000次/s。
lua:
减少网络开销:使用Lua脚本,无需向Redis 发送多次请求,执行一次即可,减少网络传输
原子操作:Redis 将整个Lua脚本作为一个命令执行,原子,无需担心并发
复用:Lua脚本一旦执行,会永久保存 Redis 中,,其他客户端可复用
操作
在需要限流的接口处添加如下注解(@RedisLimit),在原有基础上,无需添加任何依赖即可实现限流。
@RedisLimit(name = "订单秒杀", prefix = "seckill", key = "distributed", count = 1, period = 1, limitType = LimitType.IP, msg = "当前排队人数较多,请稍后再试!")
@GetMapping("/limit/distributed/{id}")
public ResponseEntity<Object> limitDistributed(@PathVariable("id") String id) {
return ResponseEntity.ok("成功购买:" + id + "个");
}介绍
/**
* 资源名称
*/
String name() default "";
/**
* 前缀
*/
String prefix() default "";
/**
* 资源key
*/
String key() default "";
/**
* 最多访问次数
*/
int count();
/**
* 时间,秒级
*/
int period();
/**
* 类型
*/
LimitType limitType() default LimitType.CUSTOMER;
/**
* 提示信息
*/
String msg() default "系统繁忙,请稍后再试";功能
默认根据全局接口的QPS作为流控指标
可在独立IP和全局接口自由切换
可自定义时间及规定时间内的QPS
可根据key值做后期的数据监控和统计
原理
以流量作为切点、滑动时间窗口作为核心算法。
lua脚本以保证其原子性操作
核心代码(部分展示)
lua脚本
redis.replicate_commands();
local listLen,time
listLen = redis.call('LLEN', KEYS[1])
if listLen and tonumber(listLen) < tonumber(ARGV[1]) then
local a = redis.call('TIME');
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
else
time = redis.call('LINDEX', KEYS[1], -1)
local a = redis.call('TIME');
if a[1]*1000000+a[2] - time < tonumber(ARGV[2])*1000000 then
return 0;
else
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
redis.call('LTRIM', KEYS[1], 0, tonumber(ARGV[1])-1)
end
end
return 1;切点处理
Long number = redisTemplate.execute(SECKILL_SCRIPT, keys, count, period);
if(number != null && number.intValue() == 1){
return pjp.proceed();
}
往期推荐

点分享

点收藏

点点赞

点在看
边栏推荐
猜你喜欢
随机推荐
[损失函数]——均方差
华为防火墙-5-NAT
TOP2 Add two numbers
空间点模式方法_一阶效应和二阶效应
Open Set Domain Adaptation 开集领域适应
OA项目之待开会议&历史会议&所有会议
arcgis填坑_3
buildroot setup dhcp
亚马逊API接口大全
kill 命令
MySQl进阶之索引结构
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
HCIA实验
每日sql -用户两天留存率
SATA、SAS、SSD三种硬盘存储性能数据
ETCD containerized to build a cluster
Especially the redis
HCIP MGRE\OSPF综合实验
拼多多API接口(附上我的可用API)
华为防火墙-2-状态检测与会话









