dubbo-代理类的生成过程

在这里插入图片描述

首先看proxyFactory的生成过程

    private static final ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();

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 interface!");
        }
        if(!withExtensionAnnotation(type)) {
            throw new IllegalArgumentException("Extension type(" + type + 
                    ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
        }
        
        ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        if (loader == null) {
            EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
            loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        }
        return loader;
    }

在调用getAdaptiveExtension方法:

    @SuppressWarnings("unchecked")
    public T getAdaptiveExtension() {
        Object instance = cachedAdaptiveInstance.get();
        if (instance == null) {
            if(createAdaptiveInstanceError == null) {
                synchronized (cachedAdaptiveInstance) {
                    instance = cachedAdaptiveInstance.get();
                    if (instance == null) {
                        try {
                            instance = createAdaptiveExtension();
                            cachedAdaptiveInstance.set(instance);
                        } catch (Throwable t) {
                            createAdaptiveInstanceError = t;
                            throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t);
                        }
                    }
                }
            }
            else {
                throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
            }
        }

        return (T) instance;
    }

createAdaptiveExtension方法:

    
    @SuppressWarnings("unchecked")
    private T createAdaptiveExtension() {
        try {
            return injectExtension((T) getAdaptiveExtensionClass().newInstance());
        } catch (Exception e) {
            throw new IllegalStateException("Can not create adaptive extenstion " + type + ", cause: " + e.getMessage(), e);
        }
  
dubbo-py 是一个 Python 实现的 Dubbo 协议的客户端,它提供了简单易用的 API 以方便开发者在 Python 项目中使用 Dubbodubbo-py 的源代码托管在 GitHub 上,可以方便地查看和下载。 dubbo-py 的源码结构清晰,主要分为 dubbo_client 和 dubbo_codec 两个模块,其中 dubbo_client 模块实现了 Dubbo 协议的客户端,dubbo_codec 模块实现了 Dubbo 协议的序列化和反序列化操作。 在 dubbo_client 模块中,主要实现了 DubboClient 类和 DubboService 类。DubboClient 类封装了 Dubbo 协议的请求响应过程,提供 give_me_a_stub() 方法用于生成 DubboService 代理类DubboService 类则是 Dubbo 服务实现类的代理类,使用时只需实例化 DubboService 类并调用其中的方法即可。DubboClient 和 DubboService 类的代码结构清晰,易于理解和调试。 在 dubbo_codec 模块中,主要实现了 Dubbo 协议的序列化和反序列化操作。DubboCodec 类实现了将 Python 对象转化成 Dubbo 协议要求的二进制数据,并提供了将 Dubbo 协议二进制数据转化成 Python 对象的方法。DubboCodec 类的实现较为复杂,但是提供了丰富的功能和对 Dubbo 协议的完整支持。 综上所述,dubbo-py 的源码结构清晰,实现简单易用,对 Dubbo 协议的支持非常完整。在 Python 项目中使用 Dubbo 的开发者可以通过查看 dubbo-py 的源码,学习 Dubbo 客户端的实现方法和 Dubbo 协议的序列化和反序列化操作,提高自己的技术水平。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值