问题描述
我有两项服务,经理和收藏家.
- 管理器已通过routingKey
user.collected
订阅了队列COLLECTED_USER
,并调用了UserCollected
处理程序. - 收集器已通过routingKey
user.collect
订阅了队列COLLECT_USER
,并调用了CollectUser
处理程序.
- Manager is subscribed to Queue
COLLECTED_USER
with routingKeyuser.collected
and invokes aUserCollected
handler. - Collector is subscribed to Queue
COLLECT_USER
with routingKeyuser.collect
and invokes aCollectUser
handler.
可以有多个收集器,因此我已将exclusive
设置为false
(有关代码,请参见下文).
There can be multiple collectors so I have set exclusive
to false
(see below for code).
还有其他服务可以监听事件,例如
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 | routingKey | queueName |
| -------- | ----- | -------------- | ------------- |
| MY_APP | topic | user.collect | COLLECT_USER |
| MY_APP | topic | user.collected | COLLECTED_USER |
会发生什么:
- 管理器发布带有routingKey
user.collect
的消息 - 收集器获取
user.collect
消息并调用CollectUser
处理程序 - 收集器的
CollectUser
处理程序确实起作用,然后使用routingKeyuser.collected
发布消息 - 管理器获取
user.collected
消息并调用UserCollected
处理程序
- Manager publishes message with routingKey
user.collect
- Collector gets the
user.collect
message and invokes aCollectUser
handler - Collector's
CollectUser
handler does work, then publishes a message with routingKeyuser.collected
- Manager gets the
user.collected
message and invokes theUserCollected
handler
实际发生的事情:
- 管理器发布带有routingKey
user.collect
的消息(正确) - 收集器获取
user.collect
消息并调用CollectUser
处理程序(正确) - 管理器还会获取
user.collect
消息,并使用错误的数据调用UserCollected
处理程序. (错误) - 收集器的
CollectUser
处理程序确实起作用,然后发布带有routingKeyuser.collected
(正确)的消息 - 管理器获取
user.collected
消息并调用UserCollected
处理程序(正确)
- Manager publishes message with routingKey
user.collect
(correct) - Collector gets the
user.collect
message and invokes aCollectUser
handler (correct) - Manager also gets the
user.collect
message and invokes theUserCollected
handler with the wrong data. (wrong) - Collector's
CollectUser
handler does work, then publishes a message with routingKeyuser.collected
(correct) - Manager gets the
user.collected
message and invokes theUserCollected
handler (correct)
我的问题
管理器为什么会得到user.collect
消息,原因是:
My Question
Why does the Manager get the user.collect
message, given:
- 它在
COLLECTED_USER
队列而不是COLLECT_USER
队列上侦听,并且 - 正在
COLLECT_USER
队列上监听的 Collector 已经处理了该消息.
- It's listening on the
COLLECTED_USER
queue not theCOLLECT_USER
queue, and - The Collector, which is listening on the
COLLECT_USER
queue, has already handled the message.
实施细节
我按如下方式创建订阅者和发布者(针对相关性进行了修剪)
Implementation details
I create the subscribers and publishers as follows (trimmed for relevance)
根据AMQP url
和参数url
,exchange
,type
,routingKey
,queueName
和handler
given the AMQP url
and params url
, exchange
, type
, routingKey
, queueName
and handler
const connection = await amqp.connect(url)
const channel = await connection.createChannel()
channel.assertExchange(exchange, type, { durable: true })
const result = await channel.assertQueue(queueName, { exclusive: false })
channel.bindQueue(result.queue, exchange, routingKey)
channel.prefetch(1)
channel.consume(result.queue, handler)
创建发布者
鉴于AMQP url
和参数url
,exchange
和type
const connection = await amqp.connect(url)
const channel = await connection.createChannel()
await channel.assertExchange(exchange, type, { durable: true })
发布
给出channel
和参数exchange
,routingKey
和message
await channel.publish(exchange, routingKey, message)
注意
此问题是.
Note
This question is a follow-on from RabbitMQ — Why are my Routing Keys being ignored when using topic exchange.
推荐答案
我终于弄清了我的问题所在.肮脏的交流.在尝试此操作时,我无意间添加了一个交换,该交换将消息路由到错误的队列,这引起了我的困惑.
I finally worked out what my problem was. A dirty exchange. While experimenting with this I'd inadvertently added an exchange that was routing messages to the wrong queue, and this was causing my confusion.
要解决此问题,我启动了RabbitMQ管理员GUI并删除了所有队列,然后让我的代码创建所需的队列.上面列出的代码没有问题.
To fix it I fired up the RabbitMQ admin GUI and deleted all of the queues and let my code create the ones it needed. There was no issue with the code as outlined above.
这篇关于RabbitMQ-为什么错误的订户会收到已发布的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!