From a60d481c0d80eb39f379c003ebba11659af46623 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Mon, 23 Sep 2024 17:13:22 +0800 Subject: [PATCH] =?UTF-8?q?perf(X-Pack):=20=E5=90=8E=E5=8F=B0=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=BB=AA=E8=A1=A8=E6=9D=BF=E5=B9=B6=E5=8F=91=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- de-xpack | 2 +- .../main/java/io/dataease/traffic/DeTraffic.java | 2 +- .../java/io/dataease/traffic/DeTrafficAop.java | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/de-xpack b/de-xpack index c8534a3fe6..f0cccdc95b 160000 --- a/de-xpack +++ b/de-xpack @@ -1 +1 @@ -Subproject commit c8534a3fe6d2892bc519d7eeb345a74ebac414c7 +Subproject commit f0cccdc95b184735f6ea73ff82eb7d84423e1464 diff --git a/sdk/common/src/main/java/io/dataease/traffic/DeTraffic.java b/sdk/common/src/main/java/io/dataease/traffic/DeTraffic.java index 4daaa7fc5a..7999cb6434 100644 --- a/sdk/common/src/main/java/io/dataease/traffic/DeTraffic.java +++ b/sdk/common/src/main/java/io/dataease/traffic/DeTraffic.java @@ -6,7 +6,7 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DeTraffic { - int value() default 2; + int value() default 0; String api(); } diff --git a/sdk/common/src/main/java/io/dataease/traffic/DeTrafficAop.java b/sdk/common/src/main/java/io/dataease/traffic/DeTrafficAop.java index 0c4550fca2..ea9cd86b27 100644 --- a/sdk/common/src/main/java/io/dataease/traffic/DeTrafficAop.java +++ b/sdk/common/src/main/java/io/dataease/traffic/DeTrafficAop.java @@ -6,11 +6,11 @@ import io.dataease.traffic.dao.mapper.CoreApiTrafficMapper; import io.dataease.utils.IDUtils; import io.dataease.utils.LogUtil; import jakarta.annotation.Resource; -import org.apache.commons.lang3.ObjectUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @@ -22,6 +22,9 @@ public class DeTrafficAop { @Resource private CoreApiTrafficMapper coreApiTrafficMapper; + @Value("${dataease.traffic:2}") + private Integer defaultTraffic; + final private static String errorMsg = "当前API【%s】设定并发阈值为【%s】,现已经达到限流阈值,请稍后再试!"; @Around(value = "@annotation(io.dataease.traffic.DeTraffic)") @@ -29,9 +32,14 @@ public class DeTrafficAop { MethodSignature ms = (MethodSignature) point.getSignature(); Method method = ms.getMethod(); DeTraffic traffic = method.getAnnotation(DeTraffic.class); + int value = traffic.value(); + if (value == 0) { + value = defaultTraffic; + } String api = traffic.api(); Object result = null; + boolean access = false; try { Integer count = coreApiTrafficMapper.apiCount(api); if (count == 0) { @@ -41,19 +49,21 @@ public class DeTrafficAop { apiTraffic.setThreshold(value); apiTraffic.setApi(api); coreApiTrafficMapper.insert(apiTraffic); + access = true; result = point.proceed(); return result; } int alive = coreApiTrafficMapper.getAlive(api); if (alive < value) { coreApiTrafficMapper.upgrade(api); + access = true; result = point.proceed(); return result; } } catch (Exception e) { LogUtil.error(e.getMessage(), e); } finally { - if (ObjectUtils.isNotEmpty(result)) { + if (access) { coreApiTrafficMapper.releaseAlive(api); } }