Dubbo服务的引用及其调用过程源码分析
2021-07-02 15:14:28 3 举报
我是董成林(Fox),源码爱好者,此图是我对Dubbo源码的理解,供大家学习交流
作者其他创作
大纲/内容
获取invoker
loadProperties();
Registry
recover()
FailoverClusterInvoker
解析并注册
AbstractClusterInvoker
invoke(Invocation)
MockClusterInvoker
getRegistry(URL url)
ZookeeperRegistry
LoadBalance
子节点变化后调用
init()
订阅服务
InvokerInvocationHandler
createProxy(map)
Protocol$Adaptive
Cluster
doRegister(url)
缓存invoker到consumerInvokers
registryFactory.getRegistry(url)
consumer.xml
createPersistent(path)
返回ZookeeperReigstry
创建ChildListener,监听子节点变化
会在com.alibaba.dubbo.demo.DemoService/comsumers 节点下创建子节点toUrlPath(url) :/dubbo/com.alibaba.dubbo.demo.DemoService/consumers/consumer%3A%2F%2F192.168.3.1%2Fcom.alibaba.dubbo.demo.DemoService%3Fapplication%3Ddemo-consumer%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.0.0%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D29568%26qos.port%3D33333%26side%3Dconsumer%26timestamp%3D1567756038069创建持久节点: /dubbo创建持久节点: /dubbo/com.alibaba.dubbo.demo.DemoService创建持久节点:/dubbo/com.alibaba.dubbo.demo.DemoService/comsumers创建临时节点: toUrlPath(url)
引用服务:通过implements FactoryBean在getObject()中获取代理对象
overrideDirectoryUrl :zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&mock=com.alibaba.dubbo.demo.consumer.DemoServiceMock&pid=26800&qos.port=33333®ister.ip=192.168.3.1&remote.timestamp=1567776260905&side=consumer×tamp=1567776471070
ZookeeperRegistryFactory
Protocol
注册subscribeUrl到注册中心consumers节点下
retry()
JavassistProxyFactory
获取registry
ProtocolListenerWrapper
notify(url.getBackupUrls());
调父类构造器
notify(categoryList)
listener.notify(categoryList)
retryPeriod : 重试周期 5秒retry(); 重试 , 重新注册和订阅failedRegistered: 缓存了注册失败的url
toMethodInvokers(newUrlInvokerMap)
恢复,父类实现
ConditionRouter
调用失败,执行mock逻辑
saveProperties(url)
InjvmProtocol
更新invoker列表
DubboProtocol
doRegister(URL url)
获取调用结果
injvm本地引用
RegistryFactory
directory.subscribe(subscribeUrl)
LeastActiveLoadBalance
ZookeeperRegistry
createEphemeral(path)
返回调用结果,exception或result
ZookeeperClient
AbstractZookeeperClient
client.create().withMode(CreateMode.EPHEMERAL).forPath(path)
连接zk,创建节点
DubboInvoker#doInvoke(Invocation)
返回invoker
根据负载均衡策略获取invoker
注册中心协议
proxyFactory.getProxy(invoker)
getObject()
dubbo远程引用
ProtocolFilterWrapper
如果invoker在selected中,或者invoker不可用&& availablecheck为真,则重新选择
client.create().forPath(path)
buildInvokerChain 调用3个默认过滤器ConsumerContextFilterFutureFilterMonitorFilter
zkClient.addStateListener
AbstractRegistryFactory
get()
缓存invoker到consumerInvokersProviderConsumerRegTable: 服务提供者消费者注册表缓存了providerInvokers和consumerInvokers
返回代理对象
返回结果result
refreshInvoker(invokerUrls)
先从缓存中取,没有创建
invokers.size()==1 ,直接返回invokers.size()>1 ,调用对应的负载均衡算法dubbo负载均衡算法1.RandomLoadBalance: 权重随机算法2.RoundRobinLoadBalance: 加权轮询算法3.LeastActiveLoadBalance: 最少活跃调用数算法3.ConsistentHashLoadBalance: 一致性hash
recreate()
javassist生成代理对象
register(URL url)
AbstractRegistry
ReferenceBean
注册中心异步(默认)保存文件C:\\Users\\chaos/.dubbo/dubbo-registry-demo-provider-127.0.0.1:2181.cache
调用
引用服务,返回代理对象
通过代理对象调用业务逻辑
new FailoverClusterInvoker<T>(directory)
ReferenceConfig
创建节点
FailbackRegistry
RegistryDirectory
根据loadBalance调用
subscribeUrl: consumer://192.168.3.1/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=29568&qos.port=33333&side=consumer×tamp=15677560380691. 注册subscribeUrl到 zookeeper的 consumers节点2. 订阅服务
proxy0.sayHello(\"fox\")
result = this.invoker.invoke(invocation)
FailbackRegistry
registry.register(subscribeUrl)
创建invoker对象
CuratorZookeeperClient
createRegistry(url)
定时器重试机制
RandomLoadBalance
Invoker invoker = cluster.join(directory)
监听zk的节点变化,通过notify更新url列表此处不做处理: getSubscribed().entrySet() 为空
join(Directory<T> directory)
invoker类型为 MockClusterInvoker 内部封装了服务降级逻辑
AbstractLoadBalance
RegistryFactory$Adaptive
条件路由
DubboNamespaceHandler
AbstractRegistry
url: zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-consumer&dubbo=2.0.0&pid=24988&qos.port=33333&refer=application%3Ddemo-consumer%26check%3Dfalse%26dubbo%3D2.0.0%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D24988%26qos.port%3D33333%26register.ip%3D192.168.3.1%26side%3Dconsumer%26timestamp%3D1567754599462×tamp=1567754599603
获取invoker,返回代理对象
Spring IOC
ConsistentHashLoadBalance
RoundRobinLoadBalance
路由过滤invoker列表
解析配置,创建代理对象
Result result = invoker.invoke(invocation)
0 条评论
下一页