问题
Below is the desirable design of the queue with:
- P producer. The application that insert data
- X exchange.
- C1-C3 consumer. The applications that read from the queue
Queue details:
- A. Is just like queue log, if there is no client binding then message will be discarded.
- B. This is a working queue. it will do something if there is criteria match.
- C. Also a working queue. it will transform the data
A is optional, but B. C. will always in queue until some client process connect it.
The problem is determine which type of exchange that i should use. is it a fanout, direct or topic ? because I wanted the A queue to discard the message if there is no client connected but B & C should always keep the message.
And should the producer write once to the exchange, or write multiple time with different routing key or topic ?
回答1:
Answer the question: Do I want all queues to receive all messages?
If the answer is yes then you should use fanout. If the answer is no then you should use direct or topic. The whole point of direct or topic is that the queues themselves will only receive messages based on matching the routing key to the binding key.
Queue A should be instantiated by the consumer C1, and set to autodelete and non durable. This way when C1 disconnects the queue will be deleted and the messages will be discarded.
Conversely Queues B and C should be instantiated when the exchange is, either separately, or by the producer. The should be set to non autodelete and probably durable. If you are using durable queues you might want to have persistent messages (don't worry if queue A doesn't exist even persistent message won't be a problem here). This way as soon as the producer starts sending messages the queues will start queuing them up and no message will be missed, even if the consumers are not yet running.
Whether to use direct or topic exchanges is personal preference. I understand that direct exchanges should be faster while topic exchanges allow a lot of flexibility with routing/binding keys.
I am not 100% what you mean by your last question. Each message should only be written once to an exchange. If using fanout the exchange will take care of routing the messages to the queues correctly and that is it. If you are using direct or topic exchanges then its down to the binding keys to make sure that each queue receives the correct messages. You should not need to send a message with more than one routing key, if you are wishing to do something like that then you have got something backwards in your understanding. But you can have multiple binding keys to the exchange from a single queue.
Simple example. X is a direct exchange. B has the binding key black, C has one binding key of black and one binding key of white. P sends messages with either the routing key black or white. If it is black then both B and C will receive the message, if it is white then only C will receive it.
来源:https://stackoverflow.com/questions/12686868/rabbitmq-amqp-queue-design