java - 测试 RabbitMQ 交换器

标签 java rabbitmq rabbitmq-exchange

我正在尝试编写一个应用程序来测试将 N 个交换器绑定(bind)到内存、IO 等相同队列的效果。

所有测试都使用“主题”交换类型。我遇到的问题是,当我测试超过 1 个交换时,我没有收到我发布回来的所有消息。但是,当我使用 1 次交换时,我会收到所有消息。

知道为什么会这样吗?

谢谢。

编辑:

我有一个队列使用相同的绑定(bind)键绑定(bind)到两个“主题”交换:

  • *.system.log.#
  • #.system.error

我将如下向每个交易所发布两条消息:

  1. 交换 0

    • [正文] 消息 0 [路由键] #.system.error
    • [正文] 消息 1 [路由键] *.system.log.#
  2. 交换 1

    • [正文] 消息 0 [路由键] #.system.error
    • [正文] 消息 1 [路由键] *.system.log.#

我有一个消费者在监听队列,但只收到以下消息:

  • 带有路由 key (*.system.log.#) 的消息,来自交换器 0,正文为“消息 1”
  • 带有路由 key (*.system.log.#) 的消息,来自交换器 1,正文为“消息 1”

有什么想法吗?

最佳答案

我不太确定你想通过测试证明什么,但请记住 IO 和内存将是一个与队列整体大小相关的函数,而不是你绑定(bind)了多少交换单队列。您可能会在 Routing Topologies for Performance and Scalability with RabbitMQ 上找到这篇博文很有帮助。

无论如何,问题似乎出在您的绑定(bind)上,我可以通过使用 RabbitMQ 的 Web 管理门户来重现问题,而无需编写一行代码。

查看主题交换部分 here但重要的部分是:

Messages sent to a topic exchange can't have an arbitrary routing_key - it must be a list of words, delimited by dots. The words can be anything, but usually they specify some features connected to the message. A few valid routing key examples: "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit". There can be as many words in the routing key as you like, up to the limit of 255 bytes.

The binding key must also be in the same form. The logic behind the topic exchange is similar to a direct one - a message sent with a particular routing key will be delivered to all the queues that are bound with a matching binding key. However there are two important special cases for binding keys:

* (star) can substitute for exactly one word.
# (hash) can substitute for zero or more words.

这意味着当您发布消息时,您应该执行 system.errorsystem.log

但是,我认为您的交换绑定(bind)实际上有问题。如果您尝试使用 RabbitMQ Web 管理使用这些路由键进行发布,您会注意到您的消息实际上根本没有被路由到队列,这可以解释为什么您的消费者会收到它们......我会玩弄绑定(bind)并使用 RabbitMQ Web 管理在运行更大的测试之前尝试您尝试实现的路由行为。

关于java - 测试 RabbitMQ 交换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11931069/

相关文章:

rabbitmq - Spring-Rabbit模板-批量操作

ruby - 订阅一个队列,收到1条消息,然后取消订阅

java - DeliverCallback deliverCallback = (consumerTag, delivery) 函数未被调用

Grails RabbitMQ native 过滤发送给消费者的消息

rabbitmq - RabbitMQ 中长时间未确认的消息

java - 将 xml 数据导入 sql 数据库的语法 - 逐行

java - 解析批处理文件中的可执行控制台输出

java - 解释Hibernate查询缓存

java - hibernate : Table doesn't get updated

java - 如何检查MQTT中是否存在主题?