七Dubbo各模块的层次核心接口及组件类--1Transport--Remoting模块
七Dubbo各模块的层次核心接口及组件类--1Transport--Remoting模块
模块 | 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 Invoker Prococol Exporter |
dubbo-remoting | Exchange | Exchanger ExchangeChannel ExchangeClient ExchangeServer |
: | Transport | Transport Channel Client Server Codec |
dubbo-common | Serialize |
注意:
1 在Dubbo-RPC中,protocol是核心层,只要有protocol+invoker+exporter,就可以完成非透明的RPC调用;
2 在Dubbo-RPC中,proxy层封装了所有接口的透明化代理(就是通过proxyFactory.getInvoker(proxy,type,url)获取provider服务的invoker类),其它层都以invoker为中心。当要暴漏给consumer使用时,proxyfactory.getProxy(Invoker
3 Remoting模块,是dubbo协议的实现;如果用例如RMI协议,那么remoting模块就不再使用。Transport层负责消息单向传输,对Netty等实际实现的abstract抽象(具体传输方法逻辑,由子类Netty等实现);Exchange层是在Transprot层基础上封装了Request-Response的语义。
在Dubbo的源码分析过程中,注意init初始化和call调用过程的区分。
7.1 Transport(Remoting模块)
模块 | 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 |
transport层(网络传输层),负责数据传输的实现,Transport是netty等网络传输的接口;此外,transport层,除了传输能力,还有数据接收之后的处理,就是Dispatcher(分派任务器)和ThreadPool(处理任务的线程池)。
该层的分析,同Exchange层的结构思路相通。
7.1.1 Transport
区分transporter和transporters:
同样提供了bind和connect方法,但是Transporter是Dubbo体系层级结构中的,具有继承结构关系的类,定义了SPI服务接口(包括默认adaptive参数的设置),以及定义了服务接口中的具体方法;但是Transporters仅仅是Transporter的门面类,代替后者做了具体的方法的实现,以及获取SPI对象的实现(如getExtensionLoader)——getTransproter()
默认实现是NettyTransporter。
@SPI("netty")
public interface Transporter {
/**
* Bind a server.
*
* @param url server url
* @param handler
* @return server
* @throws RemotingException
* @see com.alibaba.dubbo.remoting.Transporters#bind(URL, Receiver, ChannelHandler)
*/
@Adaptive({Constants.SERVER_KEY, Constants.TRANSPORTER_KEY})
Server bind(URL url, ChannelHandler handler) throws RemotingException;
/**
* Connect to a server.
*
* @param url server url
* @param handler
* @return client
* @throws RemotingException
* @see com.alibaba.dubbo.remoting.Transporters#connect(URL, Receiver, ChannelListener)
*/
@Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY})
Client connect(URL url, ChannelHandler handler) throws RemotingException;
}
Transports为Transport的门面类,提供bind和connect方法,作为接口。
public class Transporters {
static {
// check duplicate jar package
Version.checkDuplicate(Transporters.class);
Version.checkDuplicate(RemotingException.class);
}
private Transporters() {
}
public static Server bind(URL url, ChannelHandler... handlers) throws RemotingException {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
if (handlers == null || handlers.length == 0) {
throw new IllegalArgumentException("handlers == null");
}
ChannelHandler handler;
if (handlers.length == 1) {
handler = handlers[0];
} else {
handler = new ChannelHandlerDispatcher(handlers);
}
return getTransporter().bind(url, handler);
}
public static Client connect(URL url, ChannelHandler... handlers) throws RemotingException {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
ChannelHandler handler;
if (handlers == null || handlers.length == 0) {
handler = new ChannelHandlerAdapter();
} else if (handlers.length == 1) {
handler = handlers[0];
} else {
handler = new ChannelHandlerDispatcher(handlers);
}
return getTransporter().connect(url, handler);
}
//SPI服务,获取spi的动态扩展实现类
public static Transporter getTransporter() {
return ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
}
}
调用Transporter的connect和bind方法,最终通过Transporters类,获取spi服务实现类,代理到默认实现NettyTransporter的相应方法。
public class NettyTransporter implements Transporter {
public static final String NAME = "netty";
public Server bind(URL url, ChannelHandler listener) throws RemotingException {
return new NettyServer(url, listener);
}
//此处创建nettyClient的客户端
public Client connect(URL url, ChannelHandler listener) throws RemotingException {
return new NettyClient(url, listener);
}
}