This document describes the concept, types, and usage of exchange in TDMQ for RabbitMQ.
Concept
Exchange is a message routing agent in TDMQ for RabbitMQ. A producer sends a message to an exchange, which then routes the message to one or more queues based on its attributes or content (or discards it). Then, a consumer pulls it from such queues for consumption.
TDMQ for RabbitMQ currently supports four types of exchange: Direct, Fanout, Topic, and Header.
Direct: a direct exchange will route messages to the queue whose binding key exactly matches the routing key.
Fanout: a fanout exchange will route messages to all queues bound to it.
Topic: a topic exchange supports multi-condition match and fuzzy match; that is, it will route messages to the queues bound to it by using routing key pattern match and string comparison.
Header: A header exchange has nothing to do with routing key and matches messages by the Headers
attribute. When binding a queue to a header exchange, declare a map key-value pair to implement the binding.
Direct Exchange
Routing rule: a direct exchange will route messages to the queue whose binding key exactly matches the routing key.
Use case: this type of exchange is suitable for filtering messages by simple character identifiers and is often used for unicast routing.
Example:
Fanout Exchange
Routing rule: this type of exchange will route messages to all queues bound to it.
Use case: this type of exchange is suitable for broadcast message scenarios. For example, a distribution system can use a fanout exchange to broadcast various status and configuration updates.
Example
Topic Exchange
Routing rule: this type of exchange supports multi-condition match and fuzzy match; that is, it will route messages to the queues bound to it by using routing key pattern match and string comparison.
The wildcards supported by topic exchanges include asterisk "*" and pound sign "#".
"*" represents a word, such as sh
.
"#" represents zero, one, or more words separated by period ".", such as cn.hz
.
Use case
This type of exchange is often used for multicast routing. For example, you can use a topic exchange to distribute data about specific geographic locations.
Example
|
Message 1 | cn.hz
| cn.hz.#
| Queue 1 |
Message 2 | cn.hz.store
| cn.hz.# , cn.*.store
| Queue 1, Queue 2 |
Message 3 | cn.sz.store
| cn.*.store
| Queue 2 |
Header Exchange
A header exchange has nothing to do with routing key and matches messages by the Headers
attribute. When binding a queue to a header exchange, declare a map key-value pair to implement the binding. When a message is sent to RabbitMQ, the Headers
attribute of the message will be obtained to match the key-value pair specified during exchange binding, and the message will be routed to the queue only if there is a full match.
The x-match rule has two types:
x-match = all: A message can be received only if all key-value pairs are matched.
x-match = any: A message can be received as long as any key-value pair is matched.
|
| key1=value1
key2=value2
| x-match = any
key1=value1
key2=value2
| Queue 1 |
| key1=value1
key2=value2
key3=value3
| x-match = any
key1=value1
key2=value2
x-match = all
key1=value1
key2=value2`
key3=value3
| Queue 1, Queue 2 |
Was this page helpful?