温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot如何在一定时间内限制接口请求次数

发布时间:2022-03-16 13:34:29 来源:亿速云 阅读:1381 作者:iii 栏目:开发技术

这篇文章主要介绍“SpringBoot如何在一定时间内限制接口请求次数”,在日常操作中,相信很多人在SpringBoot如何在一定时间内限制接口请求次数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot如何在一定时间内限制接口请求次数”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

需要用到的知识:注解、AOP、ExpiringMap(带有有效期的映射)

我们可以自定义注解,把注解添加到我们的接口上。定义一个切面,执行方法前去ExpiringMap查询该IP在规定时间内请求了多少次,如超过次数则直接返回请求失败。

需要用到的依赖

<!-- AOP依赖 -->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.1.5.RELEASE</version>
</dependency>
<!-- Map依赖 -->
<dependency>
            <groupId>net.jodah</groupId>
            <artifactId>expiringmap</artifactId>
            <version>0.5.8</version>
</dependency>

自定义注解@LimitRequest

@Documented
@Target(ElementType.METHOD) // 说明该注解只能放在方法上面
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitRequest {
    long time() default 6000; // 限制时间 单位:毫秒
    int count() default 1; // 允许请求的次数
}

自定义AOP

@Aspect
@Component
public class LimitRequestAspect {
 
    private static ConcurrentHashMap<String, ExpiringMap<String, Integer>> book = new ConcurrentHashMap<>();
 
    // 定义切点
    // 让所有有@LimitRequest注解的方法都执行切面方法
    @Pointcut("@annotation(limitRequest)")
    public void excudeService(LimitRequest limitRequest) {
    }
 
    @Around("excudeService(limitRequest)")
    public Object doAround(ProceedingJoinPoint pjp, LimitRequest limitRequest) throws Throwable {
 
        // 获得request对象
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        
        // 获取Map对象, 如果没有则返回默认值
        // 第一个参数是key, 第二个参数是默认值
        ExpiringMap<String, Integer> uc = book.getOrDefault(request.getRequestURI(), ExpiringMap.builder().variableExpiration().build());
        Integer uCount = uc.getOrDefault(request.getRemoteAddr(), 0);
 
 
 
        if (uCount >= limitRequest.count()) { // 超过次数,不执行目标方法
            return "接口请求超过次数";
        } else if (uCount == 0){ // 第一次请求时,设置有效时间
//            /** Expires entries based on when they were last accessed */
//            ACCESSED,
//            /** Expires entries based on when they were created */
//            CREATED;
            uc.put(request.getRemoteAddr(), uCount + 1, ExpirationPolicy.CREATED, limitRequest.time(), TimeUnit.MILLISECONDS);
        } else { // 未超过次数, 记录加一
            uc.put(request.getRemoteAddr(), uCount + 1);
        }
        book.put(request.getRequestURI(), uc);
 
        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
 
        return result;
    }
 
 
}

第一个静态Map是多线程安全的Map(ConcurrentHashMap),它的key是接口对于的url,它的value是一个多线程安全且键值对是有有效期的Map(ExpiringMap)。

ExpiringMap的key是请求的ip地址,value是已经请求的次数。

ExpiringMap更多的使用方法可以参考:https://github.com/jhalterman/expiringmap

SpringBoot如何在一定时间内限制接口请求次数

最后在方法上面加上@LimitRequest就行了

SpringBoot如何在一定时间内限制接口请求次数

到此,关于“SpringBoot如何在一定时间内限制接口请求次数”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI