RabbitMQ 有几种 exchange 类型,它们决定了消息如何路由到队列 — direct、topic、fanout 和 headers。理解它们对于 RabbitMQ 的灵活路由至关重要,这使得不同的消息传递模式成为可能。
交换机类型
text
DIRECT → routes by an EXACT routing key match:
→ message routing key = queue binding key → delivered to that queue
→ for: routing to specific queues by a key (e.g. route by severity, type)
FANOUT → broadcasts to ALL bound queues (ignores routing key):
→ every queue bound to the exchange gets the message → PUB/SUB (one-to-many)
→ for: broadcasting events to all interested consumers
TOPIC → routes by PATTERN matching on the routing key (wildcards):
→ routing keys like "order.created", "order.*", "*.error" → flexible pattern routing
→ for: routing by topics/categories with wildcards (flexible pub/sub)
HEADERS → routes by message HEADER attributes (not the routing key) → less common
