rocketMq消息查询
2018-04-22 本文已影响171人
晴天哥_王志
概述
最近有人问我知道rocketMq是怎么查询消息的,我发现我貌似回答不上来,所以抽空就把这块内容补充一下,主要是讲清楚根据key查询消息和根据msgId查询消息两块内容。
根据key查询消息
看下参数列表中我们可以看到-k指出了核心key的参数,指定了根据key查询消息的方法,这个命令返回的是msgId,据说还有一些坑,可以参考网易团队的这篇博文。

查询过程中我们从client端看可以看出来,其实根据key去查询的过程中其实是遍历所有broker去进行查询的,然后针对查询结果进行组合。
由于这个查询过程中server端涉及到index对象,关于index部分的查询参见《rocketMq - index介绍》博文。






根据msgId查询消息
根据msgId的查询的参数如下图所示,可以看出来核心的参数在于-i的参数,也就是msgId。根据msgId去查询消息的过程中最重要的当然是msgId的参数了。

根据msgId查询的核心点在于msgId的生成规律,其实msgId是有两个部分组成的,分别是broker的address 和 commitLog的物理偏移量offset,也就是说通过messageId我们就能知道去哪个broker的哪个位置offset去获取消息体。
关于messageId的消息体,可以见下图中decodeMessageId的类图。
client端的查询过程和server端的解析查询过程都在下面的代码中贴出来,相信大家应该都能够看得懂的。





