springbootopenfeign客户端调⽤过程
读这篇⽂章之前请先看下这⼏篇⽂章
spring boot consul 客户端加载过程
spring boot Open Feign 客户端加载过程
springboot推荐算法
读完上⾯的⽂章后我们知道
1,Cosnul客户端加载过程⽐OpenFeign晚,所以你想在OpenFeign加载完就发起请求是不可能的
2,OpenFeign依赖Consul,和 Load Balancer
3,OpenFeign真正发起请求是通过Java动态代理完成的
先回忆⼀下
OpenFeign动态代理⽣成对象的代码的ReflectiveFeign
这⾥⽤到了⼀个InvocationHandler 就是它⾃⼰的内部类
FeignInvocationHandler
public class ReflectiveFeign extends Feign {
private final SynchronousMethodHandler.Factory factory;
@Override
public <T> T newInstance(Target<T> target) {
Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
Map<Method, MethodHandler> methodToHandler = new LinkedHashMap<Method, MethodHandler>();
List<DefaultMethodHandler> defaultMethodHandlers = new LinkedList<DefaultMethodHandler>();
for (Method method : pe().getMethods()) {
if (DeclaringClass() == Object.class) {
continue;
} else if (Util.isDefault(method)) {
DefaultMethodHandler handler = new DefaultMethodHandler(method);
defaultMethodHandlers.add(handler);
methodToHandler.put(method, handler);
} else {
methodToHandler.put(method, (pe(), method)));
}
}
InvocationHandler handler = ate(target, methodToHandler);
T proxy = (T) pe().getClassLoader(),
new Class<?>[] {pe()}, handler);
for (DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) {
defaultMethodHandler.bindTo(proxy);
}
return proxy;
}
static class FeignInvocationHandler implements InvocationHandler {
private final Target target;
private final Map<Method, MethodHandler> dispatch;
FeignInvocationHandler(Target target, Map<Method, MethodHandler> dispatch) {
this.target = checkNotNull(target, "target");
this.dispatch = checkNotNull(dispatch, "dispatch for %s", target);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
。。。
(method).invoke(args);
}
}
static final class ParseHandlersByName {
private final Contract contract;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。