RocketMQ - NameSrv

Introduction to Namesrv Features

NameServer is responsible for maintaining the configuration information and status information of Producer and Consumer, and coordinating the cooperative execution of various roles. Through the roles of NameServer, you can understand the overall information of the cluster, and they will report the status to NameServer regularly

NamesrvStartup startup configuration

// NamesrvConfig 默认配置
// private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));

// private String kvConfigPath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "kvConfig.json";
    
// private String configStorePath = System.getProperty("user.home") + File.separator + "namesrv" + File.separator + "namesrv.properties";
    
// private String productEnvName = "center";
// private boolean clusterTest = false;
// private boolean orderMessageEnable = false;
final NamesrvConfig namesrvConfig = new NamesrvConfig();

// NettyServerConfig 默认配置
// private int listenPort = 8888;
// private int serverWorkerThreads = 8;
// private int serverCallbackExecutorThreads = 0;
// private int serverSelectorThreads = 3;
// private int serverOnewaySemaphoreValue = 256;
// private int serverAsyncSemaphoreValue = 64;
// private int serverChannelMaxIdleTimeSeconds = 120;

// private int serverSocketSndBufSize = NettySystemConfig.socketSndbufSize;
// private int serverSocketRcvBufSize = NettySystemConfig.socketRcvbufSize;
// private boolean serverPooledByteBufAllocatorEnable = true;
final NettyServerConfig nettyServerConfig = new NettyServerConfig();
nettyServerConfig.setListenPort(9876);

Namesrv routing function

https://www.jianshu.com/p/5161c16a0a29
namesrv supports cluster mode, but each namesrv is independent of each other without any communication . Its multi-point disaster recovery is obtained by polling for information when the producer/consumer accesses namesrv (If the current node fails to access, move to the next one)

As the registration center, namesrv is responsible for receiving regular registration information of brokers and maintaining them in memory. Namesrv has no persistence function. All data is stored in memory. The registration process of brokers also loops through all namesrv to register.

Namesrv routing information management class

org.apache.rocketmq.namesrv.routeinfo。RouteInfoManager

    private final static long BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    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;

Cluster information: cluster -> broker -> topic -> queue
Active Broker information: brokerLiveTable
Message filtering server: filterServerTable

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

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

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

Route registration

When the Broker starts, it sends heartbeat statements to all NameServers in the cluster, and sends heartbeat packets to all NameServers in the cluster every 30s. NameServer
updates the lastUpdateTimestamp of BrokerLiveInfo in the brokerLiveTable cache after receiving the Broker heartbeat packets,
and maintains cluster -> broker -> topic -> queue relationship
brokerLiveTable 使用读写锁的方式操作

Route deletion

  1. NameServer scans the brokerLiveTable every 10s. If no heartbeat packet is received for 120s, NameServer will remove the routing information of the Broker and close the Socket connection.
  2. Broker goes offline normally, execute the unregisterBroker instruction

Route discovery

RocketMQ routing discovery is non-real-time. When Topic routing changes, NameServer does not actively push to the client, but the client regularly pulls the latest route of the topic

Guess you like

Origin blog.csdn.net/lewee0215/article/details/111936932