Topic exchange ambiguity with RabbitMQ

谁说我不能喝 提交于 2019-12-07 18:25:08

问题


I'm a little confused. I'm trying to implement topic exchanges and am not sure what is needed.

I want to have several routing keys and 1 topic exchange (the default amq.topic). My keys would be like:

  • customer.appA.created
  • customer.appB.created
  • customer.*.created

I want my queue(s) to be durable, but do I need 1 'customer' queue or 2 queues for appA and appB? I have my publisher figured out; connect, exchange declare, basic publish.

But I'm struggling with the consumers. Let's say I want to open 3 consoles, one for each of the aforementioned routing keys.
My current consumer has: connect, exchange declare, queue bind, basic consume. These are connected to a durable 'customer' queue. However my messages are being round-robin'ed to each console/consumer and not using the routing keys.

So my questions;

  1. For a typical topic exchange set up; how many queues do you need?
  2. Can my consumers get away with just exchange binding, or does it have to include queue interaction?
  3. Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?

回答1:


First thing first : Exchange do not deliver to Consumer. It delivers messages to Queue for matching routing kyes.

      1. For a typical topic exchange set up; how many queues do you need?

If you have multiple consumer then you will need one queue for every consumer.

      2. Can my consumers get away with just exchange binding, or does it have to include queue interaction?

You need to bind consumer with queue, if queue not exist then create and bind.

      3. Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?

YES, Only if the consumer have their own (separate queue bind with same routing key).Otherwise it will be Round Robin way.

So best way is to have consumer's own queue with required routing key...!!!




回答2:


For a typical topic exchange set up; how many queues do you need?

It depends on your application needs. You may start from just one queue to implement simple FIFO stack and later add more queues with for more granulated messages consuming.

Can my consumers get away with just exchange binding, or does it have to include queue interaction?

The idea of AMQP exchanges is to get published messages and put them to one or more queues (or even other exchanges or drop at all under certain condition). Consumer works only with queues, while publisher works only with exchanges.

There might be some misunderstandings with Default Exchange while it route messages to the queue name same as a routing key and it sometimes interpreted as publishing to queues which is not true.

Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?

I guess you are talking about duplicating one message to multiple queues (while there are some erroneous situations when you really may have single message being processed by multiple consumers).

If so - sure. You can just create multiple bindings for queue to get different messages. Here is small example:

Let you have three messages published to topic exchange with three different routing keys customer.appA.created, customer.appB.created and `customer.appC.created.

You can create separate queue to collect specific messages by binding queue with exact routing key - customer.appA.created, customer.appB.created and so on, or as you already noted, bind queue with wildcard routing key customer.*.created.

To collect messages only for appA and appB you can create queue with two bindings customer.appA.created and customer.appB.created, so you'll get two messages type in one queue.



来源:https://stackoverflow.com/questions/24159614/topic-exchange-ambiguity-with-rabbitmq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!