🔖 🔖 🔖 v3.0.4 spring cloud 2020.0.1 重构 archetype

This commit is contained in:
lbw 2021-01-30 23:26:04 +08:00
parent 51c19b49fe
commit bb6b4837e4

View File

@ -16,6 +16,11 @@
package com.pig4cloud.pig.common.feign.ext; package com.pig4cloud.pig.common.feign.ext;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.sentinel.feign.SentinelContractHolder; import com.alibaba.cloud.sentinel.feign.SentinelContractHolder;
import feign.Contract; import feign.Contract;
import feign.Feign; import feign.Feign;
@ -23,17 +28,14 @@ import feign.InvocationHandlerFactory;
import feign.Target; import feign.Target;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.FeignContext; import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
/** /**
* 支持自动降级注入 重写 {@link com.alibaba.cloud.sentinel.feign.SentinelFeign} * 支持自动降级注入 重写 {@link com.alibaba.cloud.sentinel.feign.SentinelFeign}
* *
@ -74,27 +76,25 @@ public final class PigSentinelFeign {
super.invocationHandlerFactory(new InvocationHandlerFactory() { super.invocationHandlerFactory(new InvocationHandlerFactory() {
@Override @Override
public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) { public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) {
// using reflect get fallback and fallbackFactory properties from
// FeignClientFactoryBean because FeignClientFactoryBean is a package
// level class, we can not use it in our package
Object feignClientFactoryBean = PigSentinelFeign.Builder.this.applicationContext
.getBean("&" + target.type().getName());
Class fallback = (Class) getFieldValue(feignClientFactoryBean, "fallback"); // 查找 FeignClient 上的 降级策略
Class fallbackFactory = (Class) getFieldValue(feignClientFactoryBean, "fallbackFactory"); FeignClient feignClient = AnnotationUtils.findAnnotation(target.type(), FeignClient.class);
String beanName = (String) getFieldValue(feignClientFactoryBean, "contextId"); Class fallback = feignClient.fallback();
Class fallbackFactory = feignClient.fallbackFactory();
String beanName = feignClient.contextId();
if (!StringUtils.hasText(beanName)) { if (!StringUtils.hasText(beanName)) {
beanName = (String) getFieldValue(feignClientFactoryBean, "name"); beanName = feignClient.name();
} }
Object fallbackInstance; Object fallbackInstance;
FallbackFactory fallbackFactoryInstance; FallbackFactory fallbackFactoryInstance;
// check fallback and fallbackFactory properties
if (void.class != fallback) { if (void.class != fallback) {
fallbackInstance = getFromContext(beanName, "fallback", fallback, target.type()); fallbackInstance = getFromContext(beanName, "fallback", fallback, target.type());
return new PigSentinelInvocationHandler(target, dispatch, return new PigSentinelInvocationHandler(target, dispatch,
new FallbackFactory.Default(fallbackInstance)); new FallbackFactory.Default(fallbackInstance));
} }
if (void.class != fallbackFactory) { if (void.class != fallbackFactory) {
fallbackFactoryInstance = (FallbackFactory) getFromContext(beanName, "fallbackFactory", fallbackFactoryInstance = (FallbackFactory) getFromContext(beanName, "fallbackFactory",
fallbackFactory, FallbackFactory.class); fallbackFactory, FallbackFactory.class);