dubbo3.0导出源码详尽
2023-11-14 16:16:47 0 举报
dubbo3.0导出源码详尽
作者其他创作
大纲/内容
消费端
初始化ServiceConfigurationListener
获取Nacos配置中心的本地缓存数据
registerServiceInstance();
2
//创建接口级别动态配置的监听器ServiceConfigurationListenerServiceConfigurationListener font color=\"#4ccbcd\
通知订阅者,根据key获取CacheData
doExportUrls();
metadataInfo的值为
tri://ip:port/接口名?k1=v1..........
//overrideSubscribeUrl相对于providerUrl,就是增加了category=configurators&check=false这两个参数,然后协议由dubbo改成了provider final URL overrideSubscribeUrl = getSubscribedOverrideUrl(providerUrl);
//根据不同的导出类型,获取不同的Registry对象,但是返回都是统一的包装类ListenerRegistryWrapper//它里面有个属性org.apache.dubbo.registry.ListenerRegistryWrapper.registry//对于应用级别注册,它的值是ServiceDiscoveryRegistry对象//对于接口级别的注册,它的值是ZookeeperRegistry对象
org.apache.dubbo.configcenter.support.nacos.NacosDynamicConfiguration.NacosConfigListener#listeners------->org.apache.dubbo.registry.integration.RegistryProtocol.ServiceConfigurationListener#notifyListener------->org.apache.dubbo.registry.integration.RegistryProtocol.OverrideListener
//DynamicConfiguration.getRuleKey(providerUrl)的值为\"org.apache.dubbo.springboot.demo.DemoService::\"//CONFIGURATORS_SUFFIX的值为\".configurators\"this.initWith(DynamicConfiguration.getRuleKey(providerUrl) + CONFIGURATORS_SUFFIX);
接口级别的注册2.7
org.apache.dubbo.configcenter.support.nacos.NacosDynamicConfiguration#addListener
doExportUrl(font color=\"#e855a4\
DubboExporter的构造方法,会把自己放入到exporterMap中
org.apache.dubbo.registry.client.metadata.MetadataUtils#getRemoteMetadata
开始遍历不同的注册中心,即registryURLs,往每个注册中心中暴露服务
应用级别的注册3.0
//如果b style=\
//在这里,往zk中注册/dubbo/mapping/org.apache.dubbo.springboot.demo.DemoService节点exported();
1
String font color=\"#e855a4\
1.2.从动态配置中心尝试根据key去配置中心获取动态配置
//从Spring的容器中获取应用级别的监听器,如果没有则创建应用级别的动态配置的监听器return moduleModel.getBeanFactory().getOrRegisterBean(font color=\"#4669ea\
url = exportRemote(font color=\"#e855a4\
urls = ConfigParser.parseConfigurators(rawConfig);
//在这个里面构造服务提供者的拦截器链org.apache.dubbo.rpc.cluster.filter.ProtocolFilterWrapper#export
final ExporterChangeableWrapper<T> exporter = doLocalExport(font color=\"#4ccbcd\
doExportUrlsFor1Protocol(font color=\"#569230\
//放入到com.alibaba.nacos.client.config.impl.CacheData#listenerslisteners.addIfAbsent(wrap)
以上是导出某一个服务,导出所有服务后,才会进行应用级注册
注册所有接口级别的元数据信息,也就是把metadataInfo对象注册出去
return new DestroyableExporter<>(exporter);
configVersion: v3.0configs:- addresses: - 192.168.3.28:20881 enabled: false parameters: timeout: 66669 side: consumerenabled: truekey: org.apache.dubbo.springboot.demo.DemoServicescope: service
exportLocal(url);
rawConfig的值为
urls = ConfigParser.parseConfigurators(rawConfig);
return exporter;
providerUrl = overrideUrlWithConfig(font color=\"#e855a4\" style=\"\
应用级别注册
org.apache.dubbo.registry.integration.RegistryProtocol#export
remote---注册到Nacos
根据serviceInstance中的dubbo.metadata-service.url-params生成MetadataService服务的客户端
genConfiguratorsFromRawRule(rawConfig);
NacosConfigListener nacosConfigListener = font color=\"#e74f4c\
DubboAdmin
{ \"serviceName\": \"dubbo-springboot-demo-provider\
//这个对象就是用来进行本地元数据导出 MetadataServiceDelegation metadataService = applicationModel.getBeanFactory().getOrRegisterBean(MetadataServiceDelegation.class);
用法
修改配置,然后发送到Nacos配置中心
2.根据接口级别的动态配置,重写providerUrl
InjvmExporter的构造方法,会把自己放入到exporterMap中
//ref是org.apache.dubbo.springboot.demo.provider.DemoServiceImpl对象//interfaceClass是org.apache.dubbo.springboot.demo.DemoService//url是注册中心url//生成一个ref的代理对象,其实没具体做啥Invoker<?> font color=\"#314aa4\
将接口级别的服务信息,也就是,dubbo://ip:port/接口名......注册到注册中心
//根据接口级别的动态配置,重写providerUrl,并返回providerUrlreturn serviceConfigurationListener.overrideUrl(providerUrl);
dubbo://ip:port/接口名?k1=v1..........
local
//dataId的值为\"org.apache.dubbo.springboot.demo.DemoService::.configurators\"//group的值为\"dubbo\"//将dataId和group组成\"org.apache.dubbo.springboot.demo.DemoService::.configurators+dubbo\"放入com.alibaba.nacos.client.config.impl.ClientWorker#cacheMapCacheData font color=\"#e0c431\
register(font color=\"#e74f4c\
NacosConfigListener nacosConfigListener = font color=\"#e74f4c\
cache.addListener(listener);
org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol#export
如果metadata-type的值是local
//将urls序列化为Configurator对象,赋值给ProviderConfigurationListener的b style=\
1.先注册监听器,用于监听动态配置
调用到org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation#getMetadataInfo
//选取MetadataServiceDelegation导出所使用的协议和端口generateMetadataProtocol()
最终结构
将应用级的元数据注册到注册中心中去
//调用到org.apache.dubbo.config.metadata.ConfigurableMetadataServiceExporter.export方法 metadataServiceExporter.export();
最终的结构
invoker = new DelegateProviderMetaDataInvoker(font color=\"#314aa4\
URL registryUrl = getRegistryUrl(originInvoker);URL providerUrl = getProviderUrl(originInvoker);
metadataInfo对象里面记录,该应用的所有接口级服务的信息
3
//wrap是ManagerListenerWrap对象wrap = new ManagerListenerWrap(font color=\"#e855a4\
将metadataInfo发送到Nacos到配置中心中去
//应用级别到元数据对象this.serviceInstance = createServiceInstance(this.metadataInfo);
org.apache.dubbo.config.metadata.ExporterDeployListener#onModuleStarted
reportMetadata(this.metadataInfo);
这里就是接口级别的动态配置的逻辑
3.将rawConfig序列化为Configurator对象
org.apache.dubbo.config.ServiceConfig#exportdoExport()
1.3.将rawConfig序列化为Configurator对象
Nacos配置中心
metadataInfo = proxyHolder.getProxy().getMetadataInfo(ServiceInstanceMetadataUtils.getExportedServicesRevision(instance))
//构建一个监听器,用了监听该监听触发的时传入的url是否有和overrideSubscribeUrl匹配的,有则说明服务被动态改变了 final OverrideListener overrideSubscribeListener = new OverrideListener(font color=\"#e855a4\" style=\"\
org.apache.dubbo.config.ServiceConfig
注册中心暴露
//在这里导出接口级的服务的数据,判断是导出到注册中心还是到本地 exportMetadataService();
nacos:.........
初始化ProviderConfigurationListener
doRegister(this.serviceInstance);
//dataId的值为\"dubbo-springboot-demo-provider.configurators\"//group的值为\"dubbo\"//将dataId和group组成\"dubbo-springboot-demo-provider.configurators+dubbo\"放入com.alibaba.nacos.client.config.impl.ClientWorker#cacheMapCacheData font color=\"#e0c431\
//url是dubbo://ip:port/org.apache.dubbo.springboot.demo.DemoService.............//根据url里面的ip和端口去启动服务的Netty客户端openServer(url);
com.alibaba.nacos.client.config.impl.CacheData#listeners-------->com.alibaba.nacos.client.config.impl.CacheData.ManagerListenerWrap#listener--------->org.apache.dubbo.configcenter.support.nacos.NacosDynamicConfiguration.NacosConfigListener#listeners------->org.apache.dubbo.registry.integration.RegistryProtocol.ServiceConfigurationListener#notifyListener------->org.apache.dubbo.registry.integration.RegistryProtocol.OverrideListener
//wrap是ManagerListenerWrap对象wrap = new ManagerListenerWrap(font color=\"#e855a4\
prepareApplicationInstance();
zk:........
//先获取注册中心的URL信息,因为会有多个注册中心,所以是List,//另外如果register-mode: all这个配置为all,则一个注册中心会生成两个URL//一个以service-discovery-registry://ip:port.............开头 //一个以registry://ip:port.............开头List<URL> font color=\"#e74f4c\
2.从动态配置中心尝试根据key去配置中心获取动态配置
//从org.apache.dubbo.rpc.model.ScopeModel.beanFactory中获取ProviderConfigurationListener对象,然后获取org.apache.dubbo.registry.integration.RegistryProtocol.font color=\"#4669ea\
org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol#export
//再次获取应用级发现的监听器,ProviderConfigurationListener,为什么呢?这里是接口级别的动态配置为啥要获取ProviderConfigurationListener呢? 是因为虽然在上一步,从动态配置中心获取了应用级别的动态配置,但是并没有进行使用,就是在这里开始使用了ProviderConfigurationListener providerConfigurationListener = getProviderConfigurationListener(providerUrl);//根据应用级别的动态配置,重写providerUrlproviderUrl = providerConfigurationListener.overrideUrl(providerUrl);
remote
//这是只是将应用级别导出的url暂时存到metadataInfo对象里面,并没有往注册中心进行注册 //真正的注册是在,org.apache.dubbo.registry.client.AbstractServiceDiscovery.register()方法
final Registry registry = getRegistry(registryUrl);
//url其值为,dubbo://ip:port/org.apache.dubbo.springboot.demo.DemoService...................font color=\"#e855a4\
否
这里就是应用级别的动态配置逻辑
开始
//对于应用级别的注册,registryURL是以\"service-discovery-registry\"开头 //对于接口级别的注册,registryURL是以\"registry\"开头
向注册中心暴露
com.alibaba.nacos.client.config.impl.CacheData#listeners-------->com.alibaba.nacos.client.config.impl.CacheData.ManagerListenerWrap#listener--------->org.apache.dubbo.configcenter.support.nacos.NacosDynamicConfiguration.NacosConfigListener#listeners------->org.apache.dubbo.registry.integration.RegistryProtocol.ProviderConfigurationListener#overrideListeners------->org.apache.dubbo.registry.integration.RegistryProtocol.OverrideListener
registry.subscribe(font color=\"#e855a4\
//exporterMap是InjvmProtocol中的exporterMapreturn new InjvmExporter<T>(font color=\"#4ccbcd\
font color=\"#314aa4\
rest://ip:port/接口名?k1=v1..........
//key是org.apache.dubbo.springboot.demo.DemoService:20880//exporterMap是DubboProtocol中的exporterMapDubboExporter<T> exporter = new DubboExporter<T>(font color=\"#4ccbcd\
//根据url对象构造出,用于本地暴露的URL对象,将暴露的协议改为\"injvm\"//local的值为injvm://127.0.0.1/org.apache.dubbo.springboot.demo.DemoService URL local = URLBuilder.from(url) .setProtocol(LOCAL_PROTOCOL) .setHost(LOCALHOST_VALUE) .setPort(0) .build();
//moduleModel.getApplicationModel().getApplicationName()的值为\"dubbo-springboot-demo-provider\"//CONFIGURATORS_SUFFIX的值为\".configurators\"this.initWith(moduleModel.getApplicationModel().getApplicationName() + CONFIGURATORS_SUFFIX);
Exporter<?> exporter = protocolSPI.export(invoker);
//走这里进行org.apache.dubbo.metadata.MetadataService对于的dubbo服务的导出,节点/dubbo/org.apache.dubbo.metadata.MetadataServiceserviceConfig.export();
configVersion: v3.0configs:- addresses: - 192.168.3.28:20881 enabled: false parameters: timeout: 66666 side: providerenabled: truekey: dubbo-springboot-demo-providerscope: application
doRegister(url)org.apache.dubbo.registry.nacos.NacosRegistry#doRegister
org.apache.dubbo.registry.client.ServiceDiscoveryRegistry#register
先本地暴露
1.创建接口级别动态配置的监听器ServiceConfigurationListener
同一个方法
是,//这个判断是为了兼容2.6版本以前的动态配置,不用管了
metadataInfo.addService(url);
//font color=\"#e74f4c\
{ \"app\": \"dubbo-springboot-demo-provider\
//在这里构建进行本地元数据导出所需要的服务org.apache.dubbo.metadata.MetadataService对应的ServiceConfigthis.serviceConfig = buildServiceConfig();
根据protocols,也就是配置的具体服务所需要导出的协议如:dubbo、triple协议,开始进行循环,按照具体协议导出。如先按照dubbo协议导出
本地暴露
1.1.先注册监听器,用于监听动态配置
//构建实例编号,也就是\"metadata\"节点下的\"dubbo.metadata.revision\"的值boolean revisionUpdated = calOrUpdateInstanceRevision(this.serviceInstance);
org.apache.dubbo.registry.integration.RegistryProtocol#getProviderConfigurationListener(org.apache.dubbo.common.URL)
Instance instance = createInstance(url);//serviceName的值为\"providers:org.apache.dubbo.springboot.demo.DemoService::\
org.apache.dubbo.configcenter.support.nacos.NacosDynamicConfiguration.NacosConfigListener#listeners------->org.apache.dubbo.registry.integration.RegistryProtocol.ProviderConfigurationListener#overrideListeners------->org.apache.dubbo.registry.integration.RegistryProtocol.OverrideListener
0 条评论
回复 删除
下一页