Codec
CodecSupport
AbstractCodec
TransportCodec
CodecAdapter
Dispacher
ChannelHandlers
Dispatcher
AllDispatcher
AllChannelHandler
ChannelEventRunnable
WrappedChannelHandler
ChannelHandler
ChannelHandlerAdapter
ChannelHandlerDispatcher
ChannelHandlerDelegate
AbstractChannelHandlerDelegate
DecodeHandler
MultiMessageHandler
Channel
AbstractChannel
ChannelDelegate
Server
AbstractServer
ServerDelegate
Client
AbstractClient
ClientDelegate
AbstractPeer
AbstractEndpint
Dubbo与Netty
client:
ReferenceConfig
referenceConfig.init(createProxy()); init方法里面调用createProxy()方法返回客户端需要的接口;
在createProxy方法里面会调用协议的refprotocol.refer()方法返回invoker;接着在调用proxyFactory.getProxy(invoker)把invoker转换为客户端需要的接口;
Protocol
在refprotocol.refer()(一般默认DubboProtocol)里面会新建一个DubboInvoker;DubboInvoker的构造方法会调用getClients()方法返回ExchangeClient[]数组;
getClients方法里面会调用getSharedClient()返回ExchangeClient;
在getSharedClient()方法里面会调用initClient()方法,初始化ExchangeClient;
在initClient()方法里面调用Exchangers.connect(url, requestHandler)的静态方法;
Exchangers
在Exchangers.connect()里面会调用getExchanger()方法,返回Exchanger(根据SPI进行拓展);
返回Exchanger,在调用Exchanger的Exchanger.connect()方法;Exchanger真正的实现类根据SPI默认为HeaderExchanger;
HeaderExchanger
在HeaderExchanger.connect的方法里面会新建一个HeaderExchangeClient();
HeaderExchangeClient
HeaderExchangeClient的构造方法会调用Transporters.connect()方法返回Client;
Transporters
根据SPI进行拓展,默认为NettyTransporter,然后Transporters.connect()方法会调用NettyTransporter的connect方法;
NettyTransporter
NettyTransporter的connect方法会新建一个NettyClient,NettyClient的构造方法中会调用父类AbstractClient的wrapChannelHandler()方法返回ChannelHandler;
其次还会调用AbstractClient的构造方法;
AbstractClient
AbstractClient的构造方法中会doOpen()方法,调用netty的底层代码,初始化netty的bootstrap;
然后再调用connect连接
其次在AbstractClient.wrapChannelHandler()方法中会调用ChannelHandlers.wrap()方法返回ChannelHandler;
ChannelHandlers
ChannelHandlers.wrap()方法里面再调用wrapInternal方法,wrapInternal方法再构造出新的MultiMessageHandler;
MultiMessageHandler
MultiMessageHandler调用父类AbstractChannelHandlerDelegate的构造方法;
AbstractChannelHandlerDelegate实现ChannelHandlerDelegate,ChannelHandlerDelegate继承ChannelHandler;
在MultiMessageHandler的构造方法里会再构建出一个HeartbeatHandler;HeartbeatHandler继承AbstractChannelHandlerDelegate;
在HeartbeatHandler里面根据SPI加载出Dispatcher,默认AllDispatcher:ExtensionLoader.getExtensionLoader(Dispatcher.class).getAdaptiveExtension().dispatch(handler, url)
AllDispatcher --> 线程池模型
AllDispatcher里面会新建一个AllChannelHandler,AllChannelHandler里面派生出线程池
NettyClient继承于AbstractClient,AbstractClient继承于AbstractEndpoint,AbstractEndpoint继承于AbstractPeer;
AbstractPeer里面会有一个私有属性private final ChannelHandler handler;这个handler就是构建NettyClient的时候作为构建参数传入进来的;
AbstractClient的构造方法中,会调用doOpen方法,doOpen方法里面会新建一个NettyHandler,NettyHandler继承于SimpleChannelHandler;
NettyHandler会用来处理请求,比如writeRequested方法,会先调用父类SimpleChannelHandler的writeRequested在netty进行处理,然后再调用构建NettyClient的时候注入的ChannelHandler;
ChannelHandler由于ChannelHandler被装饰了很多层:
DecodeHandler decodeHandler = new DecodeHandler(new HeaderExchangeHandler(handler))
new MultiMessageHandler(new HeartbeatHandler(ExtensionLoader.getExtensionLoader(Dispatcher.class)
.getAdaptiveExtension().dispatch(decodeHandler, url))
server:
ServiceConfig
ServiceConfig.export(); export方法里面调用doExport()方法对服务进行暴露;
在doExport方法继续调用doExportUrls,对协议进行遍历调用doExportUrlsFor1Protocol对服务进行暴露;
doExportUrlsFor1Protocol()方法里面调用proxyFactory.getInvoker对服务进行转换为invoke,转换为invoke之后再调用protocol.export(wrapperInvoker)把invoke转换为export;
Protocol
protocol.export里面会新建一个DubboInvoker;DubboInvoker的构造方法会调用serviceKey(url)方法返回一个服务key;
在protocol.export方法中再然后调用openServer()方法;
openServer方法里面再调用createServer(url)方法;
createServer方法里面调用Exchangers.bind(url, requestHandler)返回ExchangeServer;
getClients方法里面会调用getSharedClient()返回ExchangeClient;
在getSharedClient()方法里面会调用initClient()方法,初始化ExchangeClient;
在initClient()方法里面调用Exchangers.connect(url, requestHandler)的静态方法;
Exchangers
在Exchangers.connect()里面会调用getExchanger()方法,返回Exchanger(根据SPI进行拓展);
返回Exchanger,在调用Exchanger的Exchanger.bind()方法;Exchanger真正的实现类根据SPI默认为HeaderExchanger;
HeaderExchanger
在HeaderExchanger.connect的方法里面会新建一个HeaderExchangeServer();
HeaderExchangeServer
HeaderExchangeServer的构造方法会调用Transporters.bind()方法返回Server;
Transporters
根据SPI进行拓展,默认为NettyTransporter,然后Transporters.bind()方法会调用NettyTransporter的bind方法;
NettyTransporter
NettyTransporter的connect方法会新建一个NettyServer,NettyServer的构造方法中会调用父类AbstractServer的wrapChannelHandler()方法返回ChannelHandler;
其次还会调用AbstractServer的构造方法;
AbstractServer
AbstractClient的构造方法中会doOpen()方法,调用netty的底层代码,初始化netty的bootstrap;
TODO
ChannelHandlers
ChannelHandlers.wrap()方法里面再调用wrapInternal方法,wrapInternal方法再构造出新的MultiMessageHandler;
MultiMessageHandler
MultiMessageHandler调用父类AbstractChannelHandlerDelegate的构造方法;
AbstractChannelHandlerDelegate实现ChannelHandlerDelegate,ChannelHandlerDelegate继承ChannelHandler;
在MultiMessageHandler的构造方法里会再构建出一个HeartbeatHandler;HeartbeatHandler继承AbstractChannelHandlerDelegate;
在HeartbeatHandler里面根据SPI加载出Dispatcher,默认AllDispatcher:ExtensionLoader.getExtensionLoader(Dispatcher.class).getAdaptiveExtension().dispatch(handler, url)
AllDispatcher --> 线程池模型
AllDispatcher里面会新建一个AllChannelHandler,AllChannelHandler里面派生出线程池
NettyClient继承于AbstractClient,AbstractClient继承于AbstractEndpoint,AbstractEndpoint继承于AbstractPeer;
AbstractPeer里面会有一个私有属性private final ChannelHandler handler;这个handler就是构建NettyClient的时候作为构建参数传入进来的;
AbstractClient的构造方法中,会调用doOpen方法,doOpen方法里面会新建一个NettyHandler,NettyHandler继承于SimpleChannelHandler;
NettyHandler会用来处理请求,比如writeRequested方法,会先调用父类SimpleChannelHandler的writeRequested在netty进行处理,然后再调用构建NettyClient的时候注入的ChannelHandler;
ChannelHandler由于ChannelHandler被装饰了很多层:
DecodeHandler decodeHandler = new DecodeHandler(new HeaderExchangeHandler(handler))
new MultiMessageHandler(new HeartbeatHandler(ExtensionLoader.getExtensionLoader(Dispatcher.class)
.getAdaptiveExtension().dispatch(decodeHandler, url))