RabbitMQ —为什么在使用主题交换时忽略我的路由键 [英] RabbitMQ — Why are my Routing Keys being ignored when using topic exchange

查看:109
本文介绍了RabbitMQ —为什么在使用主题交换时忽略我的路由键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.collectMY_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.collectedMY_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.

  1. 将传递消息队列发送给其中一个消费者,而不是发送给所有消费者.
  2. 根据邮件与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆