Преглед изворни кода

:pushpin: add autoconfigure
:bug: Fixing a bug. 接口返回值不是R的强转错误

冷冷 пре 6 година
родитељ
комит
091bde7b65

+ 1 - 13
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<T> implements FallbackFactory<T> {
 
 	@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();
 		});
 	}

+ 38 - 0
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;
+	}
+}

+ 3 - 4
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)) {

+ 5 - 0
pom.xml

@@ -80,6 +80,11 @@
 			<artifactId>spring-boot-configuration-processor</artifactId>
 			<optional>true</optional>
 		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-autoconfigure-processor</artifactId>
+			<optional>true</optional>
+		</dependency>
 		<!--jasypt配置文件加解密-->
 		<dependency>
 			<groupId>com.github.ulisesbocchio</groupId>