我们看一下消费方执行流程:
ReferenceBean --> ReferenceConfig --> RegistryProtocol --> DubboProtocol --> invoker --> exporter回忆一下dubbo的spi机制,dubbo根据配置文件,从中拿到对应的扩展点,我们看一下protocol对应的配置文件:
filter=org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper
listener=org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper
mock=org.apache.dubbo.rpc.support.MockProtocol看到了ProtocolFilterWrapper和ProtocolListenerWrapper 这两个包装类,这两个即为filter和listener的扩展点。接下来看一下它们是怎么一步一步的被注入到上面的流程里的。
分析
在ReferenceConfig类中我们会引用和暴露对应的服务,我们以服务引用为场景来分析,之前提到过,服务提供方在dubboNamespaceHandler中非常重要的类为ServiceBean,对应,那么我们很容易联想到,消费方的就是ReferenceBean,看一下这个类的afterPropertiesSet方法:
...Boolean b = isInit();
if (b == null && getConsumer() != null) {
b = getConsumer().isInit();
}
if (b != null && b.booleanValue()) {
//注意这里!
getObject();
}我们看到了getObject方法,点进去,就调用了一个get(),再进入get方法,擦,又调用了init方法,点进去,在其中找到了这么几句关键的代码:
invoker = refprotocol.refer(interfaceClass, urls.get(0));