RabbitMQ exchange 绑定复制消息发布到一个交换器跨两个其他交换器

RabbitMQ exchange bind replicating messages published to one exchange across two other exchanges

我的 rabbitMQ 中有 3 个不同的交换器,我正在尝试将发送到 mainex 的所有消息路由到其他两个交换器 dmyex monex 我尝试使用 channel.exchngeBind 方法绑定交易所。

我仍然无法看到发布到 mainex 的消息转到 dmyex 和 monex。

这在 RabbitMq 中可行吗?

我在这里做的有什么错误吗?

ch.exchangeDeclare("mainex", DIRECT_EXCHANGE_TYPE, true, false, null);
ch.exchangeDeclare("dmyex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
ch.exchangeDeclare("monex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
ch.exchangeBind("dmyex","mainex","abcd_KEY");
ch.exchangeBind("monex","mainex","abcd_KEY");

我认为交换 mainex 需要是主题交换才能使通配符路由正常工作,因为直接交换根据路由键的精确匹配进行路由。根据这个 CloudAMQP blog post on exchange types and routing:

A direct exchange delivers messages to queues based on a message routing key. The routing key is a message attribute added to the message header by the producer. Think of the routing key as an "address" that the exchange is using to decide how to route the message. A message goes to the queue(s) with the binding key that exactly matches the routing key of the message.

this answer to a question about wildcard routing keys on SO中也有提到)

应该足以使您的路由正常工作。也许更简单,如果您要为绑定到 mainex 的所有交换匹配任何路由键,您可以将 mainex 设为 Fanout 交换。

刚刚发现以下模型有效。

   ch.exchangeDeclare("mainex", DIRECT_EXCHANGE_TYPE, true, false, null);
   ch.exchangeDeclare("dmyex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
   ch.exchangeDeclare("mongoex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
   
   
   ch.exchangeBind("dmyex","mainex", "abcd_KEY");
   ch.exchangeBind("monex","mainex", "abcd_KEY");
   
   
   for (String q : Arrays.asList("sharding: dmyex - rabbit@node1 - 0", "sharding: dmyex - rabbit@node2 - 0","sharding: dmyex - rabbit@node3 - 0","sharding: dmyex - rabbit@node4 - 0","sharding: dmyex - rabbit@node5 - 0","sharding: dmyex - rabbit@node6 - 0")) {
       ch.queueBind(q, "dmyex", "1");
   }
   
   
   for (String q : Arrays.asList("sharding: mongoex - rabbit@node1 - 0", "sharding: mongoex - rabbit@node2 - 0","sharding: mongoex - rabbit@node3 - 0","sharding: mongoex - rabbit@node4- 0","sharding: mongoex - rabbit@node5 - 0","sharding: mongoex - rabbit@node6 - 0")) {
       ch.queueBind(q, "mongoex", "1");
   }
   
   
     AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
     for (int i = 0; i < 10; i++) {
     ch.basicPublish("mainex", "abcd_KEY", bldr.build(), "TestMessage".getBytes("UTF-8"));````