Commit 08d39d4593c9fd21eccf75e11e2c09eb3f36af00

Authored by YunaiV
1 parent 1f327ef8

默认禁用 resilience4j 依赖,保证启动速度

yudao-framework/yudao-spring-boot-starter-protection/pom.xml
@@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
31 <dependency> 31 <dependency>
32 <groupId>io.github.resilience4j</groupId> 32 <groupId>io.github.resilience4j</groupId>
33 <artifactId>resilience4j-spring-boot2</artifactId> 33 <artifactId>resilience4j-spring-boot2</artifactId>
  34 + <optional>true</optional>
34 </dependency> 35 </dependency>
35 </dependencies> 36 </dependencies>
36 37
yudao-framework/yudao-spring-boot-starter-web/pom.xml
@@ -55,13 +55,6 @@ @@ -55,13 +55,6 @@
55 <version>${revision}</version> 55 <version>${revision}</version>
56 </dependency> 56 </dependency>
57 57
58 - <!-- 服务保障相关 -->  
59 - <dependency>  
60 - <groupId>io.github.resilience4j</groupId>  
61 - <artifactId>resilience4j-ratelimiter</artifactId>  
62 - <scope>provided</scope> <!-- 设置为 provided,主要是 GlobalExceptionHandler 使用 -->  
63 - </dependency>  
64 -  
65 <!-- xss --> 58 <!-- xss -->
66 <dependency> 59 <dependency>
67 <groupId>org.jsoup</groupId> 60 <groupId>org.jsoup</groupId>
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; @@ -11,7 +11,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
11 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; 11 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
12 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; 12 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
13 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; 13 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
14 -import io.github.resilience4j.ratelimiter.RequestNotPermitted;  
15 import lombok.AllArgsConstructor; 14 import lombok.AllArgsConstructor;
16 import lombok.extern.slf4j.Slf4j; 15 import lombok.extern.slf4j.Slf4j;
17 import org.apache.commons.lang3.exception.ExceptionUtils; 16 import org.apache.commons.lang3.exception.ExceptionUtils;
@@ -33,6 +32,7 @@ import javax.validation.ConstraintViolationException; @@ -33,6 +32,7 @@ import javax.validation.ConstraintViolationException;
33 import javax.validation.ValidationException; 32 import javax.validation.ValidationException;
34 import java.time.LocalDateTime; 33 import java.time.LocalDateTime;
35 import java.util.Map; 34 import java.util.Map;
  35 +import java.util.Objects;
36 36
37 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*; 37 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
38 38
@@ -83,9 +83,6 @@ public class GlobalExceptionHandler { @@ -83,9 +83,6 @@ public class GlobalExceptionHandler {
83 if (ex instanceof HttpRequestMethodNotSupportedException) { 83 if (ex instanceof HttpRequestMethodNotSupportedException) {
84 return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); 84 return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
85 } 85 }
86 - if (ex instanceof RequestNotPermitted) {  
87 - return requestNotPermittedExceptionHandler(request, (RequestNotPermitted) ex);  
88 - }  
89 if (ex instanceof ServiceException) { 86 if (ex instanceof ServiceException) {
90 return serviceExceptionHandler((ServiceException) ex); 87 return serviceExceptionHandler((ServiceException) ex);
91 } 88 }
@@ -186,8 +183,7 @@ public class GlobalExceptionHandler { @@ -186,8 +183,7 @@ public class GlobalExceptionHandler {
186 /** 183 /**
187 * 处理 Resilience4j 限流抛出的异常 184 * 处理 Resilience4j 限流抛出的异常
188 */ 185 */
189 - @ExceptionHandler(value = RequestNotPermitted.class)  
190 - public CommonResult<?> requestNotPermittedExceptionHandler(HttpServletRequest req, RequestNotPermitted ex) { 186 + public CommonResult<?> requestNotPermittedExceptionHandler(HttpServletRequest req, Throwable ex) {
191 log.warn("[requestNotPermittedExceptionHandler][url({}) 访问过于频繁]", req.getRequestURL(), ex); 187 log.warn("[requestNotPermittedExceptionHandler][url({}) 访问过于频繁]", req.getRequestURL(), ex);
192 return CommonResult.error(TOO_MANY_REQUESTS); 188 return CommonResult.error(TOO_MANY_REQUESTS);
193 } 189 }
@@ -226,7 +222,12 @@ public class GlobalExceptionHandler { @@ -226,7 +222,12 @@ public class GlobalExceptionHandler {
226 return tableNotExistsResult; 222 return tableNotExistsResult;
227 } 223 }
228 224
229 - // 情况二:处理异常 225 + // 情况二:部分特殊的库的处理
  226 + if (Objects.equals("io.github.resilience4j.ratelimiter.RequestNotPermitted", ex.getClass().getName())) {
  227 + return requestNotPermittedExceptionHandler(req, ex);
  228 + }
  229 +
  230 + // 情况三:处理异常
230 log.error("[defaultExceptionHandler]", ex); 231 log.error("[defaultExceptionHandler]", ex);
231 // 插入异常日志 232 // 插入异常日志
232 this.createExceptionLog(req, ex); 233 this.createExceptionLog(req, ex);