对前端限流操作(无Redis版本)

如何限制前端的请求次数

最近学习缓存击穿的时候,解决方法是限流,前端限制请求次数。故通过后端来对前端的请求做限流次数。

这里首先不用redis方法,这里采用通过Aop切面的方式来限制请求次数

创建限流注解

/**
 * 限流接口
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RequestLimit {

    // time是调用接口的间隔时间,默认是 5000 毫秒
    long time() default 5000;

}

获取单例的map对象存放数据

SubmitBufferSingleton 用来获取唯一的 HashMap<String, Long>,其中的Value是从 1970-01-01T00:00:00Z(协调世界时,UTC)到当前时间点之间的毫秒数

/**
 * 获取单例map对象
 */
public class SubmitBufferSingleton {

    private static final HashMap<String, Long> map = new HashMap<>();

    private SubmitBufferSingleton() {
    }

    public static HashMap<String, Long> getInstance() {
        return map;
    }

}

创建切面的任务

@Aspect
@Component
@Slf4j
public class NoRepeatSubmitAop {

    // 定义切面
    @Pointcut("@annotation(com.zhuang.aspect.annotation.RequestLimit)")
    private void noRepeatSubmitAop() {

    }


    @Synchronized // 作用是创建一个互斥锁,保证只有一个线程对 SubmitBufferSingleton.getInstance() 这个变量进行修改。
    @Around("noRepeatSubmitAop()&&@annotation(nrs)")
    public Object around(ProceedingJoinPoint pjp, RequestLimit nrs) throws Throwable {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        String key = getIp(request) + " :" + request.getServletPath();
        long time = nrs.time();
        Object o = pjp.proceed();
        HashMap<String, Long> hashMap = SubmitBufferSingleton.getInstance();
        long nowTime = Instant.now().toEpochMilli();
        if (!hashMap.containsKey(key)) {
            hashMap.put(key, nowTime + time);
            return o;
        } else {
            if (nowTime > hashMap.get(key)) {
                hashMap.put(key, nowTime + time);
                return o;
            } else {
                log.error("操作过于频繁 {}", key);
                return "操作过于频繁";
            }
        }

    }


    // 获取调用者ip
    private static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}

创建定时任务

这里设置一个定时器在每秒清理一下HashMap数据,防止 HashMap<String, Long> 越来越大。将 HashMap<String, Long> 从 NoRepeatSubmitAop 中抽出来的优点在这里也体现出来了

@Component
@Slf4j
public class NoRepeatSubmitTask {

    @Scheduled(cron = "* * * * * ?")
    public void start() {
        HashMap<String, Long> hashMap = SubmitBufferSingleton.getInstance();
        log.warn(hashMap.toString());
        for (Map.Entry<String, Long> next : hashMap.entrySet()) {
            String key = next.getKey();
            Long value = next.getValue();
            if (value > Instant.now().toEpochMilli()) {
                hashMap.remove(key);
            }
        }
        // 如果对时间没有特别严格的要求就直接clear
//        hashMap.clear();
    }
}

主启动类开启注解

@EnableScheduling //开启定时任务

测试接口,内部方法不用纠结

    @PostMapping("get")
    @RequestLimit(time = 1000)
    public ResultVO getUser(@RequestBody(required = false) UserDto userDto) {
        try {
            if (!ObjectUtils.isEmpty(userDto)) {
                userService.list(userDto);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultVOUtil.error(ResultEnum.ERROR);
        }
        return ResultVOUtil.success(ResultEnum.SUCCESS);
    }

第一次请求成功

在这里插入图片描述

多次请求后就会限流

在这里插入图片描述

查看日志,请求过于频繁了!

在这里插入图片描述

### 使用 Redis 实现滑动窗口算法进行限流的最佳实践 #### 1. 滑动窗口限流的特点 滑动窗口限流相比固定窗口限流更加精确,能够更好地处理突发流量。然而,这种实现方式确实更为复杂,并且对Redis的性能有更高的要求,在高并发场景下尤其如此[^1]。 #### 2. 利用 ZSET 数据结构 通过使用 Redis 的 `ZSET` (有序集合),可以在一定时间内记录请求的时间戳并对其进行排序。对于每一个新的请求到来时,程序会移除超过指定时间段之外的数据项,从而保持数据的有效性和准确性[^2]。 #### 3. Pipeline 提升效率 由于涉及到多次与 Redis 进行交互的操作(如增加新成员、删除过期成员以及获取当前计数值等),采用 pipeline 方式批量执行命令能显著减少网络延迟带来的开销,提高整体性能表现。 #### 4. Lua 脚本确保原子性 为了避免竞争条件的发生,推荐利用 Redis 内置的支持事务特性的 Lua 脚本来完成整个流程的一次性提交。这不仅简化了客户端代码的设计难度,同时也保障了操作的安全可靠[^4]。 --- 以下是 Python 版本的一个简单示例来展示如何基于上述原则构建一个高效的滑动窗口限流器: ```python import time from redis import StrictRedis class SlidingWindowRateLimiter(object): def __init__(self, redis_client: StrictRedis, key_prefix='swrl:', window_size=60, max_requests=100): self.redis = redis_client self.key_prefix = key_prefix self.window_size = window_size self.max_requests = max_requests def _get_key(self, identifier): return f"{self.key_prefix}{identifier}" def is_allowed(self, identifier): now_ms = int(time.time() * 1000) key = self._get_key(identifier) lua_script = """ local current_time = tonumber(ARGV[1]) local cutoff_time = current_time - tonumber(ARGV[2]) -- Remove expired entries from the sorted set. redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', '(' .. tostring(cutoff_time)) -- Count remaining items within allowed timeframe. local count = redis.call('ZCOUNT', KEYS[1], '-inf', '+inf') if tonumber(count) >= tonumber(ARGV[3]) then return 0 else redis.call('ZADD', KEYS[1], current_time, current_time) return 1 end """ result = self.redis.eval(lua_script, [key], [ str(now_ms), str(self.window_size*1000), str(self.max_requests)]) return bool(result) if __name__ == "__main__": rdb = StrictRedis(host="localhost", port=6379, db=0) limiter = SlidingWindowRateLimiter(rdb, 'test_api_', 60, 5) user_id = "user_1" for i in range(8): print(f"Attempt {i}: ", limiter.is_allowed(user_id)) ``` 此段代码定义了一个名为 `SlidingWindowRateLimiter` 类用于管理特定 API 接口下的用户访问频率控制逻辑;其中包含了两个主要方法 `_get_key()` 和 `is_allowed()` 。前者负责生成唯一标识符以便区分不同资源对象间的统计数据差异;后者则实现了核心业务功能——即依据传入的身份参数决定是否允许此次调用继续前进还是返回拒绝响应给前端应用层面上做进一步处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值