From 3f23d5d8439a7f84102ab0e82a64eaecb805e915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=B6=9B?= Date: Sun, 4 Feb 2024 21:11:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sentinel=E9=99=90=E6=B5=81?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=BB=9F=E4=B8=80=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BC=98=E5=85=88=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PersistenceExceptionHandlerAdvice.java | 2 + .../config/OpensabreSentinelConfig.java | 10 ++++ .../SentinelExceptionHandlerAdvice.java | 59 +++++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 3 +- .../src/main/resources/opensabre-rpc.yml | 4 +- .../DefaultGlobalExceptionHandlerAdvice.java | 2 + 6 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/config/OpensabreSentinelConfig.java create mode 100644 opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/exception/SentinelExceptionHandlerAdvice.java diff --git a/opensabre-starter-persistence/src/main/java/io/github/opensabre/persistence/exception/PersistenceExceptionHandlerAdvice.java b/opensabre-starter-persistence/src/main/java/io/github/opensabre/persistence/exception/PersistenceExceptionHandlerAdvice.java index 89052c0..eab55e0 100644 --- a/opensabre-starter-persistence/src/main/java/io/github/opensabre/persistence/exception/PersistenceExceptionHandlerAdvice.java +++ b/opensabre-starter-persistence/src/main/java/io/github/opensabre/persistence/exception/PersistenceExceptionHandlerAdvice.java @@ -3,11 +3,13 @@ import io.github.opensabre.common.core.entity.vo.Result; import io.github.opensabre.common.core.exception.SystemErrorType; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; import org.springframework.dao.DuplicateKeyException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @Slf4j +@Order(100) @RestControllerAdvice public class PersistenceExceptionHandlerAdvice { diff --git a/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/config/OpensabreSentinelConfig.java b/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/config/OpensabreSentinelConfig.java new file mode 100644 index 0000000..4a4cc3a --- /dev/null +++ b/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/config/OpensabreSentinelConfig.java @@ -0,0 +1,10 @@ +package io.github.opensabre.rpc.sentinel.config; + +import io.github.opensabre.rpc.sentinel.exception.SentinelExceptionHandlerAdvice; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.annotation.Import; + +@AutoConfiguration +@Import({SentinelExceptionHandlerAdvice.class}) +public class OpensabreSentinelConfig { +} \ No newline at end of file diff --git a/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/exception/SentinelExceptionHandlerAdvice.java b/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/exception/SentinelExceptionHandlerAdvice.java new file mode 100644 index 0000000..05fc958 --- /dev/null +++ b/opensabre-starter-rpc/src/main/java/io/github/opensabre/rpc/sentinel/exception/SentinelExceptionHandlerAdvice.java @@ -0,0 +1,59 @@ +package io.github.opensabre.rpc.sentinel.exception; + +import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; +import com.alibaba.csp.sentinel.slots.block.flow.FlowException; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; +import com.alibaba.csp.sentinel.slots.system.SystemBlockException; +import io.github.opensabre.common.core.entity.vo.Result; +import io.github.opensabre.common.core.exception.SystemErrorType; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * + */ +@Slf4j +@RestControllerAdvice +@Order(100) +public class SentinelExceptionHandlerAdvice { + + @ExceptionHandler(BlockException.class) + public Result blockException(BlockException e) { + log.error("block exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } + + @ExceptionHandler(FlowException.class) + public Result flowException(FlowException e) { + log.error("flow exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } + + @ExceptionHandler(DegradeException.class) + public Result degradeException(DegradeException e) { + log.error("degrade exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } + + @ExceptionHandler(ParamFlowException.class) + public Result paramFlowException(ParamFlowException e) { + log.error("param flow exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } + + @ExceptionHandler(SystemBlockException.class) + public Result systemBlockException(SystemBlockException e) { + log.error("system block exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } + + @ExceptionHandler(AuthorityException.class) + public Result authorityException(AuthorityException e) { + log.error("authority exception:{}", e.getMessage()); + return Result.fail(SystemErrorType.SYSTEM_BUSY); + } +} diff --git a/opensabre-starter-rpc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/opensabre-starter-rpc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index da64d14..04c4a8d 100644 --- a/opensabre-starter-rpc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/opensabre-starter-rpc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,3 @@ io.github.opensabre.rpc.openfeign.config.OpensabreFeignConfig -io.github.opensabre.rpc.openfeign.config.OpensabreLoadBalancerConfig \ No newline at end of file +io.github.opensabre.rpc.openfeign.config.OpensabreLoadBalancerConfig +io.github.opensabre.rpc.sentinel.config.OpensabreSentinelConfig \ No newline at end of file diff --git a/opensabre-starter-rpc/src/main/resources/opensabre-rpc.yml b/opensabre-starter-rpc/src/main/resources/opensabre-rpc.yml index 7217033..eb27ec0 100644 --- a/opensabre-starter-rpc/src/main/resources/opensabre-rpc.yml +++ b/opensabre-starter-rpc/src/main/resources/opensabre-rpc.yml @@ -1,4 +1,6 @@ feign: + circuitbreaker: + enabled: true sentinel: enabled: true httpclient: @@ -14,7 +16,7 @@ feign: default: connectTimeout: 5000 readTimeout: 10000 - loggerLevel: full + loggerLevel: FULL spring: cloud: sentinel: diff --git a/opensabre-web/src/main/java/io/github/opensabre/common/web/exception/DefaultGlobalExceptionHandlerAdvice.java b/opensabre-web/src/main/java/io/github/opensabre/common/web/exception/DefaultGlobalExceptionHandlerAdvice.java index c44dc11..9fd2093 100644 --- a/opensabre-web/src/main/java/io/github/opensabre/common/web/exception/DefaultGlobalExceptionHandlerAdvice.java +++ b/opensabre-web/src/main/java/io/github/opensabre/common/web/exception/DefaultGlobalExceptionHandlerAdvice.java @@ -4,6 +4,7 @@ import io.github.opensabre.common.core.exception.BaseException; import io.github.opensabre.common.core.exception.SystemErrorType; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.HttpRequestMethodNotSupportedException; @@ -18,6 +19,7 @@ * 默认全局异常处理类 */ @Slf4j +@Order @RestControllerAdvice public class DefaultGlobalExceptionHandlerAdvice {