RabbitMQ中的主题交流与直接交流 [英] Topic Exchange vs Direct Exchange in RabbitMQ

查看:94
本文介绍了RabbitMQ中的主题交流与直接交流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个将使用RabbitMQ的应用程序,它具有几个不同的队列,用于在层之间传递消息.

We've got an application which will be using RabbitMQ and have several different queues for passing messages between tiers.

最初,我打算使用多个直接交换,每种消息类型使用一次,但是看起来像使用不同路由键绑定与队列进行单个主题交换将实现相同的目的.

Initially, I was planning to use multiple direct exchanges, with one for each message type, but it looks like having a single topic exchange with queues using different routing key bindings will achieve the same thing.

进行单个交换似乎也比较容易维护,但是我想知道以一种方式进行交换是否有任何好处(如果有的话)?

Having a single exchange also seems like it would be a bit easier to maintain, but I was wondering if there is any benefit (if any) of doing it one way over the other?

选项1,使用多个直接交换:

Option 1, using multiple direct exchanges:

ExchangeA (type: direct)
-QueueA

ExchangeB (type: direct)
-QueueB

ExchangeC (type: direct)
-QueueC

选择2,使用单个主题交流:

Option 2, using single topic exchange:

Exchange (type: topic)
-QueueA  (receives messages from exchange with routing key of "TypeA")
-QueueB  (receives messages from exchange with routing key of "TypeB")
-QueueC  (receives messages from exchange with routing key of "TypeC")

推荐答案

假设两个模型都被认为是使用一个代理运行的,那么我看不出有什么区别.

Assuming both models are being considered to be implemented using one broker running, there's little difference that I can see.

选项2在现实世界中似乎更常见于解决此类路由问题(至少以我的轶事而言),而这正是主题交易所要解决的挑战.

Option 2 seems more common in the real world for solving this kind of routing problem (at least in my anecdotal experience) and it's exactly the challenge that Topic Exchanges exist to solve.

您可能会遇到的唯一区别与路由速度有关.我不确定在RabbitMQ中Exchange路由(始终基于完全匹​​配的字符串)是否比Topic Exchange中使用的路由键技术(可以包括#*这样的通配符)更快.我的直觉是交换歧视会更快,但是您可以尝试自己找出答案,或者尝试与RabbitMQ团队联系以询问他们.

The only difference that you might encounter would relate to routing speed. I don't know for sure if Exchange routing (based always on an exact string match) is faster in RabbitMQ when compared to the routing key technique used in Topic Exchanges (which can include wildcards like # and *). My hunch would be that Exchange discrimination would be faster, but you could experiment for yourself to find out, or try contacting the RabbitMQ team to ask them.

最后,如果您选择选项1,最终导致很多队列,那么您的Exchange将成比例地激增.这听起来像是维护方面的麻烦.如果您只有几个队列,那么问题就不大了.

Finally, if you go with option 1 end up with lots of queues then you'll have a proportional proliferation of Exchanges. That sounds like a maintenance headache. If you'll only have a handful of queues then it won't be too much of an issue.

这篇关于RabbitMQ中的主题交流与直接交流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆