概述
最近有人问我知道rocketMq是怎么查询消息的,我发现我貌似回答不上来,所以抽空就把这块内容补充一下,主要是讲清楚根据key查询消息和根据msgId查询消息两块内容。
根据key查询消息
看下参数列表中我们可以看到-k指出了核心key的参数,指定了根据key查询消息的方法,这个命令返回的是msgId,据说还有一些坑,可以参考网易团队的这篇博文。
![img_de95f83f72c5785d6ea1081a60623d56.png](https://yqfile.alicdn.com/img_de95f83f72c5785d6ea1081a60623d56.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询
查询过程中我们从client端看可以看出来,其实根据key去查询的过程中其实是遍历所有broker去进行查询的,然后针对查询结果进行组合。
由于这个查询过程中server端涉及到index对象,关于index部分的查询参见《rocketMq - index介绍》博文。
![img_443672887d3c02f5103c7d9ea5c505e0.png](https://yqfile.alicdn.com/img_443672887d3c02f5103c7d9ea5c505e0.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-client-1
![img_89a20c019ffa36c91da23c816440c42a.png](https://yqfile.alicdn.com/img_89a20c019ffa36c91da23c816440c42a.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-client-2
![img_39ce7692c45cea72a692fe0d331a8142.png](https://yqfile.alicdn.com/img_39ce7692c45cea72a692fe0d331a8142.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-client-3
![img_e4c4a50a92d138206e93b61ac6e0c8ef.png](https://yqfile.alicdn.com/img_e4c4a50a92d138206e93b61ac6e0c8ef.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-client-4
![img_21de20269207ba796b23cb82a5499b74.png](https://yqfile.alicdn.com/img_21de20269207ba796b23cb82a5499b74.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-server-1
![img_5d8f833c572efb2e10934bee7999ae6c.png](https://yqfile.alicdn.com/img_5d8f833c572efb2e10934bee7999ae6c.png?x-oss-process=image/resize,w_1400/format,webp)
根据key查询-server-2
根据msgId查询消息
根据msgId的查询的参数如下图所示,可以看出来核心的参数在于-i的参数,也就是msgId。根据msgId去查询消息的过程中最重要的当然是msgId的参数了。
![img_75ba8e3cfc5a18ca40b126bed9c6f877.png](https://yqfile.alicdn.com/img_75ba8e3cfc5a18ca40b126bed9c6f877.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询
根据msgId查询的核心点在于msgId的生成规律,其实msgId是有两个部分组成的,分别是broker的address 和 commitLog的物理偏移量offset,也就是说通过messageId我们就能知道去哪个broker的哪个位置offset去获取消息体。
关于messageId的消息体,可以见下图中decodeMessageId的类图。
client端的查询过程和server端的解析查询过程都在下面的代码中贴出来,相信大家应该都能够看得懂的。
![img_364c8b22b0884079ac85dabb05eb5d15.png](https://yqfile.alicdn.com/img_364c8b22b0884079ac85dabb05eb5d15.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-client-1
![img_2d64cbdbaa012d52e0bb3a1137cee349.png](https://yqfile.alicdn.com/img_2d64cbdbaa012d52e0bb3a1137cee349.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-client-2
![img_187dcd53a1b59142fb16964ed200351a.png](https://yqfile.alicdn.com/img_187dcd53a1b59142fb16964ed200351a.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-client-3
![img_4ef8dfb97e068a7a3f5b39fd2fc2921e.png](https://yqfile.alicdn.com/img_4ef8dfb97e068a7a3f5b39fd2fc2921e.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-server-1
![img_729bce5da3b17b3379e04db73eab9485.png](https://yqfile.alicdn.com/img_729bce5da3b17b3379e04db73eab9485.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-server-2
![img_cd5d74459f05998694a1f6d33d044586.png](https://yqfile.alicdn.com/img_cd5d74459f05998694a1f6d33d044586.png?x-oss-process=image/resize,w_1400/format,webp)
根据msgId查询-server-3