5-RocketMQ-NameServer

LZC發表於2021-07-07

NameServer路由實現類:org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager,在瞭解路由註冊之前,我們首先看一下NameServer到底儲存哪些資訊。

private final HashMap<String/* topic */, List<QueueData>> topicQueueTable;
private final HashMap<String/* brokerName */, BrokerData> brokerAddrTable;
private final HashMap<String/* clusterName */, Set<String/* brokerName */>> clusterAddrTable;
private final HashMap<String/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
private final HashMap<String/* brokerAddr */, List<String>/* Filter Server */> filterServerTable;

topicQueueTable:Topic訊息佇列路由資訊,訊息傳送時根據路由表進行負載均衡。

brokerAddrTable:Broker基礎資訊,包含brokerName、所屬叢集名稱、主備Broker地址。

brokerLiveTable:Broker狀態資訊。NameServer每次收到心跳包時會替換該資訊。

filterServerTable:Broker上的FilterServer列表,用於類模式訊息過濾,

public class QueueData implements Comparable<QueueData> {
    private String brokerName;
    private int readQueueNums;
    private int writeQueueNums;
    private int perm;
    private int topicSynFlag;
}

public class BrokerData implements Comparable<BrokerData> {
    private String cluster;
    private String brokerName;
    private HashMap<Long/* brokerId */, String/* broker address */> brokerAddrs;
}

class BrokerLiveInfo {
    // 上次收到Broker心跳包的時間
    private long lastUpdateTimestamp; 
    private DataVersion dataVersion;
    private Channel channel;
    private String haServerAddr;
}

多個Broker組成一個叢集,BrokerName由相同的多臺Broker組成Master-Slave架構,brokerId為0代表Master,大於0表示Slave。

RocketMQ路由註冊是透過Broker與NameServer的心跳功能實現的。Broker啟動時向叢集中所有的NameServer傳送心跳語句,每隔30s向叢集中所有NameServer傳送心跳包,NameServer收到Broker心跳包時會更新brokerLiveTable快取中BrokerLiveInfo的lastUpdate Timestamp,然後NameServer每隔10s掃描brokerLiveTable,如果連續120s沒有收到心跳包,NameServer將移除該Broker的路由資訊同時關閉Socket連線。

本作品採用《CC 協議》,轉載必須註明作者和本文連結