七Dubbo各模块的层次核心接口及组件类--2Exchange--Remoting模块

七Dubbo各模块的层次核心接口及组件类--2Exchange--Remoting模块

7.2 Exchange(remoting模块)

信息交换层:封装请求/响应模式,同步转异步,以request、response为中心,Exchanger 、ExchangeChannel ExchangeClient ExchangeServer为拓展接口。

模块 Dubbo层 各层核心接口
Service ServiceBean ReferanceBean
dubbo-config Config ServiceConfig ReferanceConfig
dubbo-RPC Proxy ServiceProxy ProxyFactory
dubbo-registry Registry RegistryFactory Registry RegistryService
dubbo-cluster Cluster Cluster Directory Router LoadBalance
dubbo-monitor Monitor
dubbo-RPC Protocol Filter Invoke Prococol Exporter
dubbo-remoting Exchange Exchanger ExchangeChannel ExchangeClient ExchangeServer
Transport Transport Channel Client Server Codec
dubbo-common Serialize

7.2.1 Exchangers

区别Exchanger与Exchangers

1 同样提供了bind和connect方法,但是Exchanger是Dubbo体系层级结构中的,具有继承结构关系的类,定义了SPI服务接口(包括默认adaptive参数的设置),以及定义了服务接口中的具体方法;但是Exchangers仅仅是Exchanger的门面类,代替后者做了具体的方法的实现,以及获取SPI对象(如getExtensionLoader)。

2 Exchangers的引入,作为一个代理层的作用,把原本需要调用Exchanger的bind和connect方法的,代理到其上,通过spi的getExtensionLoader,来调用对应的实现

image-20220830004634257

//Exchanger定义了SPI的接口,以及方法上的自适应参数设置,以用来在URL中针对相应的参数,做对应服务实现类的选择
@SPI(HeaderExchanger.NAME)
public interface Exchanger {

    /**
     * bind.
     *
     * @param url
     * @param handler
     * @return message server
     */
    @Adaptive({Constants.EXCHANGER_KEY})
    ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException;

    /**
     * connect.
     *
     * @param url
     * @param handler
     * @return message channel
     */
    @Adaptive({Constants.EXCHANGER_KEY})
    ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException;

}

Exchangers为Exchanger层的facade门面,也就是remoting模块的集成的API,通过该类及其提供的接口方法bind、connect,创建客户端ExchangeClient,自动调用remoting层内Exchange和transport的通信底层构建,进行通信。

提供static的方法——bind和connect。

bind绑定,获取服务端ExchangeServer;connect,创建客户端ExchangeClient。

public class Exchangers {

    static {
        // check duplicate jar package
        Version.checkDuplicate(Exchangers.class);
    }

    private Exchangers() {
    }

  

    public static ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException {
        if (url == null) {
            throw new IllegalArgumentException("url == null");
        }
        if (handler == null) {
            throw new IllegalArgumentException("handler == null");
        }
        url = url.addParameterIfAbsent(Constants.CODEC_KEY, "exchange");
        return getExchanger(url).bind(url, handler);
    }


    public static ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
        if (url == null) {
            throw new IllegalArgumentException("url == null");
        }
        if (handler == null) {
            throw new IllegalArgumentException("handler == null");
        }
        url = url.addParameterIfAbsent(Constants.CODEC_KEY, "exchange");
        return getExchanger(url).connect(url, handler);
    }

     //这里是通过dubbo的spi机制,动态的获取Exchanger的实现类,HeaderExchanger,调用connect、bind执行具体方法。
    public static Exchanger getExchanger(URL url) {
        String type = url.getParameter(Constants.EXCHANGER_KEY, Constants.DEFAULT_EXCHANGER);
        return getExchanger(type);
    }

    public static Exchanger getExchanger(String type) {
        return ExtensionLoader.getExtensionLoader(Exchanger.class).getExtension(type);
    }

}

Exchanger的默认实现(spi服务)

public class HeaderExchanger implements Exchanger {

    public static final String NAME = "header";

     //connect建立客户端
    // 这里包含了多个调用,分别如下:
    // 1. 创建 HeaderExchangeHandler 对象
    // 2. 创建 DecodeHandler 对象
    // 3. 通过 Transporters 构建 Client 实例
    // 4. 创建 HeaderExchangeClient 对象
    public ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
        return new HeaderExchangeClient(Transporters.connect(url, new DecodeHandler(new HeaderExchangeHandler(handler))));
    }
	//bind建立服务端
    public ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException {
        return new HeaderExchangeServer(Transporters.bind(url, new DecodeHandler(new HeaderExchangeHandler(handler))));
    }

}

7.2.2 ExchangeChannel

image-20221011155857232

7.2.3 ExchangeClient

image-20221011155731852

ExchangeClient实际上并不具备通信能力,它需要基于更底层的客户端实例进行通信。比如NettyClient、MinaClient等,默认情况下,Dubbo使用NettyClient进行通信。

7.2.4 ExchangeServer

posted @ 2023-03-13 14:53  LeasonXue  阅读(56)  评论(0编辑  收藏  举报