通过注解实现请求频率限制

1.首先创建注解类RequestLimit

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 RequestLimit {
    /**
     * 两次请求的最小间隔时间(ms)
     * @return
     */
    int interval() default -1;
}

2.创建切面类RepeatRequestAspect


import com.alibaba.fastjson.JSON;
import com.atguigu.cache.annotations.RequestLimit;
import com.atguigu.cache.enums.BusinessExceptionEnum;
import com.atguigu.cache.exception.BusinessException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class RepeatRequestAspect {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;

    private String REQUEST_PRFIX = "api:request:REQUEST_%s_%d";

    private Long OFFSET = 1577808000000L;

    @Around("@annotation(limit)")
    public Object around(ProceedingJoinPoint pjp, RequestLimit limit) throws Throwable {
        if (limit.interval() == -1) {
            return pjp.proceed();
        }
        Long userId = getUserId();
        String path = String.format(REQUEST_PRFIX, request.getServletPath(), userId);
        limitInterval(limit, path);
        return pjp.proceed();
    }

    private void limitInterval(RequestLimit limit, String path) throws BusinessException {
        path = path.replaceAll("/", "");
        Boolean hasKey = redisTemplate.hasKey(path);
        if (hasKey) {
            String timeJson = (String) redisTemplate.boundValueOps(path).get();
            Long beforeTime = Long.valueOf(timeJson);
            Long compareTime = System.currentTimeMillis() - OFFSET - beforeTime;
            int count = limit.interval();
            if (compareTime < Long.valueOf(count)) {
                throw new BusinessException(BusinessExceptionEnum.TOO_MANY_REQUESTS);
            }

        }
        String s = JSON.toJSONString(System.currentTimeMillis() - OFFSET);
        redisTemplate.boundValueOps(path).set(s,1000*20,TimeUnit.MILLISECONDS);
    }

    public Long getUserId() {
        //简写token校验用户。。。
        String userId = request.getHeader("userId");
        return Long.valueOf(userId);
    }
}

@Around("@annotation(limit)")
public Object around(ProceedingJoinPoint pjp, RequestLimit limit)

注意这里的写法:

@annotation(limit) 这里的limit,是spring提供的根据上下文解析到的参数里的RequestLimit limit ,表示切面表达式是以加了该注解为切面进行方法增强。并且在around方法中可以获取RequestLimit limit里面设置的参数(interval )。ProceedingJoinPoint pjp这个参数:环绕通知 ProceedingJoinPoint 执行proceed方法的作用是让目标方法执行,这也是环绕通知和前置、后置通知方法的一个最大区别。简单理解,环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的.

本次只写了限制两次请求间隔时间:

实现原理:将上一次的请求url+用户id作为key存入redis,使用时间戳-固定时间戳作为value(这样做是为了‘压缩’存入redis的数据值);下次请求来的时候判断间隔时间就行了。

3.使用该注解进行访问限制

    @GetMapping("/limit")
    @RequestLimit(interval = 5000)
    public String limit() {
        return "normal";
    }

注意:

需要添加的依赖有:

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>

关于次数限制和次数加频率限制,相信大家看了这篇博客之后稍加思考就能搞定,3q大家的观看!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现接口调用频率限制可以使用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次,超过次数会抛出异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值