dubbo RegistryProtocol何时加载

当启动provider服务配置时,

serviceConfig.export();

会在ServiceConfig中调用Protocol.export启动连接:

Invoker<?> invoker = PROXY_FACTORY.getInvoker(ref, (Class) interfaceClass,
                               registryURL.addParameterAndEncoded(EXPORT_KEY, url.toFullString()));

DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this);

Exporter<?> exporter = PROTOCOL.export(wrapperInvoker);
                        exporters.add(exporter);

我看别人写的源码分析,此时的ProtocolRegistryProtocol

然后我就想知道RegistryProtocol何时加载的,默认的Protocol不应该是DubboProtocol么?


@SPI("dubbo")
public interface Protocol {

 @Adaptive
 <T> Exporter<T> export(Invoker<T> invoker) throws RpcException;


}
dubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
阅读 2.1k
2 个回答

一开始url里的protocol是registry,在org.apache.dubbo.registry.integration.RegistryProtocol#export里移除并设置为dubbo

因为 Protocol@Adaptive 修饰的

ServiceConfig中是这么初始化的

// 初始化的是一个 Adaptive 实现类
private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

也就是说具体用哪个 protocol实现类是看Url中protocol是什么配置

在export的过程中会通过修改Url参数的protocol属性来实现动态适应加载实现类

另外 invoker 也有 getUrl 方法,protocol.export(invoker)的时候invoker中的url中的protocol属性会影响最终的实现类,详情可以研究下 SPI

@Adaptive带有这个注解的方法必须参数里有 URL 或者参数有getUrl方法,否则会报错,该注解会根据url中对应key的参数确定具体加载哪个扩展

比如:org.apache.dubbo.config.ServiceConfig#exportLocal

private void exportLocal(URL url) {
    if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
        URL local = URL.valueOf(url.toFullString())
                // 这里修改了 protocol
                .setProtocol(Constants.LOCAL_PROTOCOL) 
                .setHost(LOCALHOST)
                .setPort(0);
        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(url.getServiceKey(), getServiceClass(ref));
        // 这里就是动态适配的扩展类
        Exporter<?> exporter = protocol.export(
                proxyFactory.getInvoker(ref, (Class) interfaceClass, local)); 
        exporters.add(exporter);
        logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题