Feign源码

1. 当有@EnableFeignClients开启Feign

2. 扫描主类下的FeignClient.class注解的类

basePackages = getBasePackages(metadata);

3. 注册FeignClient

registerFeignClient(registry, annotationMetadata, attributes);

4. 创建代理对象

 ReflectiveFeign -> newInstance

T proxy = Proxy.newProxyInstance(target.type().getClassLoader(), new Class[]{target.type()}, handler);
static class FeignInvocationHandler implements InvocationHandler {
        private final Target target;
        private final Map<Method, MethodHandler> dispatch;

        FeignInvocationHandler(Target target, Map<Method, MethodHandler> dispatch) {
            this.target = (Target)Util.checkNotNull(target, "target", new Object[0]);
            this.dispatch = (Map)Util.checkNotNull(dispatch, "dispatch for %s", new Object[]{target});
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        }
}


this.dispatch.get(method)).invoke(args);
 

5. 调用invoke方法

通过ribbon讲服务名称转为IP:端口

并通过 HttpURLConnection 发出请求

 

posted @ 2022-03-23 13:07  yifanSJ  阅读(48)  评论(0编辑  收藏  举报