RabbitMQ —为什么在使用主题交换时忽略我的路由键 [英] RabbitMQ — Why are my Routing Keys being ignored when using topic exchange
问题描述
我正在使用RabbitMQ来协调一组服务之间的事件,如下所示:
I'm using RabbitMQ to coordinate events between a collection of services as follows:
- 希望从单独的服务中收集用户数据时发出
user.collect
- 收听由单独服务发出的
user.collected
- emits
user.collect
when it wants user data to be collected from a separate service - listens for
user.collected
which is emitted by a separate service
- 在应该收集某些用户数据时监听
user.collect
,并且 - 在收集数据时会发出
user.collected
.
- listens for
user.collect
when it is supposed to collect some user data, and - emits
user.collected
when it's collected the data.
还有其他服务可以监听事件,例如
There are also other services that listen for events like
-
user.created
, -
user.updated
, -
user.deleted
user.created
,user.updated
,user.deleted
此外,还有一些服务可以监听更常见的事件,例如
In addition there are services that listen for more general events like
-
#.created
-
user.#
#.created
user.#
以此类推.
所以我正在使用topic
交换器.
So I am using a topic
exchange.
| exchange | type | queue | routingKey |
| -------- | ----- | --------------- | -------------- |
| MY_APP | topic | USER_COLLECTION | user.collect |
| MY_APP | topic | USER_COLLECTION | user.collected |
所有服务都与称为MY_APP
的交换机通信.
All of the services talk to the exchange called MY_APP
.
用户管理器创建一个生产者,该生产者向routingKey
user.collect
的MY_APP
交换发出user.collect
事件,并且它创建一个使用者,该消费者在队列USER_COLLECTION
上侦听具有routingKey user.collected
的事件.
The User Manager creates a producer that emits the user.collect
event to the MY_APP
exchange with routingKey
user.collect
, and it creates a consumer that listens on the queue USER_COLLECTION
for events with routingKey user.collected
.
用户收集器创建了一个生产者,该生产者使用routingKey user.collected
向MY_APP
交换发出user.collected
事件,并创建了一个消费者,该消费者在队列USER_COLLECTION
上侦听具有routingKey user.collect
的事件.
The User Collector creates a producer that emits the user.collected
event to the MY_APP
exchange with routingKey user.collected
and it creates a consumer that listens on the queue USER_COLLECTION
for events with routingKey user.collect
.
但是我发现用户管理器发出user.collect
,并且由用户管理器本身使用user.collected
的侦听器来拾取.
However I'm finding that the User Manager emits user.collect
and this is picked up by the User Manager itself with the listener for user.collected
.
就好像忽略了routingKey一样.我在做什么错了?
It's as if the routingKey is being ignored. What am I doing wrong?
请参见后续问题
推荐答案
为每个使用者创建不同的队列,并使用适当的绑定键user.collect或user.collected将队列绑定到主题交换.
Create different queues for each Consumer and bind queue(s) to topic exchange using appropriate binding key user.collect or user.collected.
- 将传递消息队列发送给其中一个消费者,而不是发送给所有消费者.
- 根据邮件与Topc Exchange的绑定密钥,将邮件路由到Queue.
查看此链接以获取更多详细示例. https://www.rabbitmq.com/tutorials/tutorial-five-python. html
Check this link for more detailed examples. https://www.rabbitmq.com/tutorials/tutorial-five-python.html
这篇关于RabbitMQ —为什么在使用主题交换时忽略我的路由键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!