ent 中的 namesrv 地址列表是怎么来的呢,有两种方式:
- 通过命令行或配置文件在启动的时候获得的
- 通过 Scheduled task,按一定的频率从一个 web 服务 fetch的(web服务可以自建),如果有变更,就更新这个 namesrv 地址列表。
client 选择 namesrv的过程如下, index递增取模,然并不是每次都这么干,取到后会缓存起来。
if (addrList != null && !addrList.isEmpty()) {
for (int i = 0; i < addrList.size(); i++) {
int index = this.namesrvIndex.incrementAndGet();
index = Math.abs(index);
index = index % addrList.size();
String newAddr = addrList.get(index);
this.namesrvAddrChoosed.set(newAddr);
Channel channelNew = this.createChannel(newAddr);
if (channelNew != null)
return channelNew;
}
}
看到这里我产生了疑问,那岂不是每个 client 启动的时候都取的是第一个 namesrv,它不会压力很大吗,后来发现 namesrvIndex 的初始值是随机的。
以上所有扯淡都是基于源码 https://github.com/apache/incubator-rocketmq (tag:rocketmq-all-4.1.0-incubating)所贴代码有所删减。