why do we need routing key in rabbitmq

孤街浪徒 提交于 2019-12-08 15:55:57

问题


I am wondering why do we need routing key to route message from exchange to queue. Can't we use the simply queue name to route the message. Also, in case of publishing to multiple queues we can use multiple queue names. Can anyone point out the scenario where we actually need routing key, queue name won't be suffice.


回答1:


There are several types of exchanges. The fanout exchange ignores the routing key and sends messages to all queues. But pretty much all other exchange types use the routing key to determine which queue, if any, will receive a message.

The tutorials on the RabbitMQ website describes several usecases where different exchange types are useful and where the routing key is relevant.

For instance, tutorial 5 demonstrates how to use a topic exchange to route log messages to different queues depending on the log level of each message.

If you want to target multiple queues, you need to bind them to a fanout exchange and use that exchange in your publisher.

You can't specify multiple queue names in your publisher. In AMQP, you do not publish a message to queues, you publish a message to an exchange. It's the exchange responsability to determine the relevant queues. It's possible that a message is routed to no queue at all and just dropped.




回答2:


Decoupling queue names from applications is useful for flexibility.

  • You could establish multiple queues to consume the same message, but queues can't have the same name.

  • In some cases, message's originator doesn't know the names of queues. (like when you have randomly generated queue names when horizontally scaling a server)

  • An exchange may be routing messages for more than just one type of consumer. Then you would need some wildcards in your routing keys to route messages to concerned consumers.



来源:https://stackoverflow.com/questions/36302341/why-do-we-need-routing-key-in-rabbitmq

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