Redis 多规则限流和防重复提交方案实现

👉 这是一个或许对你有用的社群

🐱 一对一交流/面试小册/简历优化/求职解惑,欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料: 

a582ddaf60af61a232b9a5de6bbbaa69.gif

👉这是一个或许对你有用的开源项目

国产 Star 破 10w+ 的开源项目,前端包括管理后台 + 微信小程序,后端支持单体和微服务架构。

功能涵盖 RBAC 权限、SaaS 多租户、数据权限、商城、支付、工作流、大屏报表、微信公众号、CRM 等等功能:

  • Boot 仓库:https://gitee.com/zhijiantianya/ruoyi-vue-pro

  • Cloud 仓库:https://gitee.com/zhijiantianya/yudao-cloud

  • 视频教程:https://doc.iocoder.cn

【国内首批】支持 JDK 21 + SpringBoot 3.2.2、JDK 8 + Spring Boot 2.7.18 双版本 

来源:juejin.cn/post/
7298635806475386916


简介

市面上很多介绍redis如何实现限流的,但是大部分都有一个缺点,就是只能实现单一的限流,比如1分钟访问1次或者60分钟访问10次这种,但是如果想一个接口两种规则都需要满足呢,我们的项目又是分布式项目,应该如何解决,下面就介绍一下redis实现分布式多规则限流的方式。

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/ruoyi-vue-pro

  • 视频教程:https://doc.iocoder.cn/video/

思考

  1. 如何一分钟只能发送一次验证码,一小时只能发送10次验证码等等多种规则的限流

  2. 如何防止接口被恶意打击(短时间内大量请求)

  3. 如何限制接口规定时间内访问次数

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/yudao-cloud

  • 视频教程:https://doc.iocoder.cn/video/

解决方法

记录某IP访问次数

使用 String结构 记录固定时间段内某用户IP访问某接口的次数

  • RedisKey = prefix : className : methodName

  • RedisVlue = 访问次数

拦截请求:

  1. 初次访问时设置 「[RedisKey] [RedisValue=1] [规定的过期时间]」

  2. 获取 RedisValue 是否超过规定次数,超过则拦截,未超过则对 RedisKey 进行加1

分析: 规则是每分钟访问 1000 次

  1. 考虑并发问题

  • 假设目前 RedisKey => RedisValue 为 999

  • 目前大量请求进行到第一步( 获取Redis请求次数 ),那么所有线程都获取到了值为999,进行判断都未超过限定次数则不拦截,导致实际次数超过 1000 次

  • 「解决办法:」 保证方法执行原子性(加锁、lua)

  1. 考虑在临界值进行访问

  • 思考下图

f596964ac6146994a5380a29ebb1f55d.jpeg
图片

代码实现: 比较简单,参考:https://gitee.com/y_project/RuoYi-Vue/blob/master/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java``。

Zset解决临界值问题

使用 Zset 进行存储,解决临界值访问问题

abe315202c331888e0b0a4944b40ed14.jpeg
图片

网上几乎都有实现,这里就不过多介绍

实现多规则限流

先确定最终需要的效果
  • 能实现多种限流规则

  • 能实现防重复提交

通过以上要求设计注解(先想象出最终实现效果)

@RateLimiter(
    rules = {
            // 60秒内只能访问10次
            @RateRule(count = 10, time = 60, timeUnit = TimeUnit.SECONDS),
            // 120秒内只能访问20次
            @RateRule(count = 20, time = 120, timeUnit = TimeUnit.SECONDS)

    },
    // 防重复提交 (5秒钟只能访问1次)
    preventDuplicate = true
)
编写注解(RateLimiter,RateRule)

编写 RateLimiter 注解。

/**
 * @Description: 请求接口限制
 * @Author: yiFei
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RateLimiter {

    /**
     * 限流key
     */
    String key() default RedisKeyConstants.RATE_LIMIT_CACHE_PREFIX;

    /**
     * 限流类型 ( 默认 Ip 模式 )
     */
    LimitTypeEnum limitType() default LimitTypeEnum.IP;

    /**
     * 错误提示
     */
    ResultCode message() default ResultCode.REQUEST_MORE_ERROR;

    /**
     * 限流规则 (规则不可变,可多规则)
     */
    RateRule[] rules() default {};

    /**
     * 防重复提交值
     */
    boolean preventDuplicate() default false;

    /**
     * 防重复提交默认值
     */
    RateRule preventDuplicateRule() default @RateRule(count = 1, time = 5);
}

编写RateRule注解

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RateRule {

    /**
     * 限流次数
     */
    long count() default 10;

    /**
     * 限流时间
     */
    long time() default 60;

    /**
     * 限流时间单位
     */
    TimeUnit timeUnit() default TimeUnit.SECONDS;

}
拦截注解 RateLimiter
  • 确定redis存储方式

    • RedisKey = prefix : className : methodName

    • RedisScore = 时间戳

    • RedisValue = 任意分布式不重复的值即可

  • 编写生成 RedisKey 的方法

/**
 * 通过 rateLimiter 和 joinPoint 拼接  prefix : ip / userId : classSimpleName - methodName
 *
 * @param rateLimiter 提供 prefix
 * @param joinPoint   提供 classSimpleName : methodName
 * @return
 */
public String getCombineKey(RateLimiter rateLimiter, JoinPoint joinPoint) {
    StringBuffer key = new StringBuffer(rateLimiter.key());
    // 不同限流类型使用不同的前缀
    switch (rateLimiter.limitType()) {
        // XXX 可以新增通过参数指定参数进行限流
        case IP:
            key.append(IpUtil.getIpAddr(((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest())).append(":");
            break;
        case USER_ID:
            SysUserDetails user = SecurityUtil.getUser();
            if (!ObjectUtils.isEmpty(user)) key.append(user.getUserId()).append(":");
            break;
        case GLOBAL:
            break;
    }
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    Class<?> targetClass = method.getDeclaringClass();
    key.append(targetClass.getSimpleName()).append("-").append(method.getName());
    return key.toString();
}

编写lua脚本

编写lua脚本 (两种将时间添加到Redis的方法)。

Zset的UUID value值

UUID(可用其他有相同的特性的值)为Zset中的value值

  • 参数介绍

    • KEYS[1] = prefix : ? : className : methodName

    • KEYS[2] = 唯一ID

    • KEYS[3] = 当前时间

    • ARGV = [次数,单位时间,次数,单位时间, 次数, 单位时间 ...]

  • 由java传入分布式不重复的 value 值

-- 1. 获取参数
local key = KEYS[1]
local uuid = KEYS[2]
local currentTime = tonumber(KEYS[3])
-- 2. 以数组最大值为 ttl 最大值
local expireTime = -1;
-- 3. 遍历数组查看是否超过限流规则
for i = 1, #ARGV, 2 do
    local rateRuleCount = tonumber(ARGV[i])
    local rateRuleTime = tonumber(ARGV[i + 1])
    -- 3.1 判断在单位时间内访问次数
    local count = redis.call('ZCOUNT', key, currentTime - rateRuleTime, currentTime)
    -- 3.2 判断是否超过规定次数
    if tonumber(count) >= rateRuleCount then
        return true
    end
    -- 3.3 判断元素最大值,设置为最终过期时间
    if rateRuleTime > expireTime then
        expireTime = rateRuleTime
    end
end
-- 4. redis 中添加当前时间
redis.call('ZADD', key, currentTime, uuid)
-- 5. 更新缓存过期时间
redis.call('PEXPIRE', key, expireTime)
-- 6. 删除最大时间限度之前的数据,防止数据过多
redis.call('ZREMRANGEBYSCORE', key, 0, currentTime - expireTime)
return false
根据时间戳作为Zset中的value值
  • 参数介绍

    • KEYS[1] = prefix : ? : className : methodName

    • KEYS[2] = 当前时间

    • ARGV = [次数,单位时间,次数,单位时间, 次数, 单位时间 ...]

  • 根据时间进行生成value值,考虑同一毫秒添加相同时间值问题

    • 以下为第二种实现方式,在并发高的情况下效率低,value是通过时间戳进行添加,但是访问量大的话会使得一直在调用 redis.call('ZADD', key, currentTime, currentTime),但是在不冲突value的情况下,会比生成 UUID 好

-- 1. 获取参数
local key = KEYS[1]
local currentTime = KEYS[2]
-- 2. 以数组最大值为 ttl 最大值
local expireTime = -1;
-- 3. 遍历数组查看是否越界
for i = 1, #ARGV, 2 do
    local rateRuleCount = tonumber(ARGV[i])
    local rateRuleTime = tonumber(ARGV[i + 1])
    -- 3.1 判断在单位时间内访问次数
    local count = redis.call('ZCOUNT', key, currentTime - rateRuleTime, currentTime)
    -- 3.2 判断是否超过规定次数
    if tonumber(count) >= rateRuleCount then
        return true
    end
    -- 3.3 判断元素最大值,设置为最终过期时间
    if rateRuleTime > expireTime then
        expireTime = rateRuleTime
    end
end
-- 4. 更新缓存过期时间
redis.call('PEXPIRE', key, expireTime)
-- 5. 删除最大时间限度之前的数据,防止数据过多
redis.call('ZREMRANGEBYSCORE', key, 0, currentTime - expireTime)
-- 6. redis 中添加当前时间  ( 解决多个线程在同一毫秒添加相同 value 导致 Redis 漏记的问题 )
-- 6.1 maxRetries 最大重试次数 retries 重试次数
local maxRetries = 5
local retries = 0
while true do
    local result = redis.call('ZADD', key, currentTime, currentTime)
    if result == 1 then
        -- 6.2 添加成功则跳出循环
        break
    else
        -- 6.3 未添加成功则 value + 1 再次进行尝试
        retries = retries + 1
        if retries >= maxRetries then
            -- 6.4 超过最大尝试次数 采用添加随机数策略
            local random_value = math.random(1, 1000)
            currentTime = currentTime + random_value
        else
            currentTime = currentTime + 1
        end
    end
end

return false
编写 AOP 拦截
@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private RedisScript<Boolean> limitScript;

/**
 * 限流
 * XXX 对限流要求比较高,可以使用在 Redis中对规则进行存储校验 或者使用中间件
 *
 * @param joinPoint   joinPoint
 * @param rateLimiter 限流注解
 */
@Before(value = "@annotation(rateLimiter)")
public void boBefore(JoinPoint joinPoint, RateLimiter rateLimiter) {
    // 1. 生成 key
    String key = getCombineKey(rateLimiter, joinPoint);
    try {
        // 2. 执行脚本返回是否限流
        Boolean flag = redisTemplate.execute(limitScript,
                ListUtil.of(key, String.valueOf(System.currentTimeMillis())),
                (Object[]) getRules(rateLimiter));
        // 3. 判断是否限流
        if (Boolean.TRUE.equals(flag)) {
            log.error("ip: '{}' 拦截到一个请求 RedisKey: '{}'",
                    IpUtil.getIpAddr(((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest()),
                    key);
            throw new ServiceException(rateLimiter.message());
        }
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 获取规则
 *
 * @param rateLimiter 获取其中规则信息
 * @return
 */
private Long[] getRules(RateLimiter rateLimiter) {
    int capacity = rateLimiter.rules().length << 1;
    // 1. 构建 args
    Long[] args = new Long[rateLimiter.preventDuplicate() ? capacity + 2 : capacity];
    // 3. 记录数组元素
    int index = 0;
    // 2. 判断是否需要添加防重复提交到redis进行校验
    if (rateLimiter.preventDuplicate()) {
        RateRule preventRateRule = rateLimiter.preventDuplicateRule();
        args[index++] = preventRateRule.count();
        args[index++] = preventRateRule.timeUnit().toMillis(preventRateRule.time());
    }
    RateRule[] rules = rateLimiter.rules();
    for (RateRule rule : rules) {
        args[index++] = rule.count();
        args[index++] = rule.timeUnit().toMillis(rule.time());
    }
    return args;
}

以上,欢迎大家提出意见。


欢迎加入我的知识星球,全面提升技术能力。

👉 加入方式,长按”或“扫描”下方二维码噢

d95cc75e0ef650711c6fc6e4b8498ad3.png

星球的内容包括:项目实战、面试招聘、源码解析、学习路线。

03a09e05994ac3b0cff89f8a1a5a594d.png

be9c9969b0686a1a8b3f447f72352699.png59e7059e792721c329ce1a467343f898.pngb0783421d85aba920db56896172d29f8.png30be63aabd06939175a49dd9d4aa5e7b.png

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在并发场景下,为了保证接口的可靠性和稳定性,我们可以使用 Redis实现接口限流刷。 具体实现方法如下: 1. 首先,在 Spring Boot 项目中引入 Redis 相关的依赖,如 jedis、lettuce 等。 2. 在 Redis 中设置一个 key,用来记录请求次数或者时间戳等信息。 3. 在接口中加入拦截器,对请求进行拦截,并从 Redis 中获取相应的 key 值,判断是否达到限流刷的条件。 4. 如果达到条件,可以返回一个自定义的错误码或者错误信息,或者直接拒绝请求。 5. 如果没有达到条件,则更新 Redis 中的 key 值,并放行请求。 下面是一个简单的示例代码: ```java @Component public class RateLimitInterceptor implements HandlerInterceptor { private final RedisTemplate<String, String> redisTemplate; @Autowired public RateLimitInterceptor(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String key = request.getRemoteAddr(); String value = redisTemplate.opsForValue().get(key); if (value == null) { redisTemplate.opsForValue().set(key, "1", 60, TimeUnit.SECONDS); // 60秒内最多访问1次 } else { int count = Integer.parseInt(value); if (count >= 10) { // 10次以上就限流 response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "Too many requests"); return false; } else { redisTemplate.opsForValue().increment(key); } } return true; } } ``` 在上面的代码中,我们使用 Redis 记录了每个 IP 地址的访问次数,并且在 60 秒内最多只能访问 1 次。如果访问次数超过了 10 次,则返回状态码 429(Too many requests)。 当然,这只是一个简单的示例,实际应用中我们可能需要更加复杂的限流策略和刷机制,但是基本原理都是类似的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值