springboot:自定义注解+redis实现接口限流

api接口限流是高并发系统的重要保护措施之一
开发步骤:
1、自定义一个AccessLimit注解,主要有两个变量maxCount和second

import java.lang.annotation.*;

@Inherited
@Documented
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessLimit {

    /**
     * 指定时间内API请求次数
     * @return
     */
    int maxCount() default 5;

    /**
     * 请求次数的指定时间间隔(redis数据过期时间)
     * @return
     */
    int second() default 60;
}

2、定义一个类实现HandlerInterceptor的preHandle方法对标注解的方法进行拦截处理

@Slf4j
@Component
public class AccessLimiterInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果不是映射到方法直接放行
        if (!(handler instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // 是否有AccessLimit注解
        if (!method.isAnnotationPresent(AccessLimit.class)){
            return true;
        }
        AccessLimit accessLimit = method.getAnnotation(AccessLimit.class);
        int maxCount = accessLimit.maxCount();
        int second = accessLimit.second();
        // 存储key
        String key = request.getRemoteAddr() + ":" + request.getContextPath() + ":" + request.getServletPath();
        // 已经访问次数
        String countStr = ObjectCacheUtil.get(key);
        if (Objects.isNull(countStr)){
            ObjectCacheUtil.set(key, "1", second);
            return true;
        }
        int count = Integer.parseInt(countStr);
        if (count < maxCount){
            ObjectCacheUtil.increment(key, 1);
            return true;
        }
        throw new BaseException("请求次数已达上限");
    }
}

3、将自定义的HandlerInterceptor交给InterceptorRegistry容器进行管理

@Configuration
public class WebWvcConfig implements WebMvcConfigurer {

    @Autowired
    private AccessLimiterInterceptor accessLimiterInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accessLimiterInterceptor);
    }
}

4、接口测试

@PostMapping("test")
@AccessLimit(maxCount = 2, second = 30)
public BiResponseDTO<String> test() {
    return BiResponseDTO.success();
}
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Redisson 实现分布式锁,具体实现如下: 1. 引入 Redisson 依赖: ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.16.1</version> </dependency> ``` 2. 定义自定义注解 `DistributedLock`: ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface DistributedLock { String value() default ""; long leaseTime() default 30L; TimeUnit timeUnit() default TimeUnit.SECONDS; } ``` 3. 在需要加锁的方法上加上 `@DistributedLock` 注解: ```java @Service public class UserService { @DistributedLock("user:#{#userId}") public User getUserById(String userId) { // ... } } ``` 4. 实现 `DistributedLockAspect` 切面: ```java @Aspect @Component public class DistributedLockAspect { private final RedissonClient redissonClient; public DistributedLockAspect(RedissonClient redissonClient) { this.redissonClient = redissonClient; } @Around("@annotation(distributedLock)") public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable { String lockKey = distributedLock.value(); if (StringUtils.isEmpty(lockKey)) { lockKey = joinPoint.getSignature().toLongString(); } RLock lock = redissonClient.getLock(lockKey); boolean locked = lock.tryLock(distributedLock.leaseTime(), distributedLock.timeUnit()); if (!locked) { throw new RuntimeException("获取分布式锁失败"); } try { return joinPoint.proceed(); } finally { lock.unlock(); } } } ``` 5. 在 `application.yml` 中配置 Redisson: ```yaml spring: redis: host: localhost port: 6379 password: database: 0 redisson: single-server-config: address: redis://${spring.redis.host}:${spring.redis.port} ``` 这样就实现了一个基于 Redis 的分布式锁。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值