SpringBoot实现一个Redis限流注解

springBoot实现的一个限流注解

文章目录

目录

文章目录

一、使用步骤

1.引入库

2.代码实现

1.添加注解

2.新增限流AOP实现

3.实现代码的拦截

4.最终结果

总结

一、使用步骤

1.引入库

代码如下(示例):

XML 复制代码
        <!-- 引入SpringBoot Aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2.代码实现

1.添加注解

java 复制代码
package com.hhh.springai_test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisLimiting {

    int number() default 3;

    int time() default 60;

    String message() default "请求过于频繁,请稍后再试";


}

2.新增限流AOP实现

java 复制代码
package com.hhh.springai_test.aop;

import cn.hutool.crypto.digest.MD5;
import com.hhh.springai_test.annotation.RedisLimiting;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Aspect
@Component("redisLimitingAspect")
@Slf4j
public class RedisLimitingAspect {

    @Autowired
    private RedisUtils redisUtils;

    @Around("@annotation(com.hhh.springai_test.annotation.RedisLimiting)")  // 只拦截带 @redisLimiting 的方法
    public Object redisLimiting(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod(); // 直接获取被代理的方法
        // 获取 @redisLimiting 注解
        RedisLimiting annotation = method.getAnnotation(RedisLimiting.class);
        if (annotation == null) {
            return joinPoint.proceed(); // 没有注解,直接执行方法
        }
        int limit = annotation.number(); // 限制次数
        int expire = annotation.time();  // 过期时间
        String message = annotation.message();

        log.info("拦截方法: {}, 限流 key: {}, 限流次数: {}, 过期时间: {} 秒",
                method.getName(), limit, expire);

        // 执行限流逻辑
        boolean isAllowed = checkRedisLimiting(method, joinPoint.getArgs(), limit, expire);
        if (!isAllowed) {
            throw new BusinessException(ErrorCode.BUSY_ERROR,message);
        }

        return joinPoint.proceed(); // 执行原方法
    }

    private boolean checkRedisLimiting(Method method, Object[] args, int limit, int expire) {
        // 生成 Redis Key
        String redisKey = generateRedisKey(method, args);
        // 查询 Redis 是否存在
        Object o = redisUtils.get(redisKey);
        if (o == null) {
            redisUtils.setex(redisKey, 1, expire); // 初始值设为1,并设置过期时间
            return true;
        } else {
            int count = Integer.parseInt(o.toString());
            if (count >= limit) {
                return false; // 超过限制
            } else {
                redisUtils.increment(redisKey, 1); // 递增计数
                return true;
            }
        }
    }

    private String generateRedisKey(Method method, Object[] args) {
        StringBuilder builder = new StringBuilder();
        builder.append(method.getDeclaringClass().getName()).append(":").append(method.getName()).append(":");
        Parameter[] parameters = method.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            builder.append(parameters[i].getName()).append("=").append(args[i]).append("&");
        }
        return MD5.create().digestHex16(builder.toString()); // 生成唯一 Redis Key
    }
}

3.实现代码的拦截

java 复制代码
@GetMapping("/getAllModel")
    @RedisLimiting(number = 3, time = 60,message = "不要再请求我的获取aiModel方法了")
    public BaseResponse<List<AiModelVO>> getAllModel() {
        return ResultUtils.success(aiModelService.getAllModel());
    }

4.最终结果


总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

相关推荐
李日灐5 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
是梦终空5 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统
My LQS5 小时前
使用 Redis Stack 向量索引构建大模型问答缓存系统
redis·缓存·ai
qq_297574675 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
郝学胜-神的一滴6 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
Tony Bai6 小时前
“Go 2,请不要发生!”:如果 Go 变成了“缝合怪”,你还会爱它吗?
开发语言·后端·golang
Victor3566 小时前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor3566 小时前
MongoDB(1)什么是MongoDB?
后端
Victor35612 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor35612 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端