Dubbo-发布服务执行的流程

如题所述

第1个回答  2022-07-05
我们以dubbo 的xml配置为例:

dubbo服务发布只需在spring.xml中如下配置即可:

<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

通过dubbo于spring的融合可以了解到<dubbo:service>标签是通过ServiceBean解析封装。

ServiceBean这个类继承 了ServiceConfig实现了spring的5个接口

InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware 来融入到spring的启动过程。

ServiceBean实现了 ApplicationListener 接口,当spring容器触发了ContextRefreshedEvent事件时,

就会调用 ServiceConfig 中的export()方法发布申明的dubbo服务,

ServiceConfig 中的export()方法部分源码如下,如果申明了delay(延迟多少),那么延迟调用doExport()发布这个服务,如果没有设置则直接调用doExport()发布服务:

接下来看ServiceConfig的doExport()方法

1,检查中是否配置了interface, 如果为空,那么抛出异常:

if (interfaceName == null || interfaceName.length() == 0) { throw new IllegalStateException("interface not allow null!");}

2,检查接口类型必需为接口

if(! interfaceClass.isInterface()) {

throw new IllegalStateException("The interface class " + interfaceClass + " is not a interface!");

}

3,检查方法是否在接口中存在

4,检查引用不为空,并且引用必需实现接口 interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"

5,检查checkApplication(); checkRegistry(); checkProtocol();有效性。

6,调用ServiceConfig.doExportUrls()发布dubbo服务

ServiceConfig.doExportUrls()如下:

通过调用loadRegistries(true)得到所有registry的url地址,例如配置了

<dubbo:registry address="zookeeper://127.0.0.1:2181">

配置结果为dubbo.registry.address=zookeeper://127.0.0.1:2181;

protocols就是将要发布服务的协议集合(dubbo服务可以同时暴露多种协议),例如配置了

<dubbo:protocol name="dubbo" port="20880">

dubbo.protocol.name=dubbo ,  dubbo.protocol.port=20880

ServiceConfig.doExportUrlsFor1Protocol()

先把application、module、provider、protocol、exporter、registries、monitor所有属性封装到Map中例如protocol=dubbo,host=10.0.0.1,port=20880,path=com.alibaba.dubbo.demo.TestService等,然后构造dubbo定义的统一数据模型URL:

URL url = new URL(name, host, port, (contextPath == null || contextPath.length() == 0 ? "" : contextPath + "/") + path, map);

这个url非常重要,贯穿整个dubbo服务的发布和调用过程,可以在服务发布后在dubbo-monitor中看到;

ServiceConfig.doExportUrlsFor1Protocol()中根据scope判断服务的发布范围:

如果配置scope = none, 那么不需要发布这个dubbo服务;

没有配置scope = none,且配置的scope != remote, 那么本地暴露 这个dubbo服务;

没有配置scope = none,且配置的scope != remote且配置的scope != local,那么远程暴露这个dubbo服务(例如远程暴露这个服务到zk上,默认情况下scope没有配置,就是在这里发布服务);

以上如果执行成功,会把dubbo服务到zookeeper上,invoker.getUrl()的值为

registry://10.0.53.87:2188/com.alibaba.dubbo.registry.RegistryService?application=dubbo-test&dubbo=2.0.0&export=dubbo%3A%2F%2F10.52.16.218%3A20886%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-test%26dubbo%3D2.0.0%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dafei%26pid%3D2380%26side%3Dprovider%26timestamp%3D1509953019382&owner=afei&pid=2380®istry=zookeeper×tamp=150995301934:

接下来我们分析

Protocol.export()暴露服务接口:

然后调用RegistryProtocol.export():

核心调用registry.register(registedProviderUrl)。

调用AbstractRegistry.register(URL),把这次需要注册的URL加到Set registered中,即本地缓存新的注册URL;

在ZookeeperRegistry.doRegister(URL)调用AbstractZookeeperClient.create(),toUrlPath将URL形式的地址转换成zookeeper路径,最终在AbstractZookeeperClient中把需要发布的服务的URL保存到zookeeper:

ZookeeperRegistry.doRegister(url)注册服务如果失败:

如果开启了启动检查check=true,那么直接抛出异常;

如果没有开启启动检查,那么将失败的注册请求记录到失败列表,定时重试;

核心调用registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener):

对发布的dubbo服务的这个url进行监听, 当服务变化有时通知重新暴露服务, 以zookeeper为例,暴露服务会在zookeeper生成一个节点,当节点发生变化的时候会触发overrideSubscribeListener的notify方法重新暴露服务

注册服务失败的重试机制:

注册服务失败后,会将url加入重试url集合中,failedRegistered.add(url);重试任务在FailbackRegistry中实现:

注册的监听机制:

订阅并设置监听registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);

--> FailbackRegistry.subscribe(URL url, NotifyListener listener)

--> ZookeeperRegistry.doSubscribe(final URL url, final NotifyListener listener),部分实现源码如下:

当服务有变化的时候:

doNotify(url, listener, urls);

AbstractRegistry.notify(URL url, NotifyListener listener, List urls)

--> RegistryDirectory.notify(List urls)

--> RegistryDirectory.refreshInvoker(List invokerUrls),这里调用toMethodInvokers(Map> invokersMap)的实现比较重要,将invokers列表转成与方法的映射关系,且每个方法对应的List需要通过Collections.sort(methodInvokers, InvokerComparator.getComparator());排序,然后,还要将其转为unmodifiable的map

其中 InvokerComparator 的定义如下,即直接根据url进行比较排序

dubbo协议发布服务会调用DubboProtocol.export()的过程:

从Invoker中获取URL: URL url = invoker.getUrl();

根据URL得到key, 由暴露的服务接口+端口组成,例如com.alibaba.dubbo.demo.DemoService:20886 ;  String key = serviceKey(url);

构造DubboExporter存到Map中local cache化:

DubboExporter exporter = new DubboExporter(invoker, key, exporterMap); exporterMap.put(key, exporter);

调用DubboProtocol.openServer()开启netty(默认)服务保持通信,并设置requestHandler处理consumer对provider的调用请求;

DubboProtocol.openServer():

key的值就是IP:Port,例如10.52.17.167:20886,根据key从serverMap中如果取不到ExchangeServer,表示还没绑定服务端口,需要调用createServer(url)-->Exchangers.bind(url, requestHandler)-->Transporters.getTransporter().bind(url, handler)(dubbo支持mina,netty,grizzly,默认实现是netty) --> NettyTransporter.bind(URL, ChannelHandler) --> NettyServer.open();

dubbo默认调用的是netty

Netty服务几个重要的地方

构造 ChannelPipeline 时指定了编码&解码,其中编码为NettyCodecAdapter.getEncoder(),解码为NettyCodecAdapter.getDncoder();

指定了handler为final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);处理请求;
相似回答