diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfiguration.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java similarity index 57% rename from pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfiguration.java rename to pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java index 42866075a..5913648e5 100755 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfiguration.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java @@ -21,12 +21,14 @@ import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser; import com.pig4cloud.pigx.common.sentinel.feign.PigxSentinelFeign; +import com.pig4cloud.pigx.common.sentinel.handle.GlobalBizExceptionHandler; import com.pig4cloud.pigx.common.sentinel.handle.PigxUrlBlockHandler; import com.pig4cloud.pigx.common.sentinel.parser.PigxHeaderRequestOriginParser; import feign.Feign; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -39,26 +41,45 @@ import org.springframework.context.annotation.Scope; */ @Configuration(proxyBeanMethods = false) @AutoConfigureBefore(SentinelFeignAutoConfiguration.class) -public class SentinelAutoConfiguration { +public class PigxSentinelAutoConfiguration { - @Bean - @Scope("prototype") - @ConditionalOnMissingBean - @ConditionalOnProperty(name = "feign.sentinel.enabled") - public Feign.Builder feignSentinelBuilder() { - return PigxSentinelFeign.builder(); - } + @Bean + @Scope("prototype") + @ConditionalOnMissingBean + @ConditionalOnProperty(name = "spring.cloud.openfeign.sentinel.enabled",matchIfMissing = true) + public Feign.Builder feignSentinelBuilder() { + return PigxSentinelFeign.builder(); + } - @Bean - @ConditionalOnMissingBean - public BlockExceptionHandler blockExceptionHandler() { - return new PigxUrlBlockHandler(); - } + @Bean + @ConditionalOnMissingBean + /** + * 创建一个块异常处理器实例 + * 如果没有定义块异常处理器,则创建一个PigxUrlBlockHandler实例 + */ + public BlockExceptionHandler blockExceptionHandler() { + return new PigxUrlBlockHandler(); + } + + @Bean + @ConditionalOnMissingBean + /** + * 创建一个请求源解析器实例 + * 如果没有定义请求源解析器,则创建一个PigxHeaderRequestOriginParser实例 + */ + public RequestOriginParser requestOriginParser() { + return new PigxHeaderRequestOriginParser(); + } + + /** + * 创建一个全局业务异常处理器实例 + * 用于处理全局的业务异常 + */ + @Bean + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) + public GlobalBizExceptionHandler globalBizExceptionHandler() { + return new GlobalBizExceptionHandler(); + } - @Bean - @ConditionalOnMissingBean - public RequestOriginParser requestOriginParser() { - return new PigxHeaderRequestOriginParser(); - } } diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/feign/PigxSentinelInvocationHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/feign/PigxSentinelInvocationHandler.java index bbebf2727..ae65adf8f 100644 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/feign/PigxSentinelInvocationHandler.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/feign/PigxSentinelInvocationHandler.java @@ -8,6 +8,7 @@ import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.Tracer; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.pig4cloud.pigx.common.core.util.R; import feign.Feign; import feign.InvocationHandlerFactory; import feign.MethodMetadata; @@ -118,6 +119,10 @@ public class PigxSentinelInvocationHandler implements InvocationHandler { throw new AssertionError(e.getCause()); } } + else if (R.class == method.getReturnType()) { + log.error("feign 服务间调用异常", ex); + return R.failed(ex.getLocalizedMessage()); + } else { throw ex; } diff --git a/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 0585eb055..db64b820e 100644 --- a/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,2 @@ -com.pig4cloud.pigx.common.sentinel.SentinelAutoConfiguration +com.pig4cloud.pigx.common.sentinel.PigxSentinelAutoConfiguration com.pig4cloud.pigx.common.sentinel.handle.GlobalBizExceptionHandler