大家好,今天分享 RocketMQ 的 Broker 挂了,会带来什么影响。
面试官:你好,如果 RocketMQ 集群中的一个 Broker 挂了,会造成什么影响呢?
我:Broker 挂了,首先会导致 Producer 发送消息失败。对于普通消息,Producer 同步发送的情况下会有重试机制,重试时把消息发送到其他 Broker。如下图,Broker1 宕机了,把消息发送到了 Broker2:
发送消息的逻辑其实是是一个循环,发送失败后会不断尝试重新发送,代码如下:
int timesTotal = communicationMode == CommunicationMode.SYNC ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;
for (; times < timesTotal; times++) {
String lastBrokerName = null == mq ? null : mq.getBrokerName();
MessageQueue mqSelected = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);
if (mqSelected != null) {
mq = mqSelected;
try {
sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout - costTime);
switch (communicationMode) {
case ASYNC:
return null;
case ONEWAY:
return null;
case SYNC:
if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
//如果发送失败了,这里会进行重试
if (this.defaultMQProducer.isRetryAnotherBrokerWhenNotStoreOK()) {
continue;
}
}
return sendResult;
default:
break;
}
} catch (RemotingException e) {
}//省略其他 catch
} else {
break;
}