客户端服务注册发布主要做了以下几件事
1.读取配置文件,封装map
2.读取zk中providers,生成netty连接,放入urlInvokerMap,将本身consumer注册到zk
3.根据配置文件ref,生成proxy客户端代理
4.通过proxy代理调用urlInvokerMap中的invoker里面的client通过netty连接发送请求,取得返回结果
下面是整个代码调用流程
ReferenceBean.afterPropertiesSet()-->getObject()-->get()
ReferenceConfig.get()-->init()-->createProxy()
init()拼装xml对应参数map
createProxy()
invoker=RegistryProtocol.refer()=MockClusterInvoker
proxyFactory.getProxy(invoker)=JavassistProxyFactory.getProxy(invoker)=proxy0
proxy0是服务代理(客户端调用的实现类)
method sayHello
handle.invoke()
handel = InvokerInvocationHandler(invoker)
RegistryProtocol.refer()
doRefer()
directory.subscribe()
RegistryDirectory.subscribe()
cluster=MockClusterWrapper()
cluster.join(directory)==MockClusterInvoker()
RegistryDirectory.subscribe()
registry === zookeeperRegistry RegistryProtocol中set的值
registry.subscribe(url, this);//consumer:// this:RegistryDirectory
RegistryDirectory.subscribe()-->FailbackRegistry.subscribe()-->ZookeeperRegistry.doSubscribe()-->notify()-->doNotify()-->AbstractRegistry.notify()-->listener.notify(categoryList);
listener = RegistryDirectory
RegistryDirectory.notify()-->refreshInvoker()-->toInvokers(invokerUrls)
invoker = new InvokerDelegete<T>(protocol.refer(serviceType, url), url, providerUrl);
protocol=DubboProtocol
protocol.refer(serviceType, url)
DubboProtocol.refer()
DubboInvoker<T> invoker = new DubboInvoker<T>(serviceType, url, getClients(url), invokers);
getClients(url)//生成netty客户端连接
initClient()
client = Exchangers.connect(url ,requestHandler);//心跳机制、生成netty连接
客户端调用过程
interface.method()//接口.调用方法名
proxy0.method()
handle.invoke()
InvokerInvocationHandler.invoke()
MockClusterInvoker.invoke()
AbstractClusterInvoker().invoke()
FailOverClusterInvoker.doInvoke()
AbstractInvoker.invoke()
DubboInvoker.doInvoke()
......
NettyChnnel.send()