java 接口限流aop_springcloud -自定义注解+redis+spring aop 实现接口限流防刷

本文介绍了如何使用Java自定义注解、Redis和Spring AOP实现接口限流防刷功能,以提高服务的可用性。通过限制同一用户在特定时间内(如1分钟内)的调用次数,防止接口被大量调用导致系统崩溃或爬虫问题。文章详细阐述了自定义注解的定义、AOP切面的创建以及限流逻辑的实现。
摘要由CSDN通过智能技术生成

【接口防刷机制】

主要防止短时间接口被大量调用(攻击),出现系统崩溃和系统爬虫问题,提升服务的可用性。限制同一用户一定时间内(如1 min)只能访问固定次数,可以减少对业务的侵入,在服务端对系统做一层保护.

【实现方案】

本文主要是通过 自定义注解+redis+spring aop+全局异常的方式实现接口限流防刷功能。

自定义注解

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

@Documented

public @interface RequestLimit {

/**

* 调用的次数

* @return

*/

int count() default 3;

/**

* 时间段; 在time内调用的次数count -单位秒

* @return

*/

int frameTime() default 1;

/**

* 锁定时间 -单位小时

* @return

*/

int  lockTime() default  1;

}

spring AOP切面

@Slf4j

@Aspect

@Component

public class RequestLimitContract {

@Autowired

private TblUserService userService;

@Autowired

private TblBlackListService tblBlackListService;

@Autowired

private RedisClient redisClient;

//region 环绕通知

@Around("@annotation(limit)")

@Transactional(rollbackFor = Exception.class)

public Object requestLimit(ProceedingJoinPoint process, RequestLimit limit) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

long

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值