为什么我不应该对所有事情都使用 rabbitmq 主题交换? [英] Why shouldn't I use rabbitmq topic exchanges for everything?

查看:20
本文介绍了为什么我不应该对所有事情都使用 rabbitmq 主题交换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎工作模式、扇出和过滤主题都可以通过主题交换来实现.为什么我会改用直接或扇出交换?

It seems like the worker pattern, fanout, and filtered topics can all be implemented with topic exchanges. Why would I ever use a direct or fanout exchange instead?

我们希望将我们组织中发现的常见模式编入一个库中,该库抽象了 amqp 的无限灵活性(命名约定、默认为持久、发送通用标头、到期等).我们应该利用不同的交换类型还是使用主题实现所有模式?为什么?

We would like to codify common patterns found in our org in a library that abstracts the infinite flexibility of amqp (naming conventions, defaulting to durable, sending common headers, expirations etc.). Should we leverage the different exchange types or implement all patterns with topics; why?

(我们在 Java 中通过 spring boot、golang 和 php 有消费者/发布者)

(We have consumers/publishers in Java via spring boot, in golang, and in php)

推荐答案

为什么我不应该对所有事情都使用 rabbitmq 主题交换?

Why shouldn't I use rabbitmq topic exchanges for everything?

没有人说你不应该.如果它对您有用,请尽情享受吧!

nothing says you shouldn't. if it works for you, have fun with it!

来自 我的 RabbitMQ:布局电子书:

关于交换类型的真相是没有主"类型——没有一种可以用作默认值,或者大多数时候.当然,给定的应用程序可能会通过单个交换或交换类型来满足其需求,但情况并非总是如此.即使在单个系统中,也可能需要以不同的方式路由消息并将它们最终放在同一个队列中.

The truth about exchange types is that there is no "master" type - not one to be used as a default, or most of the time. Sure, a given application may have its needs served by a single exchange or exchange type, but this will not always be the case. Even with in a single system, there may be a need to route messages in different ways and have them end up in the same queue.

如果您发现自己处于选择上述交换类型之一将排除您的消息所需的一组路由行为的情况,请使用多个交换.您可以从任意数量的交换路由到单个队列,或从单个交换路由到任意数量的队列.

If you find yourself in a situation where choosing one of the above exchange types will preclude a needed set of routing behaviors for your messages, use more than one exchange. You can route from any number of exchanges to a single queue, or from a single exchange to any number of queues.

不要将您的系统路由需求限制为任何给定消息或目的地的单一交换类型.根据需要利用每一项.

Don’t limit your systems routing needs to a single exchange type for any given message or destination. Take advantage of each one, as needed.

关于不同的交换类型(同样来自我的电子书)

On the different exchange types (again, from my ebook)

直接:

直接交换允许您将队列绑定到具有匹配的路由键的交换,区分大小写.这可能是它们中最直接的交换,因为没有模式匹配或其他需要跟踪和考虑的行为.如果消息中的路由键与交换中绑定的路由键匹配,则消息被路由.

A direct exchange allows you to bind a queue to an exchange with a routing key that is matched, case sensitively. This may be the most straight-forward exchange of them all, as there is no pattern matching or other behavior to track and consider. If a routing key from a message matches the routing key of a binding in the exchange, the message is routed.

扇出:

扇出交换允许您向绑定到交换的每个队列广播消息,而无法过滤哪些队列接收消息.如果队列绑定到扇出交换,它将接收通过该交换发布的任何消息.

Fanout exchanges allow you to broadcast a message to every queue bound to an exchange, with no way to filter which queues receive the message. If a queue is bound to a fanout exchange, it will receive any message published through that exchange.

和话题交流:

主题交换类似于直接交换,因为它使用路由键.但是,与直接交换不同,路由键不必完全匹配才能路由消息.主题交换允许您在绑定中指定主题"(路由键)的通配符匹配.这使您可以从多个路由键接收消息,并提供其他交换类型所没有的灵活性.

A topic exchange is similar to a direct exchange in that it uses routing keys. Unlike a direct exchange, though, the routing keys do not have to match exactly for a message to be routed. Topic exchanges allow you to specify wild-card matching of "topics" (routing keys) in your bindings. This lets you receive messages from more than one routing key and provides a level of flexibility not found in the other exchange types.

这篇关于为什么我不应该对所有事情都使用 rabbitmq 主题交换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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