Dubbo源码解析(五)-探索Dubbo中的SPI之Adaptive

目录

1、前文

2、Dubbo SP

第一步 首先获取对应的ExtensionLoader对象

二 代码中缓存对象

2.1 private final Holder>> cachedClasses = new Holder<>();

2.2  cachedAdaptiveClass

2.3 cachedAdaptiveInstance 缓存扩展类的instance 

3 ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()过程

3.1、ExtensionLoader loader 中只有一个type属性对应

3.2、调用getAdaptiveExtension方法:

获取到ObjectFactory后返回对应的ExtensionLoader

四 第二步 首先通过getAdaptiveExtension回去自适应对象

五 ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);做了什么?


 


1、前文

在前面四章进行了了解dubbo中的默认服务发现、调用等逻辑后,本章对Dubbo中使用的SPI技术进行了解。

SPI全称为Service Provider Interface,Java SPI可以自行了解,本文不做叙述。

2、Dubbo SP

案例一:ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

首先查看getExtensionLoader方法做了什么:

第一步 首先获取对应的ExtensionLoader对象

    @SuppressWarnings("unchecked")
    public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
        if (type == null) { // 判空
            throw new IllegalArgumentException("Extension type == null");
        }
        if (!type.isInterface()) { // 判断是否为接口
            throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!");
        }
        if (!withExtensionAnnotation(type)) {// 判断接口是否使用了@SPI注解
            throw new IllegalArgumentException("Extension type (" + type +
                    ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!");
        }
        // 初次加载为null  需要进行new
        ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        if (loader == null) {
            EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type)); //根据type生成指定的ExtensionLoader
            loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        }
        return loader;
    }

首先是对其进行一些列的参数校验等,然后通过

    private ExtensionLoader(Class<?> type) {
        this.type = type;
        // 首先生成指定的ExtensionFactory的自适应扩展类   Adaptive根据参数动态配置
        objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
    }

此处type为ExtensionFactory的时候直接用null否则通过ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension() 生成对应的

ExtensionFactory

二 代码中缓存对象

@Adaptive @Spi等注解来表示对应是自适应扩展类,或是spi等

实际就是根据不同文件路径的对象进行缓存响应的class信息等

private static final String SERVICES_DIRECTORY = "META-INF/services/";

private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";

private static final String DUBBO_INTERNAL_DIRECTORY = DUBBO_DIRECTORY + "internal/";

代码中进行各种缓存的存入,如封装类的缓存、实例缓存等

    private Map<String, Class<?>> loadExtensionClasses() {
        cacheDefaultExtensionName();

        Map<String, Class<?>> extensionClasses = new HashMap<>();
        loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName());
        loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName().replace("org.apache", "com
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值