Exchanges
Direct, fanout, topic, headers, x-delayed-message, and x-consistent-hash exchanges are available.
Direct Exchange
How It Works
- A queue is bound to a direct exchange with a routing key.
- The direct exchange routes a received message to the bound queue whose routing key is matched.
Routing
Based on a routing key matching
Scenario
Unicast routing
Example
As shown in this chart, Message A will be sent to Queue 1 and Queue 2. Message B will be sent to Queue 3.
Fanout Exchange
How It Works
A fanout exchange bound with multiple queues routes received messages to each queue. Fanout exchanges forward messages faster than other exchanges.
Routing
The fanout exchange delivers messages to all bound queues.
Scenario
Broadcast routing
Example
As shown in this chart, Message A will be sent to Queue 1 and Queue 2.
Topic Exchange
How It Works
- A queue is bound to a topic exchange with a routing key that includes a wildcard.
- The topic exchange routes a received message to the queue if the message's routing key wildcard is matched.
Supported wildcards are stars (*) and hashes (#). Separate wildcards and words by periods (.), for example, test.#.
- * matches one word.
- # matches zero or more words.
Routing
Based on a routing key wildcard matching
Scenario
Multicast routing
Example
As shown in this chart, Message A will be sent to Queue 1 and Queue 2, Message B to Queue 1, Queue 2, and Queue 3, and Message C to Queue 3.
Headers Exchange
How It Works
- A queue is bound to a headers exchange with a binding expressed in a key-value pair.
- The headers exchange routes a message to the queue if the binding matches the message's key-value header.
The matching algorithm uses a specific binding key-value pair, which is x-match. Values:
- all: Messages are routed only when all header pairs match.
- any: Messages are routed when any header pair matches.
Routing
Based on matching between key-value pairs in the message headers and the binding (a key-value pair)
Example
As shown in this chart, Message A will be sent to Queue 1 and Queue 2, and Message B to Queue 2.
x-delayed-message Exchange
How It Works
- An x-delayed-message exchange is created with one of these exchange types: Direct Exchange, Fanout Exchange, Topic Exchange, and Headers Exchange. The exchange type specifies the routing rules.
- A queue is bound to the x-delayed-message exchange.
- A message with the header attribute x-delay is sent to the x-delayed-message exchange. x-delay specifies the message delivery schedule, in milliseconds.
- The x-delayed-message exchange does not deliver the message immediately after receiving it. The message will be routed as scheduled based on the exchange type-specified routing rules.
Routing
Messages are routed based on the rules of the exchange type set in the x-delayed-message exchange creation.
Scenario
x-delayed-message exchanges are applicable to scheduled message delivery.
Example
This example uses a direct exchange.
As shown in the preceding figure, the x-delayed-message exchange receives message A, waits 100 ms, and sends it to queue 1 and queue 2.
x-consistent-hash Exchange
How It Works
- A queue is bound to an x-consistent-hash exchange with a routing key. The key must be a positive integer. A routing key indicates the weight of the queue. The larger the key, the heavier the weight, which means that the queue receives more messages.
- A message with a routing key is sent to the x-consistent-hash exchange. The exchange calculates a hash value based on the key, and distributes the message to the hashed queue.
Routing
x-consistent-hash exchange calculates a hash value based on a routing key, and routes a message to the hashed queue.
Example
As shown in the preceding figure, x-consistent-hash exchange receives messages A and B with routing keys, calculates hash values based on the keys, and routes the messages to the hashed queues. Queue 1 weighs 1 and queue 2 weighs 2. In this case, queue 2 receives twice as many messages as queue 1.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot