mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-10 21:19:00 +08:00
📌 add autoconfigure
🐛 Fixing a bug. 接口返回值不是R的强转错误
This commit is contained in:
1 parent
02fa2dcd73
commit
091bde7b65
4 files changed
+47
-17
No files matched your search
+1
-13
@@ -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,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
@@ -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)) {
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user