This topic introduces different types of exchanges to help you better understand and select exchanges in ApsaraMQ for RabbitMQ.
What is an exchange?
An exchange is a message routing agent of ApsaraMQ for RabbitMQ. When a producer sends a message to the ApsaraMQ for RabbitMQ broker, the message is first sent to an exchange. Then, the exchange routes the message to one or more queues. An exchange routes messages based on binding keys, routing keys, and headers attributes.
Direct exchanges
- Routing rule
A direct exchange routes a message to the queue whose binding key exactly matches the routing key of the message.
- Scenarios
A direct exchange can be used in scenarios where messages are distinguished by simple strings. In most cases, a direct exchange is used for the unicast routing of messages.
- Example
The following figure shows an example.
Message Routing key Binding key Queue Message A img.create
img.create
Queue A Message B img.log
img.log
Queue B
Topic exchanges
- Routing rule
A topic exchange routes a message to the queues whose binding keys partially match the routing key of the message. An asterisk (*) or a number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) indicates a word. Example: cn. A number sign (#) indicates zero, one, or more words that are separated by periods (.). Example: cn.zj.hz.
- Scenarios
A topic exchange can be used in scenarios where messages are distinguished by wildcards. In most cases, a topic exchange is used for the multicast routing of messages. For example, you can use a topic exchange to distribute geographic location data.
- Example
The following figure shows an example.
Message Routing key Binding key Queue Message A files.cn.hz
files.cn.hz.#
Queue A Message B files.cn.hz.store
files.cn.hz.#
files.cn.*.store
- Queue A
- Queue B
Message C files.cn.sz.store
files.cn.*.store
Queue B
Fanout exchanges
- Routing rule
A fanout exchange routes messages to all of the queues that are bound to it. Routing keys and binding keys are ignored.
- Scenarios
A fanout exchange is ideal for the broadcast routing of messages. For example, you can use a fanout exchange in a distribution system to broadcast status or configuration updates.
- Example
The following figure shows an example.
Message Routing key Binding key Queue Message A img.create
files.cn.hz.#
queue.msgText
- Queue A
- Queue B
Message B queue.msgMap
files.cn.hz.#
queue.msgText
- Queue A
- Queue B
Message C cn.hz.topic.alarm
files.cn.hz.#
queue.msgText
- Queue A
- Queue B
Headers exchanges
- Routing rule
A headers exchange can be considered as a special direct exchange. It works in a similar way to direct exchanges. The difference lies in that a headers exchange routes messages to queues based on headers attributes instead of routing keys. When you bind a headers exchange to a queue, you must configure binding attributes in the key-value format for the binding. When you send a message to a headers exchange, you must configure headers attributes in the key-value format for the message. After a headers exchange receives a message, it routes the message based on the matching between the headers attributes of the message and binding attributes of the bound queues.
A special binding attribute is used to determine the matching method. This binding attribute is x-match. The value of x-match can be set to all or any:
- all: A headers exchange routes a message to a queue only if all binding attributes of the queue except for x-match match the headers attributes of the message.
- any: A headers exchange routes a message to a queue if one or more binding attributes of the queue except for x-match match the headers attributes of the message.
A headers attribute is considered to match a binding attribute in the following scenarios:
- The key and value of the headers attribute are the same as those of the binding attribute.
- The key of the headers attribute is the same as that of the binding attribute, but the value of the binding attribute is empty.
- Scenarios
A headers exchange can be used in scenarios where messages are distinguished by multiple headers attributes. In most cases, a headers exchange is used for the multicast routing of messages. For example, you can use a headers exchange to distribute news that is classified or tagged.
- Example
The following figure shows an example.
Message Headers attribute Binding attribute Queue Message A type=read resource=group
type=read resource=group x-match=all
type=read resource=topic x-match=any
- Queue A
- Queue B
Message B type=read
type=read resource=topic x-match=any
Queue B Message C type=write resource=topic
type=read resource=topic x-match=any
Queue B
JMS queue exchanges
In addition to AMQP exchanges, ApsaraMQ for RabbitMQ also supports Java Message Service (JMS) queue exchanges. A JMS queue exchange functions as a direct exchange. ApsaraMQ for RabbitMQ uses JMS queue exchanges to support JMS queues.
- Routing rule
A JMS queue exchange routes a message to the queue whose binding key exactly matches the routing key of the message.
- Scenarios
A JMS queue exchange can be used in scenarios in which messages are distinguished by simple string. In most cases, a JMS queue exchange is used for the unicast routing of messages.
- Example
The following figure shows an example.
Message Routing key Binding key Queue Message A queue.msgText
queue.msgText
Queue A Message B queue.msgMap
queue.msgMap
Queue B Message C queue.msgObj
queue.msgObj
Queue C
JMS topic exchanges
In addition to AMQP exchanges, ApsaraMQ for RabbitMQ also supports JMS topic exchanges. In essence, a JMS topic exchange is a topic exchange. ApsaraMQ for RabbitMQ uses JMS topic exchanges to support JMS topics.
- Routing rule
A JMS topic exchange routes a message to the queues whose binding keys partially match the routing key of the message. An asterisk (*) or a number sign (#) is used as a wildcard in a binding key to support partial match. An asterisk (*) indicates a word. Example: cn. A number sign (#) indicates zero, one, or more words that are separated by periods (.). Example: cn.zj.hz.
- Scenarios
A JMS topic exchange can be used in scenarios where messages are distinguished by wildcards. In most cases, a JMS topic exchange is used for the multicast routing of messages. For example, you can use a JMS topic exchange to distribute geographic location data.
- Example
The following figure shows an example.
Message Binding key Routing key Queue Message A cn.hz.instance
cn.hz.instance.#
Queue A Message B cn.hz.instance.alarm
cn.hz.instance.#
cn.hz.*.alarm
- Queue A
- Queue B
Message C cn.hz.topic.alarm
cn.hz.*.alarm
Queue B
x-delayed-message exchanges
ApsaraMQ for RabbitMQ is also compatible with x-delayed-message exchanges of open source RabbitMQ. Open source RabbitMQ allows you to use x-delayed-message exchanges by installing the delayed message plug-in. To use x-delayed-message exchanges in Message Queue for RabbitMQ, you do not need to install the plug-in. You need only to declare an exchange of the x-delayed-message type and set the x-delay header attribute of the message to specify the period of time after which the message will be delivered. The period of time is in milliseconds. The message will be delivered to the corresponding queue after the period of time specified by the x-delay header attribute.
- Routing ruleAn x-delayed-message exchange determines the routing rule based on the exchange type specified by the x-delayed-type extended attribute. The following types of exchanges support the x-delayed-message type:
- Scenarios
An x-delayed-message exchange can be used in scenarios where messages need to be delivered after a specified delay. For more information, see Delayed messages.
- Example
An x-delayed-message exchange routes messages based on the routing rule of the exchange type specified by the x-delayed-type extended attribute. The following figure shows an example on x-delayed-message exchanges whose x-delayed-type extended attribute is set to direct.
Message x-delay Routing key Binding key Queue Message A 3000 img.create
img.create
Queue A Message B 4000 img.log
img.log
Queue B The following items describe how the x-delayed-message exchange routes Message A and Message B:- After the x-delayed-message exchange receives Message A, the x-delayed-message exchange delivers Message A to Queue A after 3,000 milliseconds.
- After the x-delayed-message exchange receives Message B, the x-delayed-message exchange delivers Message B to Queue B after 4,000 milliseconds.
x-consistent-hash exchanges
ApsaraMQ for RabbitMQ is also compatible with x-consistent-hash exchanges that are provided by open source RabbitMQ. To use x-consistent-hash exchanges in Message Queue for RabbitMQ, you do not need to install the plug-in. You need to only declare an exchange of the x-consistent-hash type.
- Routing rules
- x-consistent-hash exchanges allow you to route messages based on the routing key or header value. After an x-consistent-hash exchange receives a message, the exchange performs hash calculation based on the routing key or header value of the message to determine to which bound queue the message is routed.
- The hash value that is calculated by using the same routing key or header value is the same. As a result, messages that use the same routing key or header value are routed to the same queue.
- If you route messages based on the header value, you must use the hash-header parameter to declare the exchange and the header that is to be used. Meanwhile, messages that are published by producers must contain the header that is specified by the hash-header parameter. If the specified header is not contained, all messages are routed to the same random queue.
- If both the routing key and the hash-header parameter are specified, the hash calculation is performed based on the value of the hash-header parameter.
- When you bind an x-consistent-hash exchange to a queue, you must specify a binding key whose value ranges from 1 to 20 to indicate the weight of the queue. A larger value specifies a higher weight. A higher weight indicates that the exchange receives more messages.
- If the value of a binding key is greater than 20, the weight is considered to be 20.
- If multiple bindings exist, only the first binding whose value is a positive integer is valid. When you change the weight of a queue, you must first delete the existing bindings.
- Scenarios
x-consistent-hash exchanges can be used in scenarios in which messages are categorized by weight.
- Example
An x-consistent-hash exchange routes messages to queues based on the hash value that is calculated by using a specified method. The weight of the queues that are bound to the exchange varies, and messages are routed to queues based on the weight. The following figure shows an example on x-consistent-hash exchanges whose routing rule is calculated based on a routing key.
Message Routing key Message A cn.hz.1 Message B cn.hz.2 Message C cn.hz.3 Queue Binding key Queue A 1 Queue B 2 The following items describe how an x-delayed-message exchange routes Message A, Message B, and Message C:- After Message A, Message B, and Message C arrive at the exchange, the exchange delivers the messages to queues by using the hash value that is calculated based on the routing key.
- The exchange is bound to Queue A and Queue B. The weight of Queue A is 1, and the weight of Queue B is 2. In this case, Message A, Message B, and Message C are routed to Queue A and Queue B based on a weight ratio of 1:2.