dubbo源码3-registry
dubbo-registry包封装注册中心相关代码。
api层定义registry的基本接口,主要包括:
registryservice: 定义注册的基本操作,注册、取消注册、订阅、取消订阅
registryfactory:获取注册器工厂
abstractregistry: 缓存注册、订阅以及通知信息和执行通知触发
failbackregistry: 定义失败重试逻辑,是具体注册器的直接父类,该抽象类定义doRegister/doUnRegister/doSubscribe/doUnSubscribe方法由子类实现。
integration包中的RegistryProtocol组合底层Protocol,增加注册功能,在export服务时,会通过url里的protocol参数指定使用该类作为protocol的实现类,具体代码在Protocol自动生成的adapter中,生成的代码如下:
package org.apache.dubbo.rpc; import org.apache.dubbo.common.extension.ExtensionLoader; public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol { public void destroy() { throw new UnsupportedOperationException("The method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!"); } public int getDefaultPort() { throw new UnsupportedOperationException("The method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!"); } public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException { if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null"); if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null"); org.apache.dubbo.common.URL url = arg0.getUrl(); String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() ); if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])"); org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName); return extension.export(arg0); } public java.util.List getServers() { throw new UnsupportedOperationException("The method public default java.util.List org.apache.dubbo.rpc.Protocol.getServers() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!"); } public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException { if (arg1 == null) throw new IllegalArgumentException("url == null"); org.apache.dubbo.common.URL url = arg1; String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() ); if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])"); org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName); return extension.refer(arg0, arg1); } }
主要关注的是 String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() ); 这句代码,表示从url中取protocol来决定使用哪个protocol。
具体url设置是在ServiceConfig类的如下代码:
这里的url协议是register,然后在dubbo-register包下的meta-inf的internal目录下org.apache.dubbo.rpc.Protocol里register对应的类是: InterfaceCompatibleRegistryProtocol。 该类就是RegistryProtocol的子类,所以间接实例化了RegistryProtocol。
其他包和类待补充,目前没有看到哪里有使用到。