当前位置:   article > 正文

springboot整合redis+自定义注解+反射+aop实现接口限流_自定义注解 + redis + aop 实现接口限流,

自定义注解 + redis + aop 实现接口限流,

为什么我们要对接口进行限流呢?

因为在面对大并发大流量的请求时,突发情况下,大量的请求会将系统整垮,造成响应失败超时等状况。那为了防止出现这种情况最常见的解决方案之一就是限流,当请求达到一定的并发数或速率,就进行等待、排队、降级、拒绝服务等。

本文基于限流反射之一的计数器方式来实现限流

1.定义注解

  1. package com.cwf.framework.limit.annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Target(ElementType.METHOD)
  8. public @interface Limit {
  9. /**
  10. * 资源的key,唯一
  11. * 作用:不同的接口,不同的流量控制
  12. */
  13. String key() default "";
  14. /**
  15. * 最多的访问限制次数
  16. */
  17. long count() default 7;
  18. /**
  19. * 过期时间也可以理解为单位时间,单位秒,默认60
  20. */
  21. long expire() default 60;
  22. /**
  23. * 提示语
  24. */
  25. String msg() default "系统繁忙,请稍后再试!";
  26. }

2.aop+redis

利用redis 的incr 高并发 原子性计数器来计数,当请求次数超过规定次数就抛出自定义异常,全局异常捕获异常信息返回给前台

  1. package com.cwf.framework.limit.aspectj;
  2. import com.cwf.common.core.controller.BaseController;
  3. import com.cwf.common.exception.base.MyException;
  4. import com.cwf.common.utils.ip.IpUtils;
  5. import com.cwf.framework.limit.annotation.Limit;
  6. import com.cwf.framework.redis.RedisService;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.context.request.RequestAttributes;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.util.concurrent.TimeUnit;
  18. /**
  19. * 限流服务
  20. */
  21. @SuppressWarnings("all")
  22. @Slf4j
  23. @Component
  24. @Aspect
  25. public class LimitAOP extends BaseController {
  26. @Autowired
  27. private RedisService redisService;
  28. @Pointcut("@annotation(com.cwf.framework.limit.annotation.Limit)")
  29. private void LimitPointcut() {
  30. }
  31. @Before("@annotation(limit)")
  32. public void before(JoinPoint point, Limit limit) throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
  33. // 获取RequestAttributes
  34. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  35. // 从获取RequestAttributes中获取HttpServletRequest的信息
  36. assert requestAttributes != null;
  37. HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
  38. assert request != null;
  39. String path = request.getRequestURI();
  40. String ip = IpUtils.getIpAddress(request);
  41. //获取注解信息
  42. String key = limit.key();
  43. long count = limit.count();
  44. String msg = limit.msg();
  45. long expire = limit.expire();
  46. String redisKey = ip+path+key+getUserId();
  47. Long inc = redisService.inc(redisKey);
  48. if (inc==1){
  49. redisService.expire(redisKey, expire, TimeUnit.SECONDS);
  50. }
  51. if (inc > count){
  52. log.error("\n{}触发限制流量",key);
  53. throw new MyException(msg,400);
  54. }
  55. }
  56. }


1.使用

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号