Sender 线程在发送消息的的sendProducerData()方法中,会对Kafka的的每个node 进行检测是否可以发送消息,将没有就绪的node节点移除,这个时候就会调用NetworkClient的ready方法对指定的node 进行检测。
1.流程图:
2.源码分析:
1.检测node的连接状态是否就绪
可用则为true,不可用则返回false,并会初始化一个连接
/**
* Begin connecting to the given node, return true if we are already connected and ready to send to that node.
*
* 检测指定node是否可以发送请求
* 如果可以发送,那么就返回true,否则就初始化一个连接,
* @param node The node to check
* @param now The current timestamp
* @return True if we are ready to send to the given node
*/
@Override
public boolean ready(Node node, long now) {
if (node.isEmpty())
throw new IllegalArgumentException("Cannot connect to empty node " + node);
//是否已经准备好发送请求 [2]
if (isReady(node, now))
return true;
// 检测该节点现在是否能够连接
if (connectionStates.canConnect(node.idString(), now))
// if we are interested in sending to a node and we don't have a connection to it, initiate one
//初始化一个连接
initiateConnect(node, now)