当前位置:   article > 正文

26.RocketMQ之生产者发送消息源码

rocketmq发送消息源码

highlight: arduino-light

生产者发送消息的流程图

image.png

生产者发送消息的调用链路如下:

java org.apache.rocketmq.client.producer.DefaultMQProducer#send(org.apache.rocketmq.common.message.Message) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#send(org.apache.rocketmq.common.message.Message) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#send(org.apache.rocketmq.common.message.Message, long) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#sendDefaultImpl

接下来通过源码一步一步分析生产者是如何发送消息的,注释都在代码里。

同步消息发送

DefaultMQProducerImpl#send

java //发送消息 public SendResult send(Message msg) { //this.defaultMQProducer.getSendMsgTimeout(),发送超时时间默认3秒 return send(msg, this.defaultMQProducer.getSendMsgTimeout()); }

DefaultMQProducerImpl#send

java //发送消息,默认超时时间为3s public SendResult send(Message msg,long timeout){ //发送消息的模式是CommunicationMode.SYNC,即同步 return this.sendDefaultImpl(msg, CommunicationMode.SYNC, null, timeout); } 第一个参数是要发送的消息。

第二个参数是发送消息的模式,同步、异步、单向等。

第三个参数是回调,由于是同步发送消息,不需要回调,只有异步发送消息才需要回调。

第四个参数是发送消息的超时时间,默认3秒。

具体发送消息的源码先贴出来,接下来一步一步分析。

DefaultMQProducerImpl#sendDefaultImpl

```java private SendResult sendDefaultImpl( Message msg, final CommunicationMode communicationMode, final SendCallback sendCallback, final long timeout ) throws MQClientException, RemotingException, MQBrokerException, InterruptedException { this.makeSureStateOK(); //1.校验消息 Validators.checkMessage(msg, this.defaultMQProducer);

  1. final long invokeID = random.nextLong();
  2. //第一次发送消息的时间
  3. long beginTimestampFirst = System.currentTimeMillis();
  4. //上一次发送消息的时间
  5. //第一次发送消息的时间-上一次发送消息的时间等于发送消息的花费时间
  6. long beginTimestampPrev = beginTimestampFirst;
  7. long endTimestamp = beginTimestampFirst;
  8. //2.根据topic查找路由
  9. //从本地的topicPublishInfoTable查找TopicPublishInfo。
  10. //(TopicPublishInfo为空)或者(messageQueueList为empty或者null)
  11. //那么从nameServer拉取。
  12. //TopicPublishInfo主要包括messageQueueList,全局的sendWhichQueue。
  13. //判断是否发生改变 若发生改变需要更新原来的路由信息
  14. TopicPublishInfo topicPublishInfo=this.tryToFindTopicPublishInfo(msg.getTopic());
  15. //如果路由信息不为空 && 路由信息ok指的是topic对应的队列不为空
  16. if (topicPublishInfo != null && topicPublishInfo.ok()) {
  17. boolean callTimeout = false;
  18. MessageQueue mq = null;
  19. Exception exception = null;
  20. SendResult sendResult = null;
  21. //private int retryTimesWhenSendFailed = 2;
  22. //如果是同步模式 那么timesTotal是 1+2 =3,即会重试2
  23. //如果是异步发送 那么timesTotal是 1,既不会重试
  24. //发送超时即花费时间超过超时时间不会再去重试,而是直接抛出异常。
  25. //重试是在一个for循环里立刻去重试
  26. //总次数
  27. int timesTotal =
  28. communicationMode == CommunicationMode.SYNC ?
  29. 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;
  30. //已使用次数
  31. int times = 0;
  32. String[] brokersSent = new String[timesTotal];
  33. //for循环开始
  34. for (; times < timesTotal; times++) {
  35. //lastBrokerName是故障的brokerNmae,初始值是null
  36. //只有发送失败了才会再次进入循环
  37. //等第二次进入时lastBrokerName是上一次发送失败的brokerNmae
  38. String lastBrokerName = null == mq ? null : mq.getBrokerName();
  39. // 3.选择队列
  40. // 这里的lastBrokerName第一次是null
  41. // 第二次lastBrokerName是上一次发送失败的brokerNmae
  42. MessageQueue mqSelected =
  43. this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);
  44. if (mqSelected != null) {
  45. mq = mqSelected;
  46. //保存每次发送时的BrokerName到对应的数组下标
  47. brokersSent[times] = mq.getBrokerName();
  48. try {
  49. beginTimestampPrev = System.currentTimeMillis();
  50. if (times > 0) {
  51. //Reset topic with namespace during resend.
  52. msg.setTopic(this.defaultMQProducer.withNamespace(msg.getTopic()));
  53. }
  54. //第一次发送消息的时间-上一次发送消息的时间等于发送消息的花费时间
  55. long costTime = beginTimestampPrev - beginTimestampFirst;
  56. //如果总的花费时间超过3秒那么认为超时 超时不在重试跳出循环然后直接报错
  57. //private int sendMsgTimeout = 3000;
  58. if (timeout < costTime) {
  59. callTimeout = true;
  60. break;
  61. }
  62. //核心代码:发送消息
  63. sendResult = this.sendKernelImpl(
  64. msg,
  65. mq,
  66. communicationMode,
  67. sendCallback,
  68. topicPublishInfo,
  69. timeout - costTime);
  70. endTimestamp = System.currentTimeMillis();
  71. //更新不可用的broker
  72. this.updateFaultItem
  73. (mq.getBrokerName(),
  74. endTimestamp - beginTimestampPrev, false);
  75. switch (communicationMode) {
  76. case ASYNC:
  77. return null;
  78. case ONEWAY:
  79. return null;
  80. case SYNC:
  81. //同步模式返回
  82. if (sendResult.getSendStatus()!= SendStatus.SEND_OK) {
  83. //isRetryAnotherBrokerWhenNotStoreOK默认是true
  84. //代表当消息发送失败尝试发送到其他broker
  85. if (this.defaultMQProducer
  86. .isRetryAnotherBrokerWhenNotStoreOK()) {
  87. continue;
  88. }
  89. }
  90. return sendResult;
  91. default:
  92. break;
  93. }
  94. } catch (RemotingException e) {
  95. endTimestamp = System.currentTimeMillis();
  96. this.updateFaultItem
  97. (mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
  98. exception = e;
  99. continue;
  100. } catch (MQClientException e) {
  101. endTimestamp = System.currentTimeMillis();
  102. this.updateFaultItem
  103. (mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
  104. exception = e;
  105. continue;
  106. } catch (MQBrokerException e) {
  107. endTimestamp = System.currentTimeMillis();
  108. this.updateFaultItem
  109. (mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
  110. exception = e;
  111. switch (e.getResponseCode()) {
  112. case ResponseCode.TOPIC_NOT_EXIST:
  113. case ResponseCode.SERVICE_NOT_AVAILABLE:
  114. case ResponseCode.SYSTEM_ERROR:
  115. case ResponseCode.NO_PERMISSION:
  116. case ResponseCode.NO_BUYER_ID:
  117. case ResponseCode.NOT_IN_CURRENT_UNIT:
  118. continue;
  119. default:
  120. if (sendResult != null) {
  121. return sendResult;
  122. }
  123. throw e;
  124. }
  125. } catch (InterruptedException e) {
  126. endTimestamp = System.currentTimeMillis();
  127. this.updateFaultItem
  128. (mq.getBrokerName(), endTimestamp - beginTimestampPrev, false);
  129. throw e;
  130. }
  131. } else {
  132. break;
  133. }
  134. }
  135. //for循环结束
  136. //返回发送结果
  137. if (sendResult != null) {
  138. return sendResult;
  139. }
  140. //如果发送结果为空
  141. String info = String.format
  142. ("Send [%d] times, still failed, cost [%d]ms, Topic: %s, BrokersSent: %s",
  143. times,
  144. System.currentTimeMillis() - beginTimestampFirst,
  145. msg.getTopic(),
  146. Arrays.toString(brokersSent));
  147. info += FAQUrl.suggestTodo(FAQUrl.SEND_MSG_FAILED);
  148. MQClientException mqClientException = new MQClientException(info, exception);
  149. //如果是发送超时直接报错
  150. if (callTimeout) {
  151. throw new RemotingTooMuchRequestException("sendDefaultImpl call timeout");
  152. }
  153. if (exception instanceof MQBrokerException) {
  154. mqClientException.setResponseCode(((MQBrokerException) exception).getResponseCode());
  155. } else if (exception instanceof RemotingConnectException) {
  156. mqClientException.setResponseCode(ClientErrorCode.CONNECT_BROKER_EXCEPTION);
  157. } else if (exception instanceof RemotingTimeoutException) {
  158. mqClientException.setResponseCode(ClientErrorCode.ACCESS_BROKER_TIMEOUT);
  159. } else if (exception instanceof MQClientException) {
  160. mqClientException.setResponseCode(ClientErrorCode.BROKER_NOT_EXIST_EXCEPTION);
  161. }
  162. throw mqClientException;
  163. //if (topicPublishInfo != null && topicPublishInfo.ok()) 判断结束
  164. }
  165. //路由信息为空 或者 路由信息不ok即topic对应的队列为空
  166. //获取nameserverList
  167. List<String> nsList=this.getmQClientFactory()
  168. .getMQClientAPIImpl()
  169. .getNameServerAddressList();
  170. //如果nameserverList为空 抛出异常 没有nameserver的地址,请配置
  171. if (null == nsList || nsList.isEmpty()) {
  172. throw new MQClientException(
  173. "No name server address, please set it.");
  174. }
  175. //直接抛出没有topic对应的路由信息的异常
  176. throw new MQClientException("No route info of this topic, ");
  177. }

```

验证消息大小

Validators#checkMessage

```java public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer) throws MQClientException { //判断是否为空 if (null == msg) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null"); } // 校验Topic Validators.checkTopic(msg.getTopic());

  1. // 校验消息体
  2. if (null == msg.getBody()) {
  3. throw new
  4. MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
  5. }
  6. if (0 == msg.getBody().length) {
  7. throw new
  8. MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the messagelength is zero");
  9. }
  10. //默认是4M 4194304=1024 * 1024 * 4
  11. if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
  12. throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
  13. "the message body size over max value, MAX: ");
  14. }
  15. }
  1. public static void checkTopic(String topic) throws MQClientException {
  2. if (UtilAll.isBlank(topic)) {
  3. throw new MQClientException("The specified topic is blank", null);
  4. }
  5. if (!regularExpressionMatcher(topic, PATTERN)) {
  6. throw new MQClientException
  7. ("The specified topic[%s] contains illegal characters, allowing only");
  8. }
  9. if (topic.length() > CHARACTER_MAX_LENGTH) {
  10. throw new MQClientException
  11. ("The specified topic is longer than topic max length 255.");
  12. }
  13. //whether the same with system reserved keyword
  14. if (topic.equals(MixAll.AUTO_CREATE_TOPIC_KEY_TOPIC)) {
  15. throw new MQClientException
  16. ("The topic[%s] is conflict with AUTO_CREATE_TOPIC_KEY_TOPIC.");
  17. }
  18. }

```

缓存查找Topic||从服务器拉取Topic

DefaultMQProducerImpl#tryToFindTopicPublishInfo

```java private TopicPublishInfo tryToFindTopicPublishInfo(final String topic) { //从缓存中获得主题的路由信息 TopicPublishInfo topicPublishInfo = this.topicPublishInfoTable.get(topic); //缓存的路由信息为空,则从NameServer获取路由 if (null == topicPublishInfo || !topicPublishInfo.ok()) { this.topicPublishInfoTable.putIfAbsent(topic, new TopicPublishInfo()); this.mQClientFactory.updateTopicRouteInfoFromNameServer(topic); topicPublishInfo = this.topicPublishInfoTable.get(topic); }

  1. if (topicPublishInfo.isHaveTopicRouterInfo() || topicPublishInfo.ok()) {
  2. return topicPublishInfo;
  3. } else {
  4. //MQClientInstance#startScheduledTask
  5. //调用updateTopicRouteInfoFromNameServer起来一个定时器 30秒执行一次
  6. //如果未找到当前主题的路由信息,则用默认主题继续查找
  7. //#进入下一个方法
  8. this.mQClientFactory.updateTopicRouteInfoFromNameServer
  9. (topic, true, this.defaultMQProducer);
  10. topicPublishInfo = this.topicPublishInfoTable.get(topic);
  11. return topicPublishInfo;
  12. }
  13. }

**代码:返回值TopicPublishInfo** java public class TopicPublishInfo { private boolean orderTopic = false; //是否是顺序消息 private boolean haveTopicRouterInfo = false; //该主题对应的所有的消息队列 private List messageQueueList = new ArrayList ();
//每选择一次消息队列,该值+1 默认值是一个随机数 private volatile ThreadLocalIndex sendWhichQueue = new ThreadLocalIndex(); //关联Topic路由元信息 private TopicRouteData topicRouteData; } **返回值:TopicRouteData** java public class TopicRouteData extends RemotingSerializable { private String orderTopicConf; private List queueDatas; private List brokerDatas; private HashMap /* Filter Server */> filterServerTable;

  1. public TopicRouteData cloneTopicRouteData() {
  2. TopicRouteData topicRouteData = new TopicRouteData();
  3. topicRouteData.setQueueDatas(new ArrayList<QueueData>());
  4. topicRouteData.setBrokerDatas(new ArrayList<BrokerData>());
  5. topicRouteData.setFilterServerTable(new HashMap<String, List<String>>());
  6. topicRouteData.setOrderTopicConf(this.orderTopicConf);
  7. if (this.queueDatas != null) {
  8. topicRouteData.getQueueDatas().addAll(this.queueDatas);
  9. }
  10. if (this.brokerDatas != null) {
  11. topicRouteData.getBrokerDatas().addAll(this.brokerDatas);
  12. }
  13. if (this.filterServerTable != null) {
  14. topicRouteData.getFilterServerTable().putAll(this.filterServerTable);
  15. }
  16. return topicRouteData;
  17. }

```

MQClientInstance#updateTopicRouteInfoFromNameServer

```java //使用默认主题从NameServer获取路由信息 返回的是TopicRouteData if (isDefault && defaultMQProducer != null) { topicRouteData = this.mQClientAPIImpl.getDefaultTopicRouteInfoFromNameServer (defaultMQProducer.getCreateTopicKey(),1000 * 3);

  1. if (topicRouteData != null) {
  2. for (QueueData data : topicRouteData.getQueueDatas()) {
  3. //defaultMQProducer.getDefaultTopicQueueNums() = 4
  4. int queueNums
  5. = Math.min(defaultMQProducer.getDefaultTopicQueueNums(),
  6. data.getReadQueueNums());
  7. data.setReadQueueNums(queueNums);
  8. data.setWriteQueueNums(queueNums);
  9. }
  10. }
  11. } else {
  12. //使用指定主题从NameServer获取路由信息
  13. topicRouteData =
  14. this.mQClientAPIImpl.getTopicRouteInfoFromNameServer(topic, 1000 * 3);
  15. }
  16. //判断路由是否发生变化 如果发生变化 那么需要更改路由信息
  17. TopicRouteData old = this.topicRouteTable.get(topic);
  18. boolean changed = topicRouteDataIsChange(old, topicRouteData);
  19. if (!changed) {
  20. changed = this.isNeedUpdateTopicRouteInfo(topic);
  21. } else {
  22. log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
  23. }
  24. if (changed) {
  25. //将topicRouteData转换为发布队列
  26. TopicPublishInfo publishInfo = topicRouteData2TopicPublishInfo(topic, topicRouteData);
  27. publishInfo.setHaveTopicRouterInfo(true);
  28. //遍历生产
  29. Iterator<Entry<String, MQProducerInner>> it = this.producerTable.entrySet().iterator();
  30. while (it.hasNext()) {
  31. Entry<String, MQProducerInner> entry = it.next();
  32. MQProducerInner impl = entry.getValue();
  33. if (impl != null) {
  34. //生产者不为空时,更新publishInfo信息
  35. impl.updateTopicPublishInfo(topic, publishInfo);
  36. }
  37. }
  38. }

```

MQClientInstance#topicRouteData2TopicPublishInfo

```java public static TopicPublishInfo topicRouteData2TopicPublishInfo(final String topic, final TopicRouteData route) { //创建TopicPublishInfo对象 TopicPublishInfo info = new TopicPublishInfo(); //关联topicRoute info.setTopicRouteData(route); //顺序消息,更新TopicPublishInfo if (route.getOrderTopicConf() != null && route.getOrderTopicConf().length() > 0) { String[] brokers = route.getOrderTopicConf().split(";"); for (String broker : brokers) { String[] item = broker.split(":"); int nums = Integer.parseInt(item[1]); for (int i = 0; i < nums; i++) { MessageQueue mq = new MessageQueue(topic, item[0], i); info.getMessageQueueList().add(mq); } }

  1. info.setOrderTopic(true);
  2. } else {
  3. //非顺序消息更新TopicPublishInfo
  4. List<QueueData> qds = route.getQueueDatas();
  5. Collections.sort(qds);
  6. //遍历topic队列信息
  7. for (QueueData qd : qds) {
  8. //是否是写队列
  9. if (PermName.isWriteable(qd.getPerm())) {
  10. BrokerData brokerData = null;
  11. //遍历写队列Broker
  12. for (BrokerData bd : route.getBrokerDatas()) {
  13. //根据名称获得读队列对应的Broker
  14. if (bd.getBrokerName().equals(qd.getBrokerName())) {
  15. brokerData = bd;
  16. break;
  17. }
  18. }
  19. if (null == brokerData) {
  20. continue;
  21. }
  22. if (!brokerData.getBrokerAddrs().containsKey(MixAll.MASTER_ID)) {
  23. continue;
  24. }
  25. //封装TopicPublishInfo写队列
  26. for (int i = 0; i < qd.getWriteQueueNums(); i++) {
  27. MessageQueue mq = new MessageQueue(topic, qd.getBrokerName(), i);
  28. info.getMessageQueueList().add(mq);
  29. }
  30. }
  31. }
  32. info.setOrderTopic(false);
  33. }
  34. //返回TopicPublishInfo对象
  35. return info;
  36. }

```

选择队列:规避不可用队列,递增取模选择队列

DefaultMQProducerImpl#selectOneMessageQueue

java public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) { return this.mqFaultStrategy.selectOneMessageQueue(tpInfo, lastBrokerName); } MQFaultStrategy#selectOneMessageQueue ```java public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) { //Broker故障延迟机制 //默认不启用Broker故障延迟机制 if (this.sendLatencyFaultEnable) { try { //累加 int index = tpInfo.getSendWhichQueue().getAndIncrement(); //循环遍历规避不可用broker for (int i = 0; i < tpInfo.getMessageQueueList().size(); i++) { int pos = Math.abs(index++) % tpInfo.getMessageQueueList().size(); if (pos < 0) pos = 0; MessageQueue mq = tpInfo.getMessageQueueList().get(pos); //从不可用brokerMap中根据brokerName获取value //如果isAvailable是true 说明是可用的broker
if (latencyFaultTolerance.isAvailable(mq.getBrokerName())) { //如果lastBrokerName 说明是第一次进入该方法 if (null == lastBrokerName || mq.getBrokerName().equals(lastBrokerName)) return mq; } }

  1. //如果遍历完所有的broker发现都是出过故障的
  2. //只能尝试从规避的Broker中选择一个可用的Broker
  3. //具体策略是从存放不可用的broker的Map中的元素放入List
  4. //shuffle以后再sort 再对size 取半 记为half
  5. // 如果 half 小于00
  6. // 否则 tmpList.get(whichItemWorst.getAndIncrement() % half).getName()
  7. final String notBestBroker = latencyFaultTolerance.pickOneAtLeast();
  8. int writeQueueNums = tpInfo.getQueueIdByBroker(notBestBroker);
  9. if (writeQueueNums > 0) {
  10. final MessageQueue mq = tpInfo.selectOneMessageQueue();
  11. if (notBestBroker != null) {
  12. //此处设置了name selectOneMessageQueue没设置name
  13. //个人猜测设置了name 是根据name找到对应的queueList 再根据下标获取
  14. //没设置name 直接从全局的messageQueueList根据下标获取
  15. mq.setBrokerName(notBestBroker);
  16. //对该broker上的queueSize 取模选择队列
  17. mq.setQueueId
  18. (tpInfo.getSendWhichQueue().getAndIncrement()%writeQueueNums);
  19. }
  20. return mq;
  21. } else {
  22. //写队列小于0 意味着没必要参与发送消息 所以从故障的brokerMap中移除notBestBroker
  23. latencyFaultTolerance.remove(notBestBroker);
  24. }
  25. } catch (Exception e) {
  26. log.error("Error occurred when selecting message queue", e);
  27. }
  28. return tpInfo.selectOneMessageQueue();
  29. }
  30. //进入TopicPublishInfo#selectOneMessageQueue(lastBrokerName)
  31. return tpInfo.selectOneMessageQueue(lastBrokerName);
  32. }
  33. // 验证该broker是否可用 ,维护了一个存放不可用的broker的Map faultItemTable
  34. //如果map里有这个broker 那么就是不可用 返回false 否则返回true
  35. @Override
  36. public boolean isAvailable(final String name) {
  37. final FaultItem faultItem = this.faultItemTable.get(name);
  38. if (faultItem != null) {
  39. return faultItem.isAvailable();
  40. }
  41. return true;
  42. }

```

这种规避Broker发送失败的方法有一个缺陷,如果broker宕机,如果上一次选择的队列是宕机broker的第一个队列,那么下次选择的是宕机broker的第二个队列,导致消息再次发送失败。那么我们就需要利用broker故障延迟机制,在第一次broker消息发送失败后就将该broker暂时排除。

参考:原文链接:https://blog.csdn.net/weixin_45003125/article/details/105093907

```java

  1. //存放不可用的broker的Map 根据brokerName获取
  2. public boolean isAvailable(final String name) {
  3. final FaultItem faultItem = this.faultItemTable.get(name);
  4. if (faultItem != null) {
  5. return faultItem.isAvailable();
  6. }
  7. return true;
  8. }
  9. //old.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);
  10. //因为startTimestamp 等于 update时的当前系统时间 + 规避时长
  11. //所以这里直接用当前的系统时间和startTimestamp 比较
  12. public boolean isAvailable() {
  13. return (System.currentTimeMillis() - startTimestamp) >= 0;
  14. }

```

TopicPublishInfo#selectOneMessageQueue(lastBrokerName)

java public MessageQueue selectOneMessageQueue(final String lastBrokerName) { //第一次选择队列 if (lastBrokerName == null) { return selectOneMessageQueue(); } else { //sendWhichQueue int index = this.sendWhichQueue.getAndIncrement(); //遍历消息队列集合 for (int i = 0; i < this.messageQueueList.size(); i++) { //sendWhichQueue自增后 对所有的queueSize取模 int pos = Math.abs(index++) % this.messageQueueList.size(); if (pos < 0) pos = 0; //尽量规避上次已经发送过的Broker队列 MessageQueue mq = this.messageQueueList.get(pos); if (!mq.getBrokerName().equals(lastBrokerName)) { return mq; } } //如果以上情况都不满足,返回sendWhichQueue取模后的队列 return selectOneMessageQueue(); } } 代码:TopicPublishInfo#selectOneMessageQueue() java //第一次选择队列 public MessageQueue selectOneMessageQueue() { //sendWhichQueue自增 int index = this.sendWhichQueue.getAndIncrement(); //对队列大小取模 int pos = Math.abs(index) % this.messageQueueList.size(); if (pos < 0) pos = 0; //返回对应的队列 return this.messageQueueList.get(pos); } image.png

  • 延迟机制接口规范

java public interface LatencyFaultTolerance<T> { //更新失败条目 void updateFaultItem(final T name, final long currentLatency, final long notAvailableDuration); //判断Broker是否可用 boolean isAvailable(final T name); //移除Fault条目 void remove(final T name); //尝试从规避的Broker中选择一个可用的Broker T pickOneAtLeast(); } * FaultItem:失败条目

java class FaultItem implements Comparable<FaultItem> { //条目唯一键,这里为brokerName private final String name; //本次消息发送延迟 private volatile long currentLatency; //故障规避开始时间 private volatile long startTimestamp; } * 消息失败策略

java public class MQFaultStrategy { //根据currentLatency本地消息发送延迟,从latencyMax尾部向前找到第一个比currentLatency小的索引,如果没有找到,返回0 private long[] latencyMax = {50L, 100L, 550L, 1000L, 2000L, 3000L, 15000L}; //根据这个索引从notAvailableDuration取出对应的时间,在该时长内,Broker设置为不可用 private long[] notAvailableDuration = {0L, 0L, 30000L, 60000L, 120000L, 180000L, 600000L}; } 原理分析

代码:DefaultMQProducerImpl#sendDefaultImpl java sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout - costTime); endTimestamp = System.currentTimeMillis(); this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, false); 如果上述发送过程出现异常,则调用DefaultMQProducerImpl#updateFaultItem java public void updateFaultItem(final String brokerName, final long currentLatency, boolean isolation) { //参数一:broker名称 //参数二:本次消息发送延迟时间 //参数三:是否隔离 this.mqFaultStrategy.updateFaultItem(brokerName, currentLatency, isolation); } 代码:MQFaultStrategy#updateFaultItem ```java public void updateFaultItem(final String brokerName, final long currentLatency, boolean isolation) { if (this.sendLatencyFaultEnable) { //计算broker规避的时长 long duration = computeNotAvailableDuration(isolation ? 30000 : currentLatency); //更新该FaultItem规避时长 this.latencyFaultTolerance.updateFaultItem(brokerName, currentLatency, duration); } }

代码:MQFaultStrategy#computeNotAvailableDuration

  1. private long computeNotAvailableDuration(final long currentLatency) {
  2. //遍历latencyMax
  3. for (int i = latencyMax.length - 1; i >= 0; i--) {
  4. //找到第一个比currentLatency的latencyMax值
  5. if (currentLatency >= latencyMax[i])
  6. return this.notAvailableDuration[i];
  7. }
  8. //没有找到则返回0
  9. return 0;
  10. }

**代码:LatencyFaultToleranceImpl#updateFaultItem** java public void updateFaultItem(final String name, final long currentLatency, final long notAvailableDuration) { //获得原FaultItem FaultItem old = this.faultItemTable.get(name); //为空新建faultItem对象,设置规避时长和开始时间 if (null == old) { final FaultItem faultItem = new FaultItem(name); faultItem.setCurrentLatency(currentLatency); faultItem.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);

  1. old = this.faultItemTable.putIfAbsent(name, faultItem);
  2. if (old != null) {
  3. old.setCurrentLatency(currentLatency);
  4. old.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);
  5. }
  6. } else {
  7. //更新规避时长和开始时间
  8. old.setCurrentLatency(currentLatency);
  9. old.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);
  10. }
  11. }

```

发送消息

java org.apache.rocketmq.client.producer.DefaultMQProducer#send(org.apache.rocketmq.common.message.Message) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#send(org.apache.rocketmq.common.message.Message) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#send(org.apache.rocketmq.common.message.Message, long) ​ org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#sendDefaultImpl    

DefaultMQProducerImpl#sendKernelImpl

```java private SendResult sendKernelImpl(    final Message msg, //待发送消息    final MessageQueue mq, //消息发送队列    final CommunicationMode communicationMode, //消息发送模式    final SendCallback sendCallback, //异步消息回调函数    final TopicPublishInfo topicPublishInfo, //主题路由信息    final long timeout //超时时间   ){ throws MQClientException, RemotingException, MQBrokerException, InterruptedException {            long beginStartTime = System.currentTimeMillis();            //获得broker网络地址信息 172.18.95.215:10911 //首先从缓存的map中找        String brokerAddr = this.mQClientFactory           .findBrokerAddressInPublish(mq.getBrokerName());       //没有找到从NameServer更新broker网络地址信息放入缓存map        if (null == brokerAddr) {            tryToFindTopicPublishInfo(mq.getTopic());            //从缓存的map中找            brokerAddr = this.mQClientFactory               .findBrokerAddressInPublish(mq.getBrokerName());       }

  1. SendMessageContext context = null;
  2.        if (brokerAddr != null) {
  3.            brokerAddr = MixAll
  4.               .brokerVIPChannel(this.defaultMQProducer
  5.                                 .isSendMessageWithVIPChannel(), brokerAddr);
  6.            byte[] prevBody = msg.getBody();
  7.            try {
  8.                
  9.                //判断不是批量发送消息 为消息分配唯一ID
  10.                if (!(msg instanceof MessageBatch)) {
  11.                    MessageClientIDSetter.setUniqID(msg);
  12.               }
  13.                boolean topicWithNamespace = false;
  14.                if (null != this.mQClientFactory.getClientConfig().getNamespace()) {
  15.                    msg.setInstanceId(this.mQClientFactory.getClientConfig()
  16.                                     .getNamespace());
  17.                    topicWithNamespace = true;
  18.               }
  19.                //消息大小超过4K,启用消息压缩
  20.                int sysFlag = 0;
  21.                boolean msgBodyCompressed = false;
  22.                if (this.tryToCompressMessage(msg)) {
  23.                    sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
  24.                    msgBodyCompressed = true;
  25.               }
  26.                
  27. //如果是事务消息,设置消息标记MessageSysFlag.TRANSACTION_PREPARED_TYPE
  28.                final String tranMsg = msg
  29.                   .getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);
  30.                
  31.                if (tranMsg != null && Boolean.parseBoolean(tranMsg)) {
  32.                    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
  33.               }
  34.                
  35. //如果注册了消息发送钩子函数,在执行消息发送前的增强逻辑
  36.                if (hasCheckForbiddenHook()) {
  37.                    
  38.                    CheckForbiddenContext
  39.                        checkForbiddenContext = new CheckForbiddenContext();
  40.                    
  41.                    checkForbiddenContext
  42.                       .setNameSrvAddr(this.defaultMQProducer.getNamesrvAddr());
  43.                    
  44.                    checkForbiddenContext
  45.                       .setGroup(this.defaultMQProducer.getProducerGroup());
  46.                    
  47.                    checkForbiddenContext.
  48.                        setCommunicationMode(communicationMode);
  49.                    
  50.                    checkForbiddenContext.setBrokerAddr(brokerAddr);
  51.                    checkForbiddenContext.setMessage(msg);
  52.                    checkForbiddenContext.setMq(mq);
  53.                    checkForbiddenContext.setUnitMode(this.isUnitMode());
  54.                    //可以通过 registerCheckForbiddenHook 注册 CheckForbiddenHook
  55.                    this.executeCheckForbiddenHook(checkForbiddenContext);
  56.               }
  57.                
  58.                if (this.hasSendMessageHook()) {
  59.                    context = new SendMessageContext();
  60.                    context.setProducer(this);
  61.                    context.setProducerGroup(this.defaultMQProducer.getProducerGroup());
  62.                    context.setCommunicationMode(communicationMode);
  63.                    context.setBornHost(this.defaultMQProducer.getClientIP());
  64.                    context.setBrokerAddr(brokerAddr);
  65.                    context.setMessage(msg);
  66.                    context.setMq(mq);
  67.                    context.setNamespace(this.defaultMQProducer.getNamespace());
  68.                    String isTrans = msg.
  69.                        getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);
  70.                    if (isTrans != null && isTrans.equals("true")) {
  71.                        context.setMsgType(MessageType.Trans_Msg_Half);
  72.                   }
  73.                    if (msg.getProperty("__STARTDELIVERTIME") != null
  74.                        || msg.getProperty(MessageConst.PROPERTY_DELAY_TIME_LEVEL)
  75.                        != null) {
  76.                        context.setMsgType(MessageType.Delay_Msg);
  77.                   }
  78.                    //可以通过 registerSendMessageHook(final SendMessageHook hook) 注册hook
  79.                    //执行钩子前置函数
  80.                    this.executeSendMessageHookBefore(context);
  81.               }
  82. //构建消息发送请求包
  83.                SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
  84.                //生产者组
  85.                requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
  86.                //主题
  87.                requestHeader.setTopic(msg.getTopic());
  88.                //默认创建主题Key
  89.                requestHeader.setDefaultTopic(this.defaultMQProducer.getCreateTopicKey());
  90.                //该主题在单个Broker默认队列树
  91.         requestHeader.setDefaultTopicQueueNums
  92.               (this.defaultMQProducer.getDefaultTopicQueueNums());
  93.                //队列ID
  94.                requestHeader.setQueueId(mq.getQueueId());
  95.                //消息系统标记
  96.                requestHeader.setSysFlag(sysFlag);
  97.                //消息发送时间
  98.                requestHeader.setBornTimestamp(System.currentTimeMillis());
  99.                //消息标记
  100.                requestHeader.setFlag(msg.getFlag());
  101.                //消息扩展信息
  102.                requestHeader.setProperties
  103.                   (MessageDecoder.messageProperties2String(msg.getProperties()));
  104.                //消息重试次数
  105.                requestHeader.setReconsumeTimes(0);
  106.                requestHeader.setUnitMode(this.isUnitMode());
  107.                //是否是批量消息等
  108.                requestHeader.setBatch(msg instanceof MessageBatch);
  109.                
  110.                //如果topic以%RETRY% 开头
  111.                if (requestHeader.getTopic()
  112.                   .startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)){
  113.                    
  114.                    //获取属性 RECONSUME_TIME
  115.                    String reconsumeTimes = MessageAccessor.getReconsumeTime(msg);
  116.                    if (reconsumeTimes != null) {
  117.                        requestHeader.
  118.                            setReconsumeTimes(Integer.valueOf(reconsumeTimes));
  119.                        //清除属性 RECONSUME_TIME
  120.                        MessageAccessor.
  121.                            clearProperty(msg, MessageConst.PROPERTY_RECONSUME_TIME);
  122.                   }
  123.                
  124. //获取属性 MAX_RECONSUME_TIMES
  125.                    String maxReconsumeTimes = MessageAccessor.getMaxReconsumeTimes(msg);
  126.                    if (maxReconsumeTimes != null) {
  127.                        requestHeader
  128.                           .setMaxReconsumeTimes(Integer.valueOf(maxReconsumeTimes));
  129.                        //清除属性 MAX_RECONSUME_TIMES
  130.                        MessageAccessor.clearProperty
  131.                           (msg,MessageConst.PROPERTY_MAX_RECONSUME_TIMES);
  132.                   }
  133.               }
  134.                SendResult sendResult = null;
  135.                switch (communicationMode) {
  136.                     //异步发送消息
  137.                    case ASYNC:
  138.                        Message tmpMessage = msg;
  139.                        boolean messageCloned = false;
  140.                        //如果消息被压缩了
  141.                        if (msgBodyCompressed) {
  142.                            tmpMessage = MessageAccessor.cloneMessage(msg);
  143.                            messageCloned = true;
  144.                            msg.setBody(prevBody);
  145.                       }
  146.                        if (topicWithNamespace) {
  147.                            if (!messageCloned) {
  148.                                tmpMessage = MessageAccessor.cloneMessage(msg);
  149.                                messageCloned = true;
  150.                           }
  151.                            msg.setTopic(
  152.                                NamespaceUtil.withoutNamespace(msg.getTopic(),                                           this.defaultMQProducer.getNamespace()));
  153.                       }
  154.                        long costTimeAsync = System.currentTimeMillis() - beginStartTime;
  155.                        if (timeout < costTimeAsync) {
  156.                            throw new RemotingTooMuchRequestException ("sendKernelImpl call timeout");
  157.                       }
  158.                        //发送消息
  159.                        sendResult = this.mQClientFactory.getMQClientAPIImpl()
  160.                           .sendMessage(
  161.                            brokerAddr,
  162.                            mq.getBrokerName(),
  163.                            tmpMessage,
  164.                            requestHeader,
  165.                            timeout - costTimeAsync,
  166.                            communicationMode,
  167.                            sendCallback,
  168.                            topicPublishInfo,
  169.                            this.mQClientFactory,
  170.                            this.defaultMQProducer.getRetryTimesWhenSendAsyncFailed(),
  171.                            context,
  172.                            this);
  173.                        break;
  174.                    case ONEWAY:
  175.                    case SYNC:
  176.                          //同步发送消息
  177.                        long costTimeSync = System.currentTimeMillis() - beginStartTime;
  178.                        if (timeout < costTimeSync) {
  179.               throw new RemotingTooMuchRequestException("sendKernelImpl call timeout");
  180.                       }
  181.                        sendResult = this.mQClientFactory.getMQClientAPIImpl().sendMessage(
  182.                            brokerAddr,
  183.                            mq.getBrokerName(),
  184.                            msg,
  185.                            requestHeader,
  186.                            timeout - costTimeSync,
  187.                            communicationMode,
  188.                            context,
  189.                            this);
  190.                        break;
  191.                    default:
  192.                        assert false;
  193.                        break;
  194.               }
  195.                
  196.                //可以通过 registerSendMessageHook(final SendMessageHook hook) 注册hook
  197. //执行后置hook函数
  198.                if (this.hasSendMessageHook()) {
  199.                    context.setSendResult(sendResult);
  200.                    this.executeSendMessageHookAfter(context);
  201.               }
  202.                return sendResult;
  203.           } catch (RemotingException e) {
  204.                if (this.hasSendMessageHook()) {
  205.                    context.setException(e);
  206.                    this.executeSendMessageHookAfter(context);
  207.               }
  208.                throw e;
  209.           } catch (MQBrokerException e) {
  210.                if (this.hasSendMessageHook()) {
  211.                    context.setException(e);
  212.                    this.executeSendMessageHookAfter(context);
  213.               }
  214.                throw e;
  215.           } catch (InterruptedException e) {
  216.                if (this.hasSendMessageHook()) {
  217.                    context.setException(e);
  218.                    this.executeSendMessageHookAfter(context);
  219.               }
  220.                throw e;
  221.           } finally {
  222.                msg.setBody(prevBody);
  223.                msg.setTopic(NamespaceUtil.withoutNamespace(msg.getTopic(),
  224.                             this.defaultMQProducer.getNamespace()));
  225.           }
  226.       }
  227.        throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
  228.   }
  229.    public MQClientInstance getmQClientFactory() {
  230.        return mQClientFactory;
  231.   }
  232. //单条消息大于4K需要压缩
  233. //private int compressMsgBodyOverHowmuch = 1024 * 4;
  234.    private boolean tryToCompressMessage(final Message msg) {
  235.        if (msg instanceof MessageBatch) {
  236.            //batch dose not support compressing right now
  237.            return false;
  238.       }
  239.        byte[] body = msg.getBody();
  240.        if (body != null) {
  241.            //
  242.            if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
  243.                try {
  244.                    byte[] data = UtilAll.compress(body, zipCompressLevel);
  245.                    if (data != null) {
  246.                        msg.setBody(data);
  247.                        return true;
  248.                   }
  249.               } catch (IOException e) {
  250.                    log.error("tryToCompressMessage exception", e);
  251.                    log.warn(msg.toString());
  252.               }
  253.           }
  254.       }
  255.        return false;
  256.   }

```

SendMessageHook

java //在producer中可以通过 registerSendMessageHook(final SendMessageHook hook) 注册hook public interface SendMessageHook {    String hookName(); ​    void sendMessageBefore(final SendMessageContext context); ​    void sendMessageAfter(final SendMessageContext context); } 一般只有异步发消息才需要回调。

批量消息发送

image.png

批量消息发送是将同一个主题的多条消息一起打包发送到消息服务端,减少网络调用次数,提高网络传输效率。当然,并不是在同一批次中发送的消息数量越多越好,其判断依据是单条消息的长度,如果单条消息内容比较长,则打包多条消息发送会影响其他线程发送消息的响应时间,并且单批次消息总长度不能超过DefaultMQProducer#maxMessageSize,默认4M。

批量消息发送要解决的问题是如何将这些消息编码以便服务端能够正确解码出每条消息的消息内容。

DefaultMQProducer#send

java public SendResult send(Collection<Message> msgs)    throws MQClientException, RemotingException, MQBrokerException, InterruptedException {    //压缩消息集合成一条消息,然后发送出去    return this.defaultMQProducerImpl.send(batch(msgs)); }

DefaultMQProducer#batch

java private MessageBatch batch(Collection<Message> msgs) throws MQClientException {    MessageBatch msgBatch;    try {        //将集合消息封装到MessageBatch        msgBatch = MessageBatch.generateFromList(msgs);        //遍历消息集合,检查消息合法性,设置消息ID,设置Topic        for (Message message : msgBatch) {            Validators.checkMessage(message, this);            MessageClientIDSetter.setUniqID(message);            message.setTopic(withNamespace(message.getTopic()));       }        //压缩消息,设置消息body        msgBatch.setBody(msgBatch.encode());   } catch (Exception e) {        throw new MQClientException("Failed to initiate the MessageBatch", e);   }    //设置msgBatch的topic    msgBatch.setTopic(withNamespace(msgBatch.getTopic()));    return msgBatch; }
生产者发送消息就讲这么多,其中同步/异步发送消息大同小异,异步消息多了1个发送的回调。至于发送消息的请求到达broker端,broker如何处理我们在讲broker的时候细说。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/256901
推荐阅读
相关标签
  

闽ICP备14008679号