Dubbo源码学习--服务是如何发布的
相关文章:
ServiceBean
ServiceBean 实现ApplicationListener接口监听ContextRefreshedEvent事件(容器加载完成事件)
public void onApplicationEvent(ApplicationEvent event) {
if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
if (isDelay() && ! isExported() && ! isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + getInterface());
}
export();
}
}
在容器加载完成后执行export(); 开始暴露
ServiceConfig 类
方法执行顺序:export() -> doExport() -> doExportUrls() -> doExportUrlsFor1Protocol();
export() 判断是否延迟发布,如果延迟发布会新建个Daemon线程然后调用doExport(), 否则直接调用doExport();
doExport() 给ServiceConfig 装载注册中心监控中心等。
doExportUrls()
private void doExportUrls() {
List<URL> registryURLs = loadRegistries(true);
for (ProtocolConfig protocolConfig : protocols) {
doExportUrlsFor1Protocol(protocolConfig, registryURLs);
}
}
-
执行loadRegistries()遍历注册中心,根据注册中心、Dubbo版本、Pid等生成要发布的URL;
URL示例: zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=ordercenter_serviceImpl
&dubbo=2.8.4&pid=15836®istry=zookeeper×tamp=1484018365125 -
遍历服务协议,为每个协议执行doExportUrlsFor1Protocol()
doExportUrlsFor1Protocol()
Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString()));
Exporter<?> exporter = protocol.export(invoker);
- 创建 Invoker
- 调用 protocol.export() 将Invoker 转换成Exporter
DubboProtocol类
方法执行顺序:export() -> openServer() ->createServer()
createServer() 通过Exchangers.bind() 创建 ExchangeServer
RegistryProtocol类
DubboProtocol export() 执行完成后最终返回上层包装类 RegistryProtocol类
RegistryProtocol export() 进行服务注册和变更订阅
public <T> Exporter<T> export(final Invoker<T> originInvoker) throws RpcException {
//export invoker
final ExporterChangeableWrapper<T> exporter = doLocalExport(originInvoker);
//registry provider
final Registry registry = getRegistry(originInvoker);
final URL registedProviderUrl = getRegistedProviderUrl(originInvoker);
registry.register(registedProviderUrl);
// 订阅override数据
// FIXME 提供者订阅时,会影响同一JVM即暴露服务,又引用同一服务的的场景,因为subscribed以服务名为缓存的key,导致订阅信息覆盖。
final URL overrideSubscribeUrl = getSubscribedOverrideUrl(registedProviderUrl);
final OverrideListener overrideSubscribeListener = new OverrideListener(overrideSubscribeUrl);
overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener);
registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);
//....
}