SpringBoot-AOP使用@Aspect切面实现请求接口频率限制

前言:在实际开发中,我们可能会遇到需要对某个接口请求频率做一定时间间隔的限制,如生活中常见的应用上二维码刷新频率限制等。于是这里做了一个简单的切面限制频率案例,使用的是切面注解方式,减少侵入性。

一、切面实现请求接口频率限制

1.pom.xml引入

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2.自定义一个注解

import java.lang.annotation.*;

/**
 * 用于限制接口请求频率
 */
@Documented
@Target({
   ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReqLimit {
   

    /**
     * 请求频率限制(单位秒)默认3秒可自己调整
     */
    int rateSecond() default 3;
}

3.创建一个@Aspect的切面类,用来处理核心逻辑

@Aspect
@Component
@Order(1)
public class ReqLimitAspect {
   

   public static final String REQUEST_LIMIT = "requestLimit";

    /**
     * 频率限制切入点(注解类的路径)
     */
    @Pointcut(value = "@annotation(com.alone.server.annotation.ReqLimit)")
    public void reqLimitPointCut() 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现接口调用频率限制可以使用AOP和ConcurrentHashMap结合的方式。 首先,在Spring Boot中,我们可以使用AOP来拦截接口的调用。我们可以定义一个切面使用@Aspect注解标注,然后在切入点方法中定义需要拦截的注解。 例如,我们可以定义一个@FrequencyLimit注解,用于标注需要限制调用频率的方法: ```java @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface FrequencyLimit { // 限制时间段,单位为秒 int interval() default 60; // 时间段内最大请求次数 int maxCount() default 10; } ``` 然后,在切面中,我们可以拦截该注解标注的方法,并且进行限制调用频率的操作。可以使用ConcurrentHashMap来存储每个接口的调用次数和最后一次调用时间。 ```java @Component @Aspect public class FrequencyLimitAspect { private ConcurrentHashMap<String, Long> lastRequestTimeMap = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, Integer> requestCountMap = new ConcurrentHashMap<>(); @Around("@annotation(frequencyLimit)") public Object frequencyLimit(ProceedingJoinPoint joinPoint, FrequencyLimit frequencyLimit) throws Throwable { Object result = null; String methodName = joinPoint.getSignature().toLongString(); long currentTime = System.currentTimeMillis(); int interval = frequencyLimit.interval(); int maxCount = frequencyLimit.maxCount(); synchronized (this) { // 获取最后一次请求时间和请求次数 Long lastRequestTime = lastRequestTimeMap.get(methodName); Integer requestCount = requestCountMap.get(methodName); if (lastRequestTime == null || currentTime - lastRequestTime >= interval * 1000) { // 如果该接口限制时间段内没有被调用过,则重置请求次数和最后一次请求时间 lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, 1); } else { // 如果该接口限制时间段内已经被调用过,则增加请求次数 requestCount++; if (requestCount > maxCount) { // 如果请求次数超过了限制,则抛出异常 throw new RuntimeException("Exceeded maximum request limit"); } lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, requestCount); } } // 调用原始方法 result = joinPoint.proceed(); return result; } } ``` 在切面中,我们使用synchronized关键字来保证线程安全,因为ConcurrentHashMap并不能完全保证线程安全。同时,我们使用了@Around注解来拦截被@FrequencyLimit注解标注的方法,然后在方法中实现限制调用频率的逻辑。 这样,我们就可以实现接口调用频率限制了。在需要限制调用频率的方法中,我们只需要加上@FrequencyLimit注解即可。例如: ```java @GetMapping("/test") @FrequencyLimit(interval = 60, maxCount = 10) public String test() { return "test"; } ``` 这样,每个IP地址每分钟内最多只能调用该方法10次,超过次数会抛出异常。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值