me/MQ/RocketMQ.md
2025-03-20 14:18:11 +08:00

71 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# RocketMQ
官方网站https://rocketmq.apache.org/zh/
中文文档https://rocketmq.apache.org/zh/docs/
从网上找的现成的消息传递基本流程
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4bd3fbf9-f992-4b59-aa8d-cb5b9427b54a%2FUntitled.png?table=block&id=390f5e79-bd3b-4df7-a589-a6b42f4bbcf2&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1420&userId=&cache=v2'/>
# 普通消息的使用场景
## 微服务解耦
<img width='80%' height='70%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fb915eeaf-8ac8-42ff-bfc2-905e4ab977f9%2F%25E6%259C%25AA%25E5%2591%25BD%25E5%2590%258D%25E6%2596%2587%25E4%25BB%25B6.png?table=block&id=fb776706-35ab-4cf0-bda9-7a4c254701d8&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1230&userId=&cache=v2'/>
## 异步执行事件
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fec792ba7-93be-40f3-a9f2-3446d2969e2f%2F%25E6%259C%25AA%25E5%2591%25BD%25E5%2590%258D%25E6%2596%2587%25E4%25BB%25B6_(1).png?table=block&id=40f2ec24-8403-41c9-8d46-5fc0e8054445&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1210&userId=&cache=v2'/>
## 数据集成
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F98d8ceec-6d68-4913-8103-c42edf6098b5%2F%25E6%259C%25AA%25E5%2591%25BD%25E5%2590%258D%25E6%2596%2587%25E4%25BB%25B6_(2).png?table=block&id=60c47f48-b2af-49c7-909d-3357e8b80474&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1250&userId=&cache=v2'/>
## 普通消息的声明周期
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F13489b45-1d77-4996-8214-527e381a9c4e%2Flifecyclefornormal-e8a2a7e42a0722f681eb129b51e1bd66.png?table=block&id=c46ce93e-1524-4bc8-b34b-2225989d23c7&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1420&userId=&cache=v2'/>
- 初始化:生产者构建并完成初始化,待发送到服务端
- 待消费:发送到服务端,对消费者可见,等待消费
- 消费中:被消费者处理,服务端会等待消费完成,如果没有收到消费者的响应,会进行重试。
- 消费提交:消费者完成消费处理,向服务端提交消费结果,服务端标记当前消息已经被处理,消费成功、失败
- 删除消息:在消息到期或存储空间不足时,将消息从物理文件中删除
发送消息时可以设置一个key方便查询消息轨迹。这个key最好是唯一的比如订单编号、用户ID、Trace等。
# 延时/定时消息
**生产者设置好消息的定时时间,服务端按时投递给消费者**。一个典型的场景用户在业务系统中发起支付创建一笔支付单后5分钟未付款关闭支付单。
## 注意事项
- 定时的时间是一个毫秒级 Unix 时间戳
- 必须设置在24小时范围内超过范围不生效
- 若设置在当前时间之前,服务端将立即投递
## 声明周期
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F7a173aeb-b3f9-414a-a8cd-890dbff7b582%2Flifecyclefordelay-2ce8278df69cd026dd11ffd27ab09a17.png?table=block&id=62387da8-21ca-4e89-aaaa-270fac739290&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1420&userId=&cache=v2'/>
相比普通消息,在初始化和待消费之间,加了一个定时中的状态。服务端将延时消息单独存储,到时间以后再挪到普通消息的存储位置,暴露给消费者进行消费。
⚠️如果大量的延时消息在同一时刻触发,会造成系统压力过大、消息延迟,影响定时精度,尽量打散定时时间。
# 顺序消息
典型的应用场景有序的事件处理、数据实时增量同步比如用canal同步mysql的binlog至ES或其他存储介质。
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ffdd6664e-9e1a-4df8-80cd-94be1e076e01%2F%25E6%259C%25AA%25E5%2591%25BD%25E5%2590%258D%25E6%2596%2587%25E4%25BB%25B6.png?table=block&id=642c7b5a-3daf-4e4c-ae3e-1a956b0fd94f&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1420&userId=&cache=v2'>
顺序消息的关系是通过 Message Group 识别的,生产者发送顺序消息时,需要为每条顺序消息设置 Message Group。相同 Message Group 的消息遵循 FIFO 原则。所以有以下两个限制需要关注:
- 必须一个生产者一个 Message Group 才能保障顺序性,不同的生产者设置同一个 Message Group 是无法保证顺序性的
- 串行发送,使用多线程并行发送的话,也无法保障顺序性
## 存储逻辑
相同的消息组按照先后顺序存储到同一个队列,不同消息组的消息可以混在同一个队列,但是不一定连续。
<img width='80%' src='https://silent-detective-d21.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F06e52f53-0095-45dd-a346-209654cf591d%2Ffifomessagegroup-aad0a1b7e64089075db956c0eca0cbf4.png?table=block&id=a4c4ac4f-97dc-46c2-bf3d-26c1cfb4b242&spaceId=60569f3a-f166-46d7-9825-a6cdb259b284&width=1360&userId=&cache=v2'>