ServiceBean中的
afterPropertiesSet方法:获取当前项目所有的具体配置项(Registry,Module,Application,Monitor)。
onApplicationEvent方法:初始化完容器以后,因为ServiceBean继承了ApplicationListener,所以会执行onApplicationEvent方法中。
执行onApplicationEvent方法中的export方法,获取该service其他配置项,如是否暴露等。
ServiceConFig中的
doExport方法:接着执行ServiceConFig中的doExport方法,预备暴露,修改暴露状态,获取该service其他配置项,如使用的协议,注册地址,获取接口类型等,
继续检查各种项目,将各个配置项组合生成AbstractServiceConfig,AbstractInterfaceConfig等组合类,检查完毕,并且AbstractServiceConfig等类整理完毕以后,执行真正的doExportUrls方法。
doExportUrls方法:
loadRegistries方法:将之前doExport方法中的注册配置项整理成RegistryConfig类,并且整理生成Service的注册URL
registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997,
循环协议配置项,执行doExportUrlsFor1Protocol方法,参数为协议配置项与注册URL。
doExportUrlsFor1Protocol方法:
获取协议的name,如果为空,默认为dubbo协议。获取service本地Host的IP,查看IP是否是本地,如果是本地服务,则直接开启socket,获取serivce端口默认为20800,将解析的协议封装为ProtocolConfig对象存进map对象进行缓存,获取service接口的方法名称加以缓存。
生成service服务的URL:
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
根据暴露状态进行暴露,配置中没有注明scope,则进行service本地暴露,执行exportLocal方法。
exportLocal方法:将本地服务生成的URL作为参数传入,将service服务URL转换成本地service服务URL:
injvm://127.0.0.1/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
进入protocol层,执行export方法,参数为invoker类,该invoker类包含真正的service实现类,接口,以及本地service服务URL。
生成invoker类,默认执行JavassistProxyFactory生成代理。生成完毕,执行ProtocolListenerWrapper类的export方法,参数为invoker。
ProtocolListenerWrapper类中
export方法:为invoker装饰,添加listener和filter。伪装成invoker返回ServiceConFig中的exportLocal方法。
ServiceConFig中的
doExportUrlsFor1Protocol方法:
生成最终的invoker参数为实际service类,类接口,以及修改后的注册URL
registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997
将最终的invoker包含service代理类,注册URL(注册中心URL和本地服务URL拼接而成)进行导出,执行类RegistryProtocol的export方法,参数为最终invoker。
进入protocol层,
RegistryProtocol中的
export方法:
执行doLocalExport方法,参数为最终invoker
doLocalExport方法:
对invokerURL进行编码
registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997
对整理protocal时获取的map中的export属性值进行编码export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014
整理对应service的provider的URL,
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
将此provider的URL作为key向本地缓存寻找是否有exporter。如果没有,将invoker封装为InvokerDelegete(包含invoker与本地provider的url)则生成最终暴露服务的exporter,暴露服务由DubboProtocol类的export方法进行,参数为InvokerDelegete。
DubboProtocol中
export方法:
将本地provider的URL转换为变量key,key为com.alibaba.dubbo.demo.DemoService:20880,将此key与传入的InvokerDelegete,以及exporterMap生成新的DubboExporter,通过ConcurrentHashMap进行缓存,执行开启本地netty服务openServer方法,参数为本地provider的URL:
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
openServer方法:
将URL的host以及IP地址以及Port提取出来,192.168.10.107:20880作为key,本地缓存查找是否存在该server,没有则创建,server为dubbo定义的ExchangeServer类。创建server的方法名为createServer方法,参数为本地provider服务的URL:
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
createServer方法:
首先对传过来的本地provider的URL进行修改,针对readonly事件
原:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
现:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
再对上面的URL进行修改,针对开启heartbeat
原:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
现:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&dubbo=2.0.0&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
获取默认的server开启方式为netty
继续修改URL,针对编码格式以及负载均衡的配置
原:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&dubbo=2.0.0&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
现:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&codec=dubbo&dubbo=2.0.0&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
将最终修改的URL传给Exchangers进行bind,参数为最终URL与生成DubboProtocol类时创建的成员变量ExchangeHandler requestHandler = new ExchangeHandlerAdapter(),requestHandler 中存在回调函数reply。
Exchangers中
bind方法:
通过spi机制,获取继承了Exchanger接口的实现类,通过meta-inf文件夹下的com.alibaba.dubbo.remoting.exchange.Exchanger文件,获取内容:header=com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger,也就是实现类为HeaderExchanger
HeaderExchanger中:
bind方法中:
通过Transporters类进行bind,先生成HeaderExchangeHandler类,HeaderExchangeHandler类持有ExchangeHandler类,ExchangeHandler类来自于DubboProtocol类初始化生成的成员变量,ExchangeHandler持有最主要的InvokerDelegete,继续生成DecodeHandler,DecodeHandler持有HeaderExchangeHandler。将最终生成的DecodeHandler传给Transporters类,执行bind方法,
参数为provider的本地服务url,以及最终生成的DecodeHandler。DecodeHandler持有最重要的InvokerDelegete类。根据spi机制,默认使用NettyTransporter进行最终bind方法的执行。
NettyTransporter中
bind方法:
创建NettyServer类,参数为DecodeHandler与本地provider本地服务URL:
URL为dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&codec=dubbo&dubbo=2.0.0&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
NettyServer中
NettyServer构造方法:
创建线程名称,默认开头为“DubboServerHandler”,根据provider本地服务URL进行修改为DubboServerHandler-192.168.10.107:20880,将此线程名称加入到本地服务URL的属性中key为threadname。返回的结果线程名称与DecodeHandler一起进入ChannelHandlers类中的wrap方法。
ChannelHandlers中
wrap方法:
跳转到wrapInternal方法
wrapInternal方法:
根据spi机制,执行AllDispatcher类中的dispatch方法,生成AllChannelHandler类。
AllChannelHandler中:
AllChannelHandler构造方法:
跳转父类WrappedChannelHandler类的构造方法
WrappedChannelHandler中
WrappedChannelHandler构造方法:
根据spi机制,执行FixedThreadPool类中的getExecutor方法,参数为provider本地服务url
FixedThreadPool中
getExecutor方法:
根据provider本地服务url:dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&channel.readonly.sent=true&codec=dubbo&dubbo=2.0.0&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider&threadname=DubboServerHandler-192.168.10.107:20880×tamp=1480988981014
以及URL中的参数列表:{owner=william, side=provider, heartbeat=60000, methods=sayHello, dubbo=2.0.0, loadbalance=roundrobin, pid=10000, interface=com.alibaba.dubbo.demo.DemoService, generic=false, codec=dubbo, application=demo-provider, channel.readonly.sent=true, anyhost=true, timestamp=1480988981014, threadname=DubboServerHandler-192.168.10.107:20880}
获取参数列表中的线程名称:DubboServerHandler-192.168.10.107:20880
获取默认的线程池线程数量:200
获取queues数量为:0
创建jdk,concurrent包中的new ThreadPoolExecutor线程池任务,参数为(threads=200,threads=200,根据queues数值取队列类型,0为SynchronousQueue,小于0为LinkedBlockingQueue,大于0指定数值的LinkedBlockingQueue)
创建完线程池之后回到WrappedChannelHandler的构造方法。创建SimpleDataStore对象,将字符串“java.util.concurrent.ExecutorService”,url的port,与线程池一起合并在该对象的map中。
接着进入AbstractServer类中
AbstractServer中
AbstractServer构造方法:
通过url以及handler封装成AbstractPeer类,通过url取得本地连接/192.168.10.107:20880,获取host0.0.0.0,拼接url/0.0.0.0:20880,执行NettyServer类中的doOpen方法开启nettyServer。
NettyServer中
doOpen方法:
开启默认的netty的log日志,继承自InternalLoggerFactory类,通过new ServerBootstrap(channelFactory);开启netty服务器。返回给DubboProtocol类中的createServer方法,返回openServer方法。将创建的nettyServer存放在DubboProtocol类的成员变量serverMap中进行缓存,key为“192.168.10.107:20880”。
返回export方法,将创建的DubboExporter类进行缓存,缓存在exporterMap中,key为“com.alibaba.dubbo.demo.DemoService:20880”,返回ProtocolFilterWrapper类中的export方法,执行filter过滤,返回RegistryProtocol类中的doLocalExport方法,将生成的Exporter与最初的Invoker进行绑定,再次封装成成Exporter,将Exporter类进行缓存,到RegistryProtocol类中的bounds中,key为provider的url:
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
RegistryProtocol中
getRegistry方法:
参数originInvoker,原始invoker,包含信息如下:
com.alibaba.dubbo.demo.provider.DemoServiceImpl@5d99c6b5
com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory@5a5338df
interface com.alibaba.dubbo.demo.DemoService
registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997
{owner=william, registry=zookeeper, backup=192.168.10.233:2181,192.168.10.234:2181, application=demo-provider, dubbo=2.0.0, pid=10000, export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014, timestamp=1480988471997}
com.alibaba.dubbo.common.bytecode.Wrapper1@2a225dd7
获取invoker中的注册中心URL地址:registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997
根据协议以及注册地址,修改URL地址
原URL:registry://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000®istry=zookeeper×tamp=1480988471997
现URL:zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000×tamp=1480988471997
将最后的URL传入AbstractRegistryFactory类中的getRegistry方法
AbstractRegistryFactory中
getRegistry方法:
处理URL,将传过来的URL中对于service服务的URL去掉,只取前面的注册中心地址:
原:zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&export=dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014&owner=william&pid=10000×tamp=1480988471997
现:zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
对该注册中心地址进行修改
原:zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
现:zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService
从缓存中查找注册中心,如果不存在,则执行createRegistry方法,参数为现URL,zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService,执行类为ZookeeperRegistryFactory,方法为createRegistry方法。
ZookeeperRegistryFactory中
createRegistry方法:
创建ZookeeperRegistry对象,参数为zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
与zookeeperTransporter对象,进入ZookeeperRegistry类的构造方法ZookeeperRegistry。进入AbstractRegistry类中,执行构造方法。
AbstractRegistry中
AbstractRegistry构造方法:
参数为zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
设置成员变量registryUrl为该参数URL,创建临时文件C:\Users\sea/.dubbo/dubbo-registry-192.168.10.232.cache与dubbo-registry-192.168.10.232.cache.lock
执行notify方法,参数为配置的zookeeper集群中的其他IPbackup=192.168.10.233:2181,192.168.10.234:2181
notify方法:
参数为配置的所有zk路径:
zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
zookeeper://192.168.10.233:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
zookeeper://192.168.10.234:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
查看是否已经订阅,如果已经订阅,则获取所有相关的NotifyListener执行notify回调方法,生成注册中心失败的话,异步重试。返回ZookeeperRegistry类的ZookeeperRegistry构造方法。
ZookeeperRegistry中
ZookeeperRegistry构造方法:
根据URL的group属性创建zk节点的跟目录,group属性为dubbo,修改为/dubbo,赋值给ZookeeperRegistry类的成员变量root,通过zookeeperTransporter类的connect方法连接注册中心,参数为:
zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
ZkclientZookeeperTransporter中
connect方法:
创建ZkclientZookeeperClient对象。
ZkclientZookeeperClient中:
ZkclientZookeeperClient构造方法:
将注册中心URL进行缓存,创建KeeperState.SyncConnected,获取url的中所有IP,192.168.10.232:2181,192.168.10.233:2181,192.168.10.234:2181
创建ZkClient节点,对客户端执行subscribeStateChanges方法。将client封装成ZkclientZookeeperClient类,将ZkclientZookeeperClient类封装成ZkclientZookeeperTransporter类,返回ZookeeperClient。
回到ZookeeperRegistry类中的ZookeeperRegistry构造方法。当前zk目录下节点存在dubbo,dubbo下存在节点com.alibaba.dubbo.demo.DemoService,com.alibaba.dubbo.demo.DemoService下有providers节点,
ZookeeperRegistry中
ZookeeperRegistry构造方法:
对返回的ZookeeperClient添加监听事件,存在AbstractZookeeperClient类中的stateListenersSet集合中,当状态为RECONNECTED(重新连接)时,执行recover方法,将该registry进行缓存于AbstractRegistryFactory类中的REGISTRIES中,key为zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService
返回RegistryProtocol类中的export方法,获取到已经与注册中心连接的Url,执行FailbackRegistry类中的register方法。
FailbackRegistry中
register方法:
参数URL:
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
将url参数进行缓存,执行doRegister方法,参数为该provider类的URL,执行ZookeeperRegistry类中的doRegister方法。
ZookeeperRegistry中
doRegister方法:
执行AbstractZookeeperClient类中的create方法。
AbstractZookeeperClient中
create方法:
将provider本地URL转换为类型文件路径的路径:
/dubbo/com.alibaba.dubbo.demo.DemoService/providers/dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014
判断是否是临时节点还是持久化节点,默认为持久化节点,递归执行ZkclientZookeeperClient类中的方法,调用原生zkClient的createPersistent方法,路径为/dubbo/com.alibaba.dubbo.demo.DemoService/providers,最底层目录为临时目录/dubbo/com.alibaba.dubbo.demo.DemoService/providers/dubbo%3A%2F%2F192.168.10.107%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.0%26generic%3Dfalse%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dwilliam%26pid%3D10000%26side%3Dprovider%26timestamp%3D1480988981014
返回RegistryProtocol类中的export方法。
RegistryProtocol中
export方法:
参数为
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
将URL替换为
provider://192.168.10.107: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&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
创建overrideSubscribeListener,向注册中心订阅Registry,进入FailbackRegistry类中的subscribe方法,将该服务提供者provider的overrideSubscribeListener进行缓存。
FailbackRegistry中
subscribe方法:
向服务器端发送订阅请求,参数为URL:
provider://192.168.10.107: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&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
以及该服务提供者provider的overrideSubscribeListener。执行ZookeeperRegistry类中的doSubscribe方法。
ZookeeperRegistry中
doSubscribe方法:
该服务提供者provider的URL添加进注册中心Listeners的列表中,在zk下创建节点/dubbo/com.alibaba.dubbo.demo.DemoService/configurators。执行notify方法,
参数为
provider://192.168.10.107: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&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
服务提供者provider的overrideSubscribeListener。进入AbstractRegistry类中notify方法中
AbstractRegistry中
notify方法:
将configurators节点下的所有url检出,执行该服务提供者下的overrideSubscribeListener。进入RegistryProtocol中的notify方法,覆盖已经有的服务地址。
回到RegistryProtocol中
export方法:
返回新的Exporter对象,过滤ProtocolListenerWrapper中的export方法。将最终的exporter导出
该对象包含:
com.alibaba.dubbo.registry.integration.RegistryProtocol@43f82e78
com.alibaba.dubbo.registry.integration.RegistryProtocol$ExporterChangeableWrapper@2a448449
com.alibaba.dubbo.registry.integration.RegistryProtocol$OverrideListener@753432a2
provider://192.168.10.107: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&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
zookeeper://192.168.10.232:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&backup=192.168.10.233:2181,192.168.10.234:2181&dubbo=2.0.0&interface=com.alibaba.dubbo.registry.RegistryService&owner=william&pid=10000×tamp=1480988471997
最终回到ServiceConfig类中,doExportUrlsFor1Protocol方法中,并将URL进行缓存。
dubbo://192.168.10.107:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=10000&side=provider×tamp=1480988981014
相关推荐
《Dubbo源码分析系列》是一份深入探讨Java开源框架Dubbo核心原理和技术细节的资料。Dubbo,作为阿里巴巴的一款高性能、轻量级的服务治理框架,它为分布式系统提供了服务发现、调用、负载均衡、容错等关键功能。这份...
### Dubbo源码解析2 #### 一、源码阅读路径 在开始深入解析Dubbo源码之前,首先需要明确的是,Dubbo虽然代码量不算庞大,但是它涉及的技术领域非常广泛,对于初学者来说,可能需要具备一定的前置知识才能更好地...
1. **Dubbo架构设计**:书中详细介绍了Dubbo的整体架构,包括服务提供者、消费者、注册中心和服务监控等组件,以及它们之间的交互方式。理解这一部分有助于开发者把握Dubbo的工作流程。 2. **Remoting层解析**:...
本文对dubbo源码进行了深入的解析,涵盖了dubbo的架构、核心机制分析、扩展点加载流程、代理机制、远程调用流程、集群和容错处理、监控机制等多个方面。通过阅读和理解这些内容,可以更好地掌握dubbo的内部工作机制...
通过深入学习和实践Dubbo源码,开发者不仅可以更好地理解Dubbo的工作原理,还能在实际项目中灵活运用,解决各种复杂的分布式服务问题。Dubbo的源码阅读也是一个不断提升自身技术深度的过程,有助于成长为更优秀的...
了解Dubbo源码通常从其主要模块入手,如服务提供者(Provider)、服务消费者(Consumer)、注册中心(Registry)、协议层(Protocol)等。通过跟踪调用流程,理解服务发布、订阅、调用过程。 2. **Dubbo诞生的背景*...
《Dubbo源码解析2.0》是一份深入剖析阿里巴巴开源框架Dubbo核心机制的资料,专注于2.0版本的源代码分析。Dubbo作为Java领域最知名的分布式服务框架之一,其设计理念、实现原理以及在实际应用中的优化策略都是开发者...
2. HelloWorld例子:通过最简单的例子,理解Dubbo服务的提供者和消费者是如何交互的。 3. 源文件概述:对源码的各个模块和文件有一个概览,明白它们的作用和相互关系。 4. 核心机制分析:详细分析Dubbo的设计模式...
实现Dubbo源码,我们需要关注以下几个关键知识点: 1. **服务接口定义**:服务的接口定义是服务调用的基础,通常通过Java接口来实现,包含了服务提供者需要对外提供的所有方法。 2. **服务暴露与引用**:服务提供...
通过以上机制,Dubbo能够实现对服务提供者、消费者、协议、注册中心等组件的动态扩展。这种高度可插拔的架构使得Dubbo能够适应各种复杂场景,同时降低了模块间的耦合度。 在实际应用中,理解并掌握Dubbo SPI的工作...
1. **创建服务接口**:定义服务提供者和消费者共同遵循的业务接口。 2. **实现服务接口**:在服务提供者端,实现接口并使用`@Service`注解标记,表明这是一个对外提供服务的类。 3. **配置服务提供者**:配置`...
Apache Dubbo 3.0.7 是一个高性能、轻量级的开源Java RPC框架,它由阿里集团贡献并维护,现已成为Apache顶级项目。Dubbo的主要目标是提供一种...同时,掌握Dubbo源码也有助于与社区保持同步,及时了解和应用最新特性。
这个"Dubbo+Zookeeper源码(提供者+消费者实例)"是一个针对初学者的实践教程,旨在帮助理解这两个组件如何协同工作。 1. **Dubbo**:Dubbo的核心功能包括服务注册、服务发现、负载均衡、容错机制和监控等。在源码中...
服务提供者是Dubbo架构中的基础组件,它负责暴露服务。在源码中,`com.alibaba.dubbo.config.ApplicationConfig`和`com.alibaba.dubbo.config.ServiceConfig`类是配置服务提供者的入口。`ServiceConfig`定义了服务的...
《Dubbo源码解析:深度探索高性能服务框架》 Dubbo作为阿里巴巴开源的一款高性能、轻量级的服务治理框架,其源码的深入理解和分析对于Java开发者来说,具有极高的学习价值。本文将针对提供的dubbo-2.4.3-sources....
《深入剖析Dubbo源码:探索Java微服务框架的核心机制》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的Java远程服务调用框架,它为开发者提供了丰富的服务治理功能,包括服务注册与发现、负载均衡、容错、监控等。...
【Dubbo源码+jar包】的资源包含了Dubbo的核心组件和源代码,这为我们深入理解这个分布式服务框架的工作原理提供了宝贵的资料。Dubbo是由阿里巴巴开源的,它旨在提高微服务架构下的服务治理效率,同时也支持多种通信...
《Dubbo源码深度解析》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的Java服务框架,其源码的深度研究对于理解分布式服务治理、RPC机制以及微服务架构有极大的帮助。本文将深入探讨Dubbo的核心设计理念和关键组件...
这个“dubbo的简单demo源码”是为了帮助初学者理解和实践Dubbo的核心概念,包括服务提供者(Provider)、服务消费者(Consumer)以及它们之间的交互。 【描述】中提到,你需要修改POM文件的目标目录,这通常是指在...