From 08d39d4593c9fd21eccf75e11e2c09eb3f36af00 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 16 Sep 2023 12:01:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=A6=81=E7=94=A8=20resilien?= =?UTF-8?q?ce4j=20=E4=BE=9D=E8=B5=96=EF=BC=8C=E4=BF=9D=E8=AF=81=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao-spring-boot-starter-protection/pom.xml | 1 + .../yudao-spring-boot-starter-web/pom.xml | 7 ------- .../web/core/handler/GlobalExceptionHandler.java | 15 ++++++++------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml index 4cb4727eb..5e19511c4 100644 --- a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml @@ -31,6 +31,7 @@ io.github.resilience4j resilience4j-spring-boot2 + true diff --git a/yudao-framework/yudao-spring-boot-starter-web/pom.xml b/yudao-framework/yudao-spring-boot-starter-web/pom.xml index e591cc329..50d986c06 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-web/pom.xml @@ -55,13 +55,6 @@ ${revision} - - - io.github.resilience4j - resilience4j-ratelimiter - provided - - org.jsoup diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java index bf241ba57..303b645a3 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java @@ -11,7 +11,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; -import io.github.resilience4j.ratelimiter.RequestNotPermitted; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -33,6 +32,7 @@ import javax.validation.ConstraintViolationException; import javax.validation.ValidationException; import java.time.LocalDateTime; import java.util.Map; +import java.util.Objects; import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*; @@ -83,9 +83,6 @@ public class GlobalExceptionHandler { if (ex instanceof HttpRequestMethodNotSupportedException) { return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); } - if (ex instanceof RequestNotPermitted) { - return requestNotPermittedExceptionHandler(request, (RequestNotPermitted) ex); - } if (ex instanceof ServiceException) { return serviceExceptionHandler((ServiceException) ex); } @@ -186,8 +183,7 @@ public class GlobalExceptionHandler { /** * 处理 Resilience4j 限流抛出的异常 */ - @ExceptionHandler(value = RequestNotPermitted.class) - public CommonResult requestNotPermittedExceptionHandler(HttpServletRequest req, RequestNotPermitted ex) { + public CommonResult requestNotPermittedExceptionHandler(HttpServletRequest req, Throwable ex) { log.warn("[requestNotPermittedExceptionHandler][url({}) 访问过于频繁]", req.getRequestURL(), ex); return CommonResult.error(TOO_MANY_REQUESTS); } @@ -226,7 +222,12 @@ public class GlobalExceptionHandler { return tableNotExistsResult; } - // 情况二:处理异常 + // 情况二:部分特殊的库的处理 + if (Objects.equals("io.github.resilience4j.ratelimiter.RequestNotPermitted", ex.getClass().getName())) { + return requestNotPermittedExceptionHandler(req, ex); + } + + // 情况三:处理异常 log.error("[defaultExceptionHandler]", ex); // 插入异常日志 this.createExceptionLog(req, ex);