From cf285407f52788a5f23b04fc73dc07aab251d5e7 Mon Sep 17 00:00:00 2001 From: lbw Date: Sat, 9 Jul 2022 09:23:26 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20fe?= =?UTF-8?q?ign=20=E5=BC=82=E5=B8=B8=E7=9B=B4=E6=8E=A5=E6=8A=9B=E5=87=BA=20?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=20=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feign/PigxSentinelInvocationHandler.java | 10 +-------- .../handle/GlobalBizExceptionHandler.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) 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 e4c67e29b..bbebf2727 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,7 +8,6 @@ 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; @@ -120,14 +119,7 @@ public class PigxSentinelInvocationHandler implements InvocationHandler { } } else { - // 若是R类型 执行自动降级返回R - if (R.class == method.getReturnType()) { - log.error("feign 服务间调用异常", ex); - return R.failed(ex.getLocalizedMessage()); - } - else { - throw ex; - } + throw ex; } } finally { diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java index ac744251c..f2112b13b 100644 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java @@ -18,7 +18,10 @@ package com.pig4cloud.pigx.common.sentinel.handle; import com.alibaba.csp.sentinel.Tracer; +import com.fasterxml.jackson.databind.ObjectMapper; import com.pig4cloud.pigx.common.core.util.R; +import feign.FeignException; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.http.HttpStatus; @@ -43,6 +46,8 @@ import java.util.List; @ConditionalOnExpression("!'${security.oauth2.client.clientId}'.isEmpty()") public class GlobalBizExceptionHandler { + private final ObjectMapper objectMapper = new ObjectMapper(); + /** * 全局异常. * @param e the e @@ -58,6 +63,22 @@ public class GlobalBizExceptionHandler { return R.failed(e.getLocalizedMessage()); } + @SneakyThrows + @ExceptionHandler(FeignException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public R handleGlobalException(FeignException e) { + log.error("全局异常信息 ex={}", e.getMessage(), e); + + // 业务异常交由 sentinel 记录 + Tracer.trace(e); + + if (e.responseBody().isPresent()) { + return objectMapper.readValue(e.responseBody().get().array(), R.class); + } + + return R.failed(e.getLocalizedMessage()); + } + /** * AccessDeniedException * @param e the e