Dubbo服务暴露源码分析
2021-07-02 15:15:54 4 举报
此图是我对Dubbo源码的理解,供大家学习交流
作者其他创作
大纲/内容
暴露服务,通过protocol.export方法完成invoker到exporter的转换dubbo: DubboProtocol.export(invoker)rmi: RmiProtocol.export(invoker)injvm: InjvmProtocol.export(invoker)
AbstractRegistry
client.create().withMode(CreateMode.EPHEMERAL).forPath(path)
new HeaderExchangeServer(server)
createRegistry(url)
register(URL url)
HeaderExchangeServer
RegistryProtocol.OverrideListener
notify(url.getBackupUrls());
exporters.add(exporter)
doRegister(url)
先从缓存中取,没有创建
registedProviderUrl :dubbo://192.168.3.1:20881/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=16932&side=provider×tamp=1567304294714
loadRegistries(true)
注册中心异步(默认)保存文件C:\\Users\\chaos/.dubbo/dubbo-registry-demo-provider-127.0.0.1:2181.cache
AbstractRegistry
FailbackRegistry
ProtocolFilterWrapper
onApplicationEvent
invoker.getUrl().getServiceKey()
启动服务
synchronizeddoExport()
暴露服务方式:1.serviceBean初始化过程中2.事件通知机制(所有bean初始化完成后)关键: delay
createServer(url)
远程暴露
JavassistProxyFactory
ProxyFactory$Adaptive
Transporter
返回ZookeeperRegistry
listener.notify(categoryList)
protocol.export(invoker)
doExportUrls()
调父类构造器
Protocol$Adaptive
ServiceConfig
final Registry registry = getRegistry(originInvoker)
overrideSubscribeUrl :provider://192.168.3.1:20881/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=16932&side=provider×tamp=1567304294714
子节点变化执行
监听zk的providers,configurators节点变化,通过notify更新url列表合并url: 读取configurators 节点下的数据,两种方案OverrideConfigurator : 覆盖添加 AbsentConfigurator :当配置不存在的时候添加 更新exporter
ZookeeperClient
恢复,父类实现
recover()
RegistryProtocol
Protocol
ZookeeperRegistry
openServer(url)
会在com.alibaba.dubbo.demo.DemoService/providers 节点下创建子节点toUrlPath(url) :/dubbo/com.alibaba.dubbo.demo.DemoService/providers/dubbo%3A%2F%2F192.168.3.1%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D20536%26side%3Dprovider%26timestamp%3D1567607129193创建持久节点: /dubbo创建持久节点: /dubbo/com.alibaba.dubbo.demo.DemoService创建持久节点:/dubbo/com.alibaba.dubbo.demo.DemoService/providers创建临时节点: toUrlPath(url)
注册中心协议
createEphemeral(path)
ZookeeperRegistryFactory
DubboNamespaceHandler
doLocalExport(originInvoker)
内部逻辑
getSubscribedOverrideUrl(registedProviderUrl)
获取注册中心 URL
暴露服务到远程
监听zk的节点变化,通过notify更新url列表此处不做处理: getSubscribed().entrySet() 为空
RegistryFactory
创建节点
线程安全
export(invoker)
获得Invoker
serviceKey(url)
CuratorZookeeperClient
ProtocolListenerWrapper
包装业务实现类
URL registryUrl = getRegistryUrl(originInvoker);
provider.xml
path: /dubbo/com.alibaba.dubbo.demo.DemoService/configurators创建持久节点: path
retryPeriod : 重试周期 5秒retry(); 重试 , 重新注册和订阅failedRegistered: 缓存了注册失败的url
Spring IOC
模板方法,子类实现
订阅overrideSubscribeUrl
zkClient.addStateListener
RegistryFactory$Adaptive
scope = none,不暴露服务scope != remote,暴露到本地scope != local,暴露到远程默认:服务先暴露到本地,再暴露到远程
registryUrl:zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.3.1%3A20881%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26bind.ip%3D192.168.3.1%26bind.port%3D20881%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D16932%26qos.port%3D22222%26side%3Dprovider%26timestamp%3D1567304294714&pid=16932&qos.port=22222×tamp=1567304294686
AbstractZookeeperClient
DubboExporter
getRegistry(URL url)
组装的url:dubbo://192.168.3.1:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.3.1&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=21676&qos.port=22222&side=provider×tamp=1567515993022本地的url: injvm://127.0.0.1/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.3.1&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=15756&qos.port=22222&side=provider×tamp=1567517466326registryURL:registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.3.1%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26bind.ip%3D192.168.3.1%26bind.port%3D20880%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D11924%26qos.port%3D22222%26side%3Dprovider%26timestamp%3D1567587629400&pid=11924&qos.port=22222®istry=zookeeper×tamp=1567587629383
protocol.export(invokerDelegete)
启动netty服务
从注册中心订阅overrideSubscribeUrl
解析注册中心配置,获取registryUrls
本地暴露
new DecodeHandler(handler)
client.create().forPath(path)
doRegister(URL url)
使用了SPI的Adaptive注解,利用javassit生成动态字节码通过代理工厂获取Invoker
getProviderUrl(originInvoker) :dubbo://192.168.3.1:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.3.1&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=19756&qos.port=22222&side=provider×tamp=1567595847730bounds : map( providerurl <--> exporter ) 避免服务重复暴露 ,先从缓存中去
retry()
定时器重试机制
Registry
createPersistent(path)
afterPropertiesSet
HeaderExchanger
创建ChildListener,监听子节点变化
AbstractRegistryFactory
ServiceBean
Wrapper.getWrapper(com.alibaba.dubbo.demo.provider.DemoServiceImpl)
获取订阅url
ProxyFactory
创建监听器
export()
解析并注册
StubProxyFactoryWrapper
通过 ProxyFactory 类的 getInvoker 方法使用 ref 生成一个 AbstractProxyInvoker 实例
new HeaderExchangeHandler(handler)
registryUrls是否有值
连接zookeeper,创建节点,注册服务
服务暴露逻辑执行,先从缓存bounds.get(key)中获取,没有就创建exporter
ExchangeServer
返回server
saveProperties(url)
scope
暴露服务
notify(categoryList)
设置url协议为injvm
暴露服务到本地
返回Invoker
FailbackRegistry
getRegistedProviderUrl(originInvoker)
loadProperties();
根据URL获取Registry实现类
InjvmProtocol
缓存exporter
ZookeeperRegistry
exportLocal(url)
url: provider://192.168.3.1:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=27480&side=provider×tamp=1567663400836urls.get(0) :empty://192.168.3.1:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=27480&side=provider×tamp=1567663400836
NettyServer.doOpen()
获取已注册的服务提供者url
0 条评论
下一页