From 9546807031ad8cedb617d98659d1bd347f242c0b Mon Sep 17 00:00:00 2001 From: Chopper Date: Mon, 21 Jun 2021 10:53:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E9=87=8F=E9=99=90=E5=88=B6=E8=AE=BE?= =?UTF-8?q?=E5=AE=9A=E6=9B=B4=E5=90=88=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/aop/limiter/LimitInterceptor.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/framework/src/main/java/cn/lili/common/aop/limiter/LimitInterceptor.java b/framework/src/main/java/cn/lili/common/aop/limiter/LimitInterceptor.java index 0e35a46a..59ea7a02 100644 --- a/framework/src/main/java/cn/lili/common/aop/limiter/LimitInterceptor.java +++ b/framework/src/main/java/cn/lili/common/aop/limiter/LimitInterceptor.java @@ -1,6 +1,7 @@ package cn.lili.common.aop.limiter; import cn.lili.common.aop.limiter.annotation.LimitPoint; +import cn.lili.common.exception.ServiceException; import com.google.common.collect.ImmutableList; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -21,6 +22,7 @@ import java.lang.reflect.Method; /** * 流量拦截 + * * @author Chopper */ @Aspect @@ -42,7 +44,7 @@ public class LimitInterceptor { } @Around("execution(public * *(..)) && @annotation(cn.lili.common.aop.limiter.annotation.LimitPoint)") - public Object interceptor(ProceedingJoinPoint pjp) { + public Object interceptor(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); LimitPoint limitPointAnnotation = method.getAnnotation(LimitPoint.class); @@ -65,22 +67,27 @@ public class LimitInterceptor { try { Number count = redisTemplate.execute(limitScript, keys, limitCount, limitPeriod); log.info("Access try count is {} for name={} and key = {}", count, name, key); - if (count != null && count.intValue() <= limitCount) { + // 如果缓存里没有值,或者他的值小于限制频率 + if (count.intValue() <= limitCount) { return pjp.proceed(); } else { - throw new RuntimeException("访问过于频繁,请稍后再试"); + throw new ServiceException("访问过于频繁,请稍后再试"); } - } catch (Throwable e) { - if (e instanceof RuntimeException) { - throw new RuntimeException(e.getLocalizedMessage()); + } catch (NullPointerException e) { + return pjp.proceed(); + } catch (Exception e) { + if (e instanceof ServiceException) { + throw new ServiceException(e.getLocalizedMessage()); } throw new RuntimeException("服务器异常,请稍后再试"); } } + //默认unknown常量值 private static final String UNKNOWN = "unknown"; + //获取ip public String getIpAddress() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String ip = request.getHeader("x-forwarded-for");