4、RocketMQ源码-Broker启动向NameServer注册、发送心跳

匿名 (未验证) 提交于 2019-12-03 00:02:01

1、Broker启动向NameServer注册

在BrokerStartup类的main方法运行的时候,创建了BrokerController,然后调用了BrokerController的start方法,在该方法中有如下代码

 // 启动的时候向每个NameServer发起注册 this.registerBrokerAll(true, false, true);   this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {             @Override             public void run() {                 try {                     // Broker会每隔30s向NameSrv注册并更新自身topic信息                     BrokerController.this.registerBrokerAll(true, false, brokerConfig.isForceRegister());                 } catch (Throwable e) {                     log.error("registerBrokerAll Exception", e);                 }             }         }, 1000 * 10, Math.max(10000, Math.min(brokerConfig.getRegisterNameServerPeriod(), 60000)), TimeUnit.MILLISECONDS);

首先在Broker启动的时候就会向每个NameServer进行注册。

 // true false true     public synchronized void registerBrokerAll(final boolean checkOrderConfig, boolean oneway, boolean forceRegister) {         // ConcurrentMap<String, TopicConfig> topicConfigTable         // 将topicConfigTable封装到TopicConfigSerializeWrapper中         TopicConfigSerializeWrapper topicConfigWrapper = this.getTopicConfigManager().buildTopicConfigSerializeWrapper();          // 判断权限如果为不可读或不可写那么久在拼装一下topicConfigTable到TopicConfigSerializeWrapper中         if (!PermName.isWriteable(this.getBrokerConfig().getBrokerPermission())             || !PermName.isReadable(this.getBrokerConfig().getBrokerPermission())) {             ConcurrentHashMap<String, TopicConfig> topicConfigTable = new ConcurrentHashMap<String, TopicConfig>();             for (TopicConfig topicConfig : topicConfigWrapper.getTopicConfigTable().values()) {                 TopicConfig tmp =                     new TopicConfig(topicConfig.getTopicName(), topicConfig.getReadQueueNums(), topicConfig.getWriteQueueNums(),                         this.brokerConfig.getBrokerPermission());                 topicConfigTable.put(topicConfig.getTopicName(), tmp);             }             topicConfigWrapper.setTopicConfigTable(topicConfigTable);         }          // forceRegister是否强制注册         // needRegister方法是与配置的每个NameServer进行通信,判断topicConfigTable是否改变了,只要其中一个改变了那么久需要发起注册         // QUERY_DATA_VERSION = 322;         if (forceRegister || needRegister(this.brokerConfig.getBrokerClusterName(),                 this.getBrokerAddr(),                 this.brokerConfig.getBrokerName(),                 this.brokerConfig.getBrokerId(),                 this.brokerConfig.getRegisterBrokerTimeoutMills())) {             // REGISTER_BROKER = 103;             doRegisterBrokerAll(checkOrderConfig, oneway, topicConfigWrapper);         }     }     private void doRegisterBrokerAll(boolean checkOrderConfig, boolean oneway,         TopicConfigSerializeWrapper topicConfigWrapper) {         List<RegisterBrokerResult> registerBrokerResultList = this.brokerOuterAPI.registerBrokerAll(             this.brokerConfig.getBrokerClusterName(),             this.getBrokerAddr(),             this.brokerConfig.getBrokerName(),             this.brokerConfig.getBrokerId(),             this.getHAServerAddr(),             topicConfigWrapper,             this.filterServerManager.buildNewFilterServerList(),             oneway,             this.brokerConfig.getRegisterBrokerTimeoutMills(),             this.brokerConfig.isCompressedRegister());          if (registerBrokerResultList.size() > 0) {             RegisterBrokerResult registerBrokerResult = registerBrokerResultList.get(0);             if (registerBrokerResult != null) {                 // 根据updateMasterHAServerAddrPeriodically标注位(在初始化时若Broker的配置文件中没有haMasterAddress参数配置,则标记为true,表示注册之后需要更新主用Broker地址)                 // 以及NameServer返回的HaServerAddr地址是否为空,若标记位是true且返回的HaServerAddr不为空,则用HaServerAddr地址更新HAService.HAClient.masterAddress的值;                 // 该HAClient.masterAddress值用于主备Broker之间的commitlog数据同步之用                 if (this.updateMasterHAServerAddrPeriodically && registerBrokerResult.getHaServerAddr() != null) {                     this.messageStore.updateHaMasterAddress(registerBrokerResult.getHaServerAddr());                 }                  // 用NameServer返回的MasterAddr值更新SlaveSynchronize.masterAddr值,用于主备Broker同步Config文件使用;                 this.slaveSynchronize.setMasterAddr(registerBrokerResult.getMasterAddr());                  if (checkOrderConfig) {                     this.getTopicConfigManager().updateOrderTopicConfig(registerBrokerResult.getKvTable());                 }             }         }     }

在NameServer中DefaultRequestProcessor类负责处理请求,在processRequest中根据不同的请求code调用不同的方法。

2、发送心跳

在BrokeController的初始化方法initialize中初始化了心跳管理线程池heartbeatExecutor;然后在registerProcessor对线程池任务进行了填充。

 // 心跳管理线程池 this.heartbeatExecutor = new BrokerFixedThreadPoolExecutor(                 this.brokerConfig.getHeartbeatThreadPoolNums(),                 this.brokerConfig.getHeartbeatThreadPoolNums(),                 1000 * 60,                 TimeUnit.MILLISECONDS,                 this.heartbeatThreadPoolQueue,                 new ThreadFactoryImpl("HeartbeatThread_", true));   this.registerProcessor();
 public void registerProcessor() {      ......     this.remotingServer.registerProcessor(RequestCode.HEART_BEAT, clientProcessor, this.heartbeatExecutor);     this.fastRemotingServer.registerProcessor(RequestCode.HEART_BEAT, clientProcessor, this.heartbeatExecutor);     ...... }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!