|
|
|
@ -25,16 +25,20 @@ import feign.Contract;
|
|
|
|
|
import feign.Feign;
|
|
|
|
|
import feign.InvocationHandlerFactory;
|
|
|
|
|
import feign.Target;
|
|
|
|
|
//import feign.hystrix.FallbackFactory;
|
|
|
|
|
//import feign.hystrix.HystrixFeign;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
|
|
|
|
import org.springframework.cloud.openfeign.FeignClientFactoryBean;
|
|
|
|
|
import org.springframework.cloud.openfeign.FeignContext;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.context.support.GenericApplicationContext;
|
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import static org.springframework.beans.factory.BeanFactory.FACTORY_BEAN_PREFIX;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link Feign.Builder} like {@link HystrixFeign.Builder}.
|
|
|
|
|
*
|
|
|
|
@ -77,38 +81,43 @@ public final class SentinelFeign {
|
|
|
|
|
@Override
|
|
|
|
|
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 = Builder.this.applicationContext
|
|
|
|
|
// .getBean("&" + target.type().getName());
|
|
|
|
|
//
|
|
|
|
|
// Class fallback = (Class) getFieldValue(feignClientFactoryBean,
|
|
|
|
|
// "fallback");
|
|
|
|
|
// Class fallbackFactory = (Class) getFieldValue(feignClientFactoryBean,
|
|
|
|
|
// "fallbackFactory");
|
|
|
|
|
// String beanName = (String) getFieldValue(feignClientFactoryBean,
|
|
|
|
|
// "contextId");
|
|
|
|
|
// if (!StringUtils.hasText(beanName)) {
|
|
|
|
|
// beanName = (String) getFieldValue(feignClientFactoryBean, "name");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// Object fallbackInstance;
|
|
|
|
|
// FallbackFactory fallbackFactoryInstance;
|
|
|
|
|
// // check fallback and fallbackFactory properties
|
|
|
|
|
// if (void.class != fallback) {
|
|
|
|
|
// fallbackInstance = getFromContext(beanName, "fallback", fallback,
|
|
|
|
|
// target.type());
|
|
|
|
|
// return new SentinelInvocationHandler(target, dispatch,
|
|
|
|
|
// new FallbackFactory.Default(fallbackInstance));
|
|
|
|
|
// }
|
|
|
|
|
// if (void.class != fallbackFactory) {
|
|
|
|
|
// fallbackFactoryInstance = (FallbackFactory) getFromContext(
|
|
|
|
|
// beanName, "fallbackFactory", fallbackFactory,
|
|
|
|
|
// FallbackFactory.class);
|
|
|
|
|
// return new SentinelInvocationHandler(target, dispatch,
|
|
|
|
|
// fallbackFactoryInstance);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
GenericApplicationContext gctx = (GenericApplicationContext) Builder.this.applicationContext;
|
|
|
|
|
BeanDefinition def = gctx.getBeanDefinition(target.type().getName());
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO
|
|
|
|
|
* 由于初始化顺序发生变更,这里为了避免循环依赖,只能通过 BeanDefinition 的方式获得 FeignClientFactoryBean
|
|
|
|
|
* 需要重点review
|
|
|
|
|
*/
|
|
|
|
|
// FeignClientFactoryBean feignClientFactoryBean = (FeignClientFactoryBean) Builder.this.applicationContext
|
|
|
|
|
// .getBean(FACTORY_BEAN_PREFIX + target.type().getName());
|
|
|
|
|
FeignClientFactoryBean feignClientFactoryBean = (FeignClientFactoryBean) def.getAttribute("feignClientsRegistrarFactoryBean");
|
|
|
|
|
|
|
|
|
|
Class fallback = feignClientFactoryBean.getFallback();
|
|
|
|
|
Class fallbackFactory = feignClientFactoryBean.getFallbackFactory();
|
|
|
|
|
String beanName = feignClientFactoryBean.getContextId();
|
|
|
|
|
|
|
|
|
|
if (!StringUtils.hasText(beanName)) {
|
|
|
|
|
beanName = feignClientFactoryBean.getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object fallbackInstance;
|
|
|
|
|
FallbackFactory fallbackFactoryInstance;
|
|
|
|
|
// check fallback and fallbackFactory properties
|
|
|
|
|
if (void.class != fallback) {
|
|
|
|
|
fallbackInstance = getFromContext(beanName, "fallback", fallback,
|
|
|
|
|
target.type());
|
|
|
|
|
return new SentinelInvocationHandler(target, dispatch,
|
|
|
|
|
new FallbackFactory.Default(fallbackInstance));
|
|
|
|
|
}
|
|
|
|
|
if (void.class != fallbackFactory) {
|
|
|
|
|
fallbackFactoryInstance = (FallbackFactory) getFromContext(
|
|
|
|
|
beanName, "fallbackFactory", fallbackFactory,
|
|
|
|
|
FallbackFactory.class);
|
|
|
|
|
return new SentinelInvocationHandler(target, dispatch,
|
|
|
|
|
fallbackFactoryInstance);
|
|
|
|
|
}
|
|
|
|
|
return new SentinelInvocationHandler(target, dispatch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|