RabbitMQ交换绑定复制消息发布到一个交换在其他两个交换之间

时间:2020-07-21 12:02:33

标签: binding rabbitmq amqp messagebroker rabbitmq-exchange

我的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");

2 个答案:

答案 0 :(得分:0)

我认为交换mainex必须是主题交换,才能使通配符路由起作用,因为直接交换基于路由密钥的精确匹配来进行路由。为此CloudAMQP blog post on exchange types and routing

直接交换根据消息路由密钥将消息传递到队列。路由密钥是生产者添加到消息头的消息属性。将路由密钥视为交换所用来确定如何路由消息的“地址”。 一条消息进入绑定队列的绑定密钥与该消息的路由密钥完全匹配。

(在this answer to a question about wildcard routing keys on SO中也有涉及)

应该足以使您的路由正常工作。也许甚至更简单些,如果您要为绑定到mainex的所有交换匹配任何路由密钥,则只需将mainex做成扇出交换。

答案 1 :(得分:0)

只要弄清楚下面的模型是可行的。

   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"));````