使用扇出交换器实现发布/订阅
学习如何使用基于扇出交换器和临时队列的 RabbitMQ 发布/订阅模式,一次向多个消费者广播消息。
使用扇出交换器实现发布/订阅 是 CoddyKit 上的免费 RabbitMQ Messaging & Async Systems 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 RabbitMQ Messaging & Async Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 RabbitMQ Messaging & Async Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond a Single Consumer
Work queues deliver each message to one worker. But sometimes every interested consumer needs the same message — like broadcasting a live score to all dashboards.
This is the publish/subscribe pattern.
The Fanout Exchange
Pub/sub in RabbitMQ uses a fanout exchange, which copies every incoming message to all queues bound to it, ignoring routing keys entirely.
Declaring a Fanout Exchange
A producer declares the fanout exchange and publishes to it.
channel.exchangeDeclare('scores', 'fanout');
channel.basicPublish('scores', '', null, body);Empty Routing Key
Notice the routing key is an empty string. Fanout exchanges ignore it, so producers do not need to specify any routing logic at all.
Temporary Queues
Each subscriber usually wants a fresh, private queue. Declaring a queue with no name lets RabbitMQ generate a random one, and marking it exclusive auto-deletes it when the consumer disconnects.
String q = channel.queueDeclare().getQueue();Binding the Queue
The subscriber binds its temporary queue to the fanout exchange so it begins receiving broadcasts.
channel.queueBind(q, 'scores', '');Multiple Independent Subscribers
Because each subscriber has its own queue, they all get independent copies of every message. Adding a new subscriber does not affect the others.
Pub/Sub vs Work Queues
Key difference:
- Work queue — one queue, many workers, each message handled once
- Pub/sub — many queues, each subscriber gets every message
Common Use Cases
Fanout pub/sub fits:
- Live notifications and dashboards
- Cache invalidation across many nodes
- Broadcasting configuration changes
- Logging to multiple sinks
Messages Without Consumers
If no queue is bound to a fanout exchange when a message arrives, the message is simply discarded. Fanout does not store anything itself — queues do.
Scaling Subscribers
To scale pub/sub, just add more subscribers, each with its own temporary queue bound to the exchange. The fanout exchange handles fan-out automatically.
Quick Check
Test your pub/sub knowledge.
Recap
You learned the publish/subscribe pattern:
- Fanout exchanges broadcast to all bound queues
- The routing key is ignored
- Subscribers use temporary exclusive queues
- Each subscriber gets an independent copy of every message
This pattern is the foundation for notifications, cache invalidation, and live updates.
常见问题解答
「使用扇出交换器实现发布/订阅」课时是免费的吗?
是的 — 「使用扇出交换器实现发布/订阅」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 RabbitMQ Messaging & Async Systems 课程的其余内容,请升级到 CoddyKit PRO。 RabbitMQ Messaging & Async Systems 课程共包含 4 节课。
「使用扇出交换器实现发布/订阅」这节课中我会学到什么?
学习如何使用基于扇出交换器和临时队列的 RabbitMQ 发布/订阅模式,一次向多个消费者广播消息。 你通过在浏览器中直接运行的动手代码来练习 RabbitMQ Messaging & Async Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 RabbitMQ Messaging & Async Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 RabbitMQ Messaging & Async Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用扇出交换器实现发布/订阅」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 RabbitMQ Messaging & Async Systems 课中编写并运行代码吗?
能。每节 RabbitMQ Messaging & Async Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Hello World:简单队列
- 工作队列:公平分发
- 消息确认与持久化
- 使用扇出交换器实现发布/订阅