Dubbo线程模型Dispatcher策略

Dubbo的文档中写到:

all 所有消息都派发到线程池,包括请求,响应,连接事件,断开事件,心跳等。
direct 所有消息都不派发到线程池,全部在 IO 线程上直接执行。
message 只有请求响应消息派发到线程池,其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
execution 只有请求消息派发到线程池,不含响应,响应和其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
connection 在 IO 线程上,将连接断开事件放入队列,有序逐个执行,其它消息派发到线程池。

看了这部分对应的源代码
org.apache.dubbo.remoting.transport.dispatcher.all.AllChannelHandler
/**

 * on channel connected.
 *
 * @param channel channel.
 */
void connected(Channel channel) throws RemotingException;

/**
 * on channel disconnected.
 *
 * @param channel channel.
 */
void disconnected(Channel channel) throws RemotingException;

/**
 * on message sent.
 *
 * @param channel channel.
 * @param message message.
 */
void sent(Channel channel, Object message) throws RemotingException;

/**
 * on message received.
 *
 * @param channel channel.
 * @param message message.
 */
void received(Channel channel, Object message) throws RemotingException;

/**
 * on exception caught.
 *
 * @param channel   channel.
 * @param exception exception.
 */
void caught(Channel channel, Throwable exception) throws RemotingException;

上面的方法有对应的建立连接,断开连接等的对应处理,但是哪一个是心跳的消息是哪个呢?

阅读 2.2k
2 个回答

2.6.2版本下:

心跳也是消息,也对应 sent/received 两个方法,对应的处理在com.alibaba.dubbo.remoting.exchange.support.header.HeartbeatHandler,入口在com.alibaba.dubbo.remoting.transport.dispatcher.ChannelHandlers

dubbo 这个模型算是 chain/delegate 的方式,每一个handler 处理一个逻辑,heartbeat 也是一个 handler

基于 3.2.0-beta.4-SNAPSHOT 版本
心跳处理在 org.apache.dubbo.remoting.exchange.support.header.HeartbeatHandler

心跳发起在org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeClient

另心跳是不参与Dispatcher模型的,都是在io线程中执行

debug 一下源码 HeartbeatHandler ,发现这个handler中delegate的handler才是dispatcher线程模型,所以心跳是在dispatcher之前就已经被处理了,也就是io线程

org.apache.dubbo.remoting.exchange.support.header.HeartbeatTimerTask#doTask 方法可知心跳是封装在Request中发送的,通过正常的request请求发出的,相应的接受消息也是received接收的,也就是这两个方法:

public void sent(Channel channel, Object message) throws RemotingException {
    handler.sent(channel, message);
}

public void received(Channel channel, Object message) throws RemotingException {
    handler.received(channel, message);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题