Reference
Install: https://www.rabbitmq.com/download.html
Doc: https://www.rabbitmq.com/documentation.html
Tutorials: https://www.rabbitmq.com/getstarted.html
Sample Repo: https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/python
Introduction
RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.
The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead, it accepts, stores and forwards binary blobs of data ‒ messages
RabbitMQ 是一个消息代理:它接受和转发消息。 您可以将其视为邮局:当您将要投递的邮件放入邮箱时,您可以确定邮递员先生或女士最终会将邮件递送给您的收件人。 在这个比喻中,RabbitMQ 是一个邮箱、一个邮局和一个邮递员。
RabbitMQ 和邮局之间的主要区别在于它不处理纸张,而是接受、存储和转发二进制数据块 - 消息。
通用术语
RabbitMQ, and messaging in general, uses some jargon
Producing means nothing more than sending. A program that sends messages is a producer
A queue is the name for a post box that lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue
Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages
简单来说: 生产只不过意味着发送。发送消息的程序是生产者; 消费与接收具有相似的含义。 消费者是一个主要等待接收消息的程序
Note
In RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
Prerequisites
Producer 工作之前需先声明exchage, 指定exchage_type
Conusumer 工作之前需声明exchage, 指定exchage_type, 声明queue,声明queue和exchange之间的binding关系
关键概念
- Exchange
- Binding
- Routing
A binding is a relationship between an exchange and a queue. This can be simply read as: the queue is interested in messages from this exchange.
The meaning of a binding key depends on the exchange type. The fanout exchanges, which we used previously, simply ignored their value.
binding 表示 exchange和queue之间的关系; 不同的exchange类型binding key 有不同的作用, fanout类型中binding key没有作用
Exchange Type
- direct
- topic
- headers
- fanout
Exchange Type | Producer 发送消息参数 | Consumer 接收消息参数 |
---|---|---|
direct | exchange;; routing_key | queue name |
topic | exchange; routing_key | queue_name |
headers | / | / |
fanout | exchange, routing_key会被忽略 | queue_name |
下文中 P 表示Producer;C 表示Consumer
Direct exchange
最直接的模式,通过binding key(routing key 全文本匹配,相同则绑定exchage和queue)
Topic
Topic 模式中,将routing key 通过以点分割的通配表达式匹配,匹配成功则绑定;
* (star) can substitute for exactly one word.
# (hash) can substitute for zero or more words.
Publish/Subscribe (Fanout)
发布订阅模式,绑定到到exchage的queue都会收到消息