RabbitMQ各种交换机类型Exchange Types介绍

本文详细介绍了RabbitMQ中的四种交换机类型:Direct、Fanout、Topic和Headers,包括它们的工作原理、特点及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最新版本的RabbitMQ有四种交换机类型,分别是Direct exchange、Fanout exchange、Topic exchange、Headers exchange。


Direct Exchange – 处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键 “dog”,则只有被标记为“dog”的消息才被转发,不会转发dog.puppy,也不会转发dog.guard,只会转发dog。 

 


Fanout Exchange – 不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。Fanout交换机转发消息是最快的。 

 


Topic Exchange – 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*” 只会匹配到“audit.irs”。我在RedHat的朋友做了一张不错的图,来表明topic交换机是如何工作的: 

 

注:这种情况下队列会收到所有路由器中符合topic规则的消息



Headers 类型的Exchanges是不处理路由键的,而是根据发送的消息内容中的headers属性进行匹配。在绑定Queue与Exchange时指定一组键值对;当消息发送到RabbitMQ时会取到该消息的headers与Exchange绑定时指定的键值对进行匹配;如果完全匹配则消息会路由到该队列,否则不会路由到该队列。headers属性是一个键值对,可以是Hashtable,键值对的值可以是任何类型。而fanout,direct,topic 的路由键都需要要字符串形式的。

匹配规则x-match有下列两种类型:

  • x-match = all   :表示所有的键值对都匹配才能接受到消息
  • x-match = any :表示只要有键值对匹配就能接受到消息

不过headers比较少用到


Headers exchange – 还没有仔细研究过,复制点官方的介绍吧。

A headers exchange is designed to for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute. A message is considered matching if the value of the header equals the value specified upon binding.

It is possible to bind a queue to a headers exchange using more than one header for matching. In this case, the broker needs one more piece of information from the application developer, namely, should it consider messages with any of the headers matching, or all of them? This is what the "x-match" binding argument is for. When the "x-match" argument is set to "any", just one matching header value is sufficient. Alternatively, setting "x-match" to "all" mandates that all the values must match.

Headers exchanges can be looked upon as "direct exchanges on steroids". Because they route based on header values, they can be used as direct exchanges where the routing key does not have to be a string; it could be an integer or a hash (dictionary) for example.


其实除了上面四种以外还有一种Default Exchange,它是一种特别的Direct Exchange。

当你手动创建一个队列时,后台会自动将这个队列绑定到一个名称为空的Direct类型交换机上,绑定路由名称与队列名称相同。有了这个默认的交换机和绑定,我们就可以像其他轻量级的队列,如Redis那样,直接操作队列来处理消息。不过只是看起来是,实际上在RabbitMQ里直接操作是不可能的。消息始终都是先发送到交换机,由交换级经过路由传送给队列,消费者再从队列中获取消息的。不过由于这个默认交换机和路由的关系,使我们只关心队列这一层即可,这个比较适合做一些简单的应用,毕竟没有发挥RabbitMQ的最大功能,如果都用这种方式去使用的话就真是杀鸡用宰牛刀了。

参考

http://melin.iteye.com/blog/691265


### RabbitMQ 中的不同类型交换机及其功能 #### 1. 直接交换机 (Direct Exchange) 直接交换机按照指定的路由键将消息转发到相应的队列。如果一个队列绑定此交换机并指定了特定的路由键,则只有带有相同路由键的消息会被传递给该队列[^1]。 ```python import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.exchange_declare(exchange='direct_logs', exchange_type='direct') severity = 'error' message = 'This is an error message' channel.basic_publish( exchange='direct_logs', routing_key=severity, body=message) ``` #### 2. 扇形交换机 (Fanout Exchange) 扇形交换机会忽略路由键,会把收到的信息广播至所有已知的队列中。这意味着任何连接到这个交换机上的队列都会获得一份副本[^2]。 ```python channel.exchange_declare(exchange='logs', exchange_type='fanout') result = channel.queue_declare(queue='', exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange='logs', queue=queue_name) ``` #### 3. 主题交换机 (Topic Exchange) 主题交换机允许更复杂的模式匹配来决定哪些消息应该被发送到哪个队列。通过通配符机制可以定义灵活的路由规则。 ```python channel.exchange_declare(exchange='topic_logs', exchange_type='topic') routing_key = "kern.critical" message = "A critical kernel error" channel.basic_publish( exchange='topic_logs', routing_key=routing_key, body=message) ``` #### 4. 头部交换机 (Headers Exchange) 头部交换机不依赖于路由键而是依据消息头属性来进行过滤。当消息到达时,它会检查这些预设条件并将符合条件的消息分发出去。 ```python headers = {"x-match": "all", "header-key": "value"} properties = pika.BasicProperties(headers=headers) channel.basic_publish( exchange="headers_exchange", routing_key="", properties=properties, body="Message with headers") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值