在学习篇3中 serverbean中执行导出的最后代码为
Exporter> exporter = protocol.export(wrapperInvoker);
其中protocol为spi机制动态生成的对象,而且依次调用了RegistryProtocol DubboProtocol
public
这里调用流程涉及到spi的一些细节,发现并不是简单的比java原生的spi多了key-value对应的过程,本文记录了学习的过程。
一 . @Adaptive 机制
调用时 实际的protocol 其实是一个类的全局适配器对象,该对象负责执行时创建真正的执行对象 最终Protocol的适配器如下
private static final Protocol protocol = (Protocol)ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
public class Protocol$Adaptive implements com.alibaba.dubbo.rpc.Protocol {
public void destroy() {throw new UnsupportedOperationException("method public abstract void com.alibaba.dubbo.rpc.Protocol.destroy() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public int getDefaultPort() {throw new UnsupportedOperationException("method public abstract int com.alibaba.dubbo.rpc.Protocol.getDefaultPort() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");com.alibaba.dubbo.common.URL url = arg0.getUrl();
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.export(arg0);
}
public com.alibaba.dubbo.rpc.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1) throws com.alibaba.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg1;
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.refer(arg0, arg1);
}
}
重点为
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
之后根据实际的protocol获取到真正的protocol对象,而调试发现传入的invoker的url如下
registry://45.40.199.157:2181/com.alibaba.dubbo.registry.RegistryService?application=springboot-dubbo-provider&dubbo=2.6.2&export=dubbo%3A%2F%2F192.168.31.212%3A20880%2Forg.boot.dubbo.api.service.HelloService%3Fanyhost%3Dtrue%26application%3Dspringboot-dubbo-provider%26bind.ip%3D192.168.31.212%26bind.port%3D20880%26dubbo%3D2.6.2%26generic%3Dfalse%26interface%3Dorg.boot.dubbo.api.service.HelloService%26methods%3DsayHello%2CsayHello1%26owner%3DLuoLiang%26pid%3D6900%26revision%3D1.0.0%26serialization%3Dkryo%26side%3Dprovider%26timestamp%3D1570003481286%26version%3D1.0.0&owner=LuoLiang&pid=6900®istry=zookeeper×tamp=1570003481281
查看系统spi定义文件找到对应
registry=com.alibaba.dubbo.registry.integration.RegistryProtocol
可知这一步首先实例化一个RegistryProtocol 来处理登记任务。
二.注入包装实例 包装类
private T injectExtension(T instance) {
try {
if (this.objectFactory != null) {
Method[] arr$ = instance.getClass().getMethods();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
Method method = arr$[i$];
if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers())) {
Class pt = method.getParameterTypes()[0];
try {
String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";
Object object = this.objectFactory.getExtension(pt, property);
if (object != null) {
method.invoke(instance, object);
}
} catch (Exception var9) {
logger.error("fail to inject via method " + method.getName() + " of interface " + this.type.getName() + ": " + var9.getMessage(), var9);
}
}
}
}
} catch (Exception var10) {
logger.error(var10.getMessage(), var10);
}
return instance;
}
Set
Class wrapperClass;
if (wrapperClasses != null && !wrapperClasses.isEmpty()) {
for(Iterator i$ = wrapperClasses.iterator(); i$.hasNext(); instance = this.injectExtension(wrapperClass.getConstructor(this.type).newInstance(instance))) {
wrapperClass = (Class)i$.next();
}
}
通过name获取到的真实实例的同时 ExtensionLoader 会尝试读取所有的setXXX的方法 并注入一个Adaptive类型实例,然后会依次找到spi配置文件中的包装类依次进行包装 这样最终会生成一个注入了所有setXXX方法 并依次包装后返回最后包装器类型的实例(如何判断是否是当前实例的包装类呢?-- 是否有当前实例类型的构造函数)
三.结合步骤一,二
查看RegistryProtocol 发现有以下set函数 可知 将会被注入Cluter Protocol
public void setCluster(Cluster cluster) {
this.cluster = cluster;
}
public void setProtocol(Protocol protocol) {
this.protocol = protocol;
}
同时还被包装了ProtocolListenerWrapper,ProtocolFilterWrapper,QosProtocolWrapper分别处理扩展事件 本文不详细描述
重要的这里注入的Protocol 实例会在RegistryProtocol的doLocalExport
Invoker> invokerDelegete = new RegistryProtocol.InvokerDelegete(originInvoker, this.getProviderUrl(originInvoker));
exporter = new RegistryProtocol.ExporterChangeableWrapper(this.protocol.export(invokerDelegete), originInvoker);
调用之前设置的protocol 此时URL变成提供者拼装的url 如下
dubbo://192.168.31.212:20880/org.boot.dubbo.api.service.HelloService?anyhost=true&application=springboot-dubbo-provider&bind.ip=192.168.31.212&bind.port=20880&dubbo=2.6.2&generic=false&interface=org.boot.dubbo.api.service.HelloService&methods=sayHello,sayHello1&owner=LuoLiang&pid=6900&revision=1.0.0&serialization=kryo&side=provider×tamp=1570003481286&version=1.0.0
可以确认本次调用会执行DubboProtocol的export的方法。
本文主要探究了dubbo之spi下的 @Adaptive 机制 以及默认扩展行为 ,基于包装类的扩展行为可以实现拦截 listener qos server之类的感兴趣可以自行查看。