From 091bde7b65f5fbd6ef1a964f396884c4c2d55541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Tue, 22 Jan 2019 14:49:24 +0800 Subject: [PATCH] =?UTF-8?q?:pushpin:=20add=20autoconfigure=20:bug:=20Fixin?= =?UTF-8?q?g=20a=20bug.=20=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E4=B8=8D=E6=98=AFR=E7=9A=84=E5=BC=BA=E8=BD=AC=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feign/PigxFeginFallbackFactory.java | 14 +------ .../feign/PigxFeignFallbackMethod.java | 38 +++++++++++++++++++ .../feign/PigxHystrixInvocationHandler.java | 7 ++-- pom.xml | 5 +++ 4 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignFallbackMethod.java diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeginFallbackFactory.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeginFallbackFactory.java index c8262baeb..97777a01b 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeginFallbackFactory.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeginFallbackFactory.java @@ -20,13 +20,10 @@ package com.pig4cloud.pigx.common.security.feign; -import com.pig4cloud.pigx.common.core.constant.CommonConstants; -import com.pig4cloud.pigx.common.core.util.R; import feign.hystrix.FallbackFactory; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cglib.proxy.Enhancer; -import org.springframework.cglib.proxy.MethodInterceptor; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -45,19 +42,10 @@ public final class PigxFeginFallbackFactory implements FallbackFactory { @SuppressWarnings("unchecked") public T create(final Class type, final Throwable cause) { - // 重写 fegin ErrorDecoder,message 知己为 body 数据,反序列化为 Result - final R result = cause instanceof PigxFeginException ? - ((PigxFeginException) cause).getResult() : R.builder() - .code(CommonConstants.FAIL) - .msg(cause.getMessage()).build(); return (T) FALLBACK_MAP.computeIfAbsent(type, key -> { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(key); - enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { - log.error("Fallback class:[{}] method:[{}] message:[{}]", - type.getName(), method.getName(), cause.getMessage()); - return result; - }); + enhancer.setCallback(new PigxFeignFallbackMethod(type, cause)); return enhancer.create(); }); } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignFallbackMethod.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignFallbackMethod.java new file mode 100644 index 000000000..3e6542b58 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignFallbackMethod.java @@ -0,0 +1,38 @@ +package com.pig4cloud.pigx.common.security.feign; + +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.util.R; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; +import org.springframework.lang.Nullable; + +import java.lang.reflect.Method; + +/** + * @author lengleng + * @date 2019-01-22 + */ +@Slf4j +@AllArgsConstructor +public class PigxFeignFallbackMethod implements MethodInterceptor { + private Class type; + private Throwable cause; + + @Nullable + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) { + log.error("Fallback class:[{}] method:[{}] message:[{}]", + type.getName(), method.getName(), cause.getMessage()); + + if (R.class == method.getReturnType()) { + final R result = cause instanceof PigxFeginException ? + ((PigxFeginException) cause).getResult() : R.builder() + .code(CommonConstants.FAIL) + .msg(cause.getMessage()).build(); + return result; + } + return null; + } +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxHystrixInvocationHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxHystrixInvocationHandler.java index 2225ad6d6..eb1896090 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxHystrixInvocationHandler.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxHystrixInvocationHandler.java @@ -134,13 +134,12 @@ public final class PigxHystrixInvocationHandler implements InvocationHandler { @Nullable @SuppressWarnings("unchecked") protected Object getFallback() { - final FallbackFactory localFallbackFactory = fallbackFactory == null ? PigxFeginFallbackFactory.INSTANCE : fallbackFactory; Object fallback; try { - if (localFallbackFactory instanceof PigxFeginFallbackFactory) { - fallback = ((PigxFeginFallbackFactory) localFallbackFactory).create(target.type(), getExecutionException()); + if (fallbackFactory == null) { + fallback = PigxFeginFallbackFactory.INSTANCE.create(target.type(), getExecutionException()); } else { - fallback = localFallbackFactory.create(getExecutionException()); + fallback = fallbackFactory.create(getExecutionException()); } Object result = fallbackMethodMap.get(method).invoke(fallback, args); if (isReturnsHystrixCommand(method)) { diff --git a/pom.xml b/pom.xml index 120505487..b700d17eb 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,11 @@ spring-boot-configuration-processor true + + org.springframework.boot + spring-boot-autoconfigure-processor + true + com.github.ulisesbocchio