欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

dubbo服务暴露源码分析

时间:2023-06-10

dubbo版本2.5.6

因为实现了ApplicationListener 所以在IOC容器初始化调用onApplicationEvent方法

public void onApplicationEvent(ApplicationEvent event) { if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName()) && this.isDelay() && !this.isExported() && !this.isUnexported()) { if (logger.isInfoEnabled()) { logger.info("The service ready on spring started、service: " + this.getInterface()); } this.export(); 《-进入这个方法 }}

public synchronized void export() { if (this.provider != null) { if (this.export == null) { this.export = this.provider.getExport();获取xml的export配置 } if (this.delay == null) {获取xml的delay配置 this.delay = this.provider.getDelay(); } } if (this.export == null || this.export) {是否允许暴露 if (this.delay != null && this.delay > 0) {是否需要延迟暴露 Thread thread = new Thread(new Runnable() { public void run() { try { Thread.sleep((long)ServiceConfig.this.delay); } catch (Throwable var2) { } ServiceConfig.this.doExport(); } }); thread.setDaemon(true); thread.setName("DelayExportServiceThread"); thread.start(); } else { this.doExport();暴露 } }}

doExport大部分是检测配置的过程,最后调用doExportUrlsprivate void doExportUrls() { List registryURLs = this.loadRegistries(true);获取服务注册中心list,从这个list可以看出来dubbo支持多个注册中心 //注册中心可以有很多种 Iterator var2 = this.protocols.iterator(); while(var2.hasNext()) {遍历多个协议,像注册中心注册 ProtocolConfig protocolConfig = (ProtocolConfig)var2.next(); this.doExportUrlsFor1Protocol(protocolConfig, registryURLs); }}

private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List registryURLs) { ……上边代码一直在构建URL scope = url.getParameter("scope"); if (!"none".toString().equalsIgnoreCase(scope)) { if (!"remote".toString().equalsIgnoreCase(scope)) { this.exportLocal(url);本地暴露 } if (!"local".toString().equalsIgnoreCase(scope)) { if (logger.isInfoEnabled()) { logger.info("Export dubbo service " + this.interfaceClass.getName() + " to url " + url); } 如果有注册中心就会进行远程暴露 if (registryURLs != null && registryURLs.size() > 0 && url.getParameter("register", true)) { Iterator var38 = registryURLs.iterator(); while(var38.hasNext()) { URL registryURL = (URL)var38.next(); url = url.addParameterIfAbsent("dynamic", registryURL.getParameter("dynamic")); URL monitorUrl = this.loadMonitor(registryURL); if (monitorUrl != null) { url = url.addParameterAndEncoded("monitor", monitorUrl.toFullString()); } if (logger.isInfoEnabled()) { logger.info("Register dubbo service " + this.interfaceClass.getName() + " url " + url + " to registry " + registryURL); } Invoker<?> invoker = proxyFactory.getInvoker(this.ref, this.interfaceClass, registryURL.addParameterAndEncoded("export", url.toFullString())); Exporter<?> exporter = protocol.export(invoker); this.exporters.add(exporter); } } else { Invoker<?> invoker = proxyFactory.getInvoker(this.ref, this.interfaceClass, url); Exporter<?> exporter = protocol.export(invoker);走到这个方法里面 this.exporters.add(exporter); } } } this.urls.add(url);}

RegistryProtocol#exportpublic Exporter export(Invoker originInvoker) throws RpcException { 根据dubbo协议 的到exporter final RegistryProtocol.ExporterChangeableWrapper exporter = this.doLocalExport(originInvoker); 加载 注册中心 Registry 实现类 这里的为ZookeeperRegistry final Registry registry = this.getRegistry(originInvoker); 获取注册中心的url final URL registedProviderUrl = this.getRegistedProviderUrl(originInvoker); 向zookeeper注册 registry.register(registedProviderUrl); 获取订阅url final URL overrideSubscribeUrl = this.getSubscribedOverrideUrl(registedProviderUrl); final RegistryProtocol.OverrideListener overrideSubscribeListener = new RegistryProtocol.OverrideListener(overrideSubscribeUrl); this.overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener); 向注册中心订阅 registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener); return new Exporter() { public Invoker getInvoker() { return exporter.getInvoker(); } public void unexport() { try { exporter.unexport(); } catch (Throwable var4) { RegistryProtocol.logger.warn(var4.getMessage(), var4); } try { registry.unregister(registedProviderUrl); } catch (Throwable var3) { RegistryProtocol.logger.warn(var3.getMessage(), var3); } try { RegistryProtocol.this.overrideListeners.remove(overrideSubscribeUrl); registry.unsubscribe(overrideSubscribeUrl, overrideSubscribeListener); } catch (Throwable var2) { RegistryProtocol.logger.warn(var2.getMessage(), var2); } } };}

dolocalExport 暴露url 打开端口private RegistryProtocol.ExporterChangeableWrapper doLocalExport(Invoker originInvoker) { String key = this.getCacheKey(originInvoker); RegistryProtocol.ExporterChangeableWrapper exporter = (RegistryProtocol.ExporterChangeableWrapper)this.bounds.get(key); if (exporter == null) { synchronized(this.bounds) { exporter = (RegistryProtocol.ExporterChangeableWrapper)this.bounds.get(key); if (exporter == null) { Invoker<?> invokerDelegete = new RegistryProtocol.InvokerDelegete(originInvoker, this.getProviderUrl(originInvoker)); exporter = new RegistryProtocol.ExporterChangeableWrapper(this.protocol.export(invokerDelegete), originInvoker); this.bounds.put(key, exporter); } } } return exporter;}

DubboProtocol#exporterpublic Exporter export(Invoker invoker) throws RpcException { URL url = invoker.getUrl(); 获取dubbo url dubbo://192...:20880/ String key = serviceKey(url); DubboExporter exporter = new DubboExporter(invoker, key, this.exporterMap); this.exporterMap.put(key, exporter); Boolean isStubSupportEvent = url.getParameter("dubbo.stub.event", false); Boolean isCallbackservice = url.getParameter("is_callback_service", false); if (isStubSupportEvent && !isCallbackservice) { String stubServiceMethods = url.getParameter("dubbo.stub.event.methods"); if (stubServiceMethods != null && stubServiceMethods.length() != 0) { this.stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods); } else if (this.logger.isWarnEnabled()) { this.logger.warn(new IllegalStateException("consumer [" + url.getParameter("interface") + "], has set stubproxy support event ,but no stub methods founded.")); } } this.openServer(url); **** return exporter;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。