maven
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
//配置
aop:
proxy-target-class: true
接口限流
接口限流全局操作通常使用令牌算法,漏桶算法。
这篇文章讲的较好,
https://blog.youkuaiyun.com/zrg523/article/details/82185088
接入层有分布式限流,nginx限流。
本文使用注解方式在springboot中限流。
声明限流类,编写AOP实现功能
@Slf4j
@Aspect
@Component
public class LimitAspect {
private final RedisTemplate<String, Serializable> limitRedisTemplate;
@Autowired
public LimitAspect(RedisTemplate<String, Serializable> limitRedisTemplate) {
this.limitRedisTemplate = limitRedisTemplate;
}
@Pointcut("@annotation(annotation.Limit)")
public void pointcut() {
// do nothing
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull