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