使用Redis实现接口防抖

说明:实际开发中,我们在前端页面上点击了一个按钮,访问了一个接口,这时因为网络波动或者其他原因,页面上没有反应,用户可能会在短时间内再次点击一次或者用户以为没有点到,很快的又点了一次。导致短时间内发送了两个请求到后台,可能会导致数据重复添加。

为了避免短时间内对一个接口访问,我们可以通过AOP+自定义注解+Redis的方式,在接口上加一个自定义注解,然后通过AOP的前置通知,在Redis中存入一个有效期的值,当访问接口时这个值还未过期,则抛出异常,以此来避免短时间内对接口的方法。

实现

第一步:创建一个自定义注解,设置两个属性,一个是key,一个是这个key在Redis中的有效时间;

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 LimitAccess {

    /**
     * 限制访问的key
     * @return
     */
    String key();

    /**
     * 限制访问时间
     * @return
     */
    int times();
}

第二步:创建对应的Aspect;

import com.hezy.annotation.LimitAccess;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/**
 * AOP类(通知类)
 */
@Component
@Aspect
public class LimitAspect {

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("@annotation(com.hezy.annotation.LimitAccess)")
    public void pt(){};

    @Around("pt()")
    public Object aopAround(ProceedingJoinPoint pjp) throws Throwable {
        // 获取切入点上面的自定义注解
        Signature signature = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;

        // 获取方法上面的注解
        LimitAccess limitAccess = methodSignature.getMethod().getAnnotation(LimitAccess.class);

        // 获取注解上面的属性
        int limit = limitAccess.times();
        String key = limitAccess.key();

        // 根据key去找Redis中的值
        Object o = redisTemplate.opsForValue().get(key);

        // 如果不存在,说明是首次访问,存入Redis,过期时间为limitAccess中的time
        if (o == null) {
            redisTemplate.opsForValue().set(key, "", limit, TimeUnit.SECONDS);
            // 执行切入点的方法
            return pjp.proceed();
        } else {
            // 如果存在,说明不是首次访问,抛出异常
            throw new RuntimeException("访问过于频繁");
        }
    }
}

第三步:在需要限制的接口上,加上这个注解,并设置key和限制访问时间,如下这个这个接口,设置key为limit,实际开发中可以设置一个随机值,或者按照规则自定义设置,times为限制访问时间;

    @GetMapping("/limit")
    @LimitAccess(key = "limit", times = 10)
    public String limit() {
        return "success";
    }

演示

启动项目,访问该接口;

(首次访问,没得问题,同时在Redis中存入值)

在这里插入图片描述


(短时间内,连续访问,因为Redis中值未过期)

在这里插入图片描述

在这里插入图片描述


(10秒之后再访问,又可以了,Redis中的值过期了)

在这里插入图片描述

以上就是使用Redis实现接口防抖,避免短时间内连续访问接口。实际开发中,可以将异常设置为自定义异常。

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot结合Redis实现接口限的步骤如下[^1][^2]: 1. 引入依赖:在Spring Boot项目的pom.xml文件中添加Redis和AOP的相关依赖。 2. 配置RedisTemplate:在Spring Boot的配置文件中配置Redis连接信息,包括主机名、端口号、密码等。 3. 创建自定义注解:使用@RateLimiter注解来标记需要进行接口限流的方法。 4. 编写切面类:创建一个切面类,使用@Aspect注解标记,并在该类中编写切点和通知方法。 5. 实现接口限流逻辑:在通知方法中,使用Redis的原子操作来实现接口限流的逻辑。可以使用Redis的incr命令来对接口的访问次数进行计数,然后根据设定的阈值来判断是否限流。 6. 配置切面:在Spring Boot的配置类中,使用@EnableAspectJAutoProxy注解开启AOP功能,并将切面类添加到容器中。 7. 在需要进行接口限流的方法上添加注解:在需要进行接口限流的方法上添加@RateLimiter注解,并配置相关参数,如限流的阈值、时间窗口大小等。 8. 测试接口限流效果:启动Spring Boot应用程序,并访问被限流的接口,观察接口的访问频率是否受到限制。 以下是一个示例代码,演示了如何使用Spring Boot和Redis实现接口限流: ```java // 1. 创建自定义注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RateLimiter { int value() default 10; // 默认限流阈值为10 int window() default 60; // 默认时间窗口为60秒 } // 2. 编写切面类 @Aspect @Component public class RateLimiterAspect { @Autowired private RedisTemplate<String, String> redisTemplate; @Around("@annotation(rateLimiter)") public Object around(ProceedingJoinPoint joinPoint, RateLimiter rateLimiter) throws Throwable { String methodName = joinPoint.getSignature().getName(); String key = "rate_limiter:" + methodName; int limit = rateLimiter.value(); int window = rateLimiter.window(); // 使用Redis的incr命令对接口的访问次数进行计数 Long count = redisTemplate.opsForValue().increment(key, 1); if (count == 1) { // 设置过期时间,保证计数器在一定时间后自动清零 redisTemplate.expire(key, window, TimeUnit.SECONDS); } if (count > limit) { // 超过限流阈值,抛出异常或返回错误信息 throw new RuntimeException("接口访问频率超过限制"); } // 执行原方法 return joinPoint.proceed(); } } // 3. 在需要进行接口限流的方法上添加注解 @RestController public class DemoController { @RateLimiter(value = 5, window = 60) // 每分钟最多访问5次 @GetMapping("/demo") public String demo() { return "Hello World!"; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何中应

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值