An exchange receives messages from producers and routes them to one or more queues based on routing rules. Each exchange type uses a different routing strategy -- a direct exchange matches routing keys exactly, while a fanout exchange broadcasts to all bound queues. If no queue matches, the exchange discards the message.
Exchange types
Choose the exchange type that matches your routing needs.
Type | Routing behavior | Use when |
direct | Routes messages to queues whose binding key exactly matches the message routing key. | You need one-to-one message delivery based on an exact key, such as routing |
topic | Routes messages to queues based on pattern matching between the message routing key and binding patterns. Supports | You need flexible routing, such as routing |
fanout | Routes all messages to every bound queue, regardless of routing keys. | You need to broadcast messages to multiple consumers, such as pushing notifications to all connected clients. |
headers | Routes messages based on message header attributes instead of routing keys. Uses the | You need routing based on multiple attributes rather than a single routing key. |
x-delayed-message | Holds messages for a specified duration before routing them. Set the delay in milliseconds using the | You need to delay message delivery, such as sending reminder notifications 30 minutes after an event. |
x-consistent-hash | Routes messages to queues using consistent hashing on routing keys or header values, distributing load across multiple queues. | You need even message distribution across a set of consumer queues for parallel processing. |
Create an exchange
Prerequisites
Before you begin, make sure that you have:
An ApsaraMQ for RabbitMQ instance
A vhost in which to create the exchange
Procedure
Log on to the ApsaraMQ for RabbitMQ console. In the left-side navigation pane, click Instances.
In the top navigation bar, select the region of your instance. Click the instance name.
In the left-side navigation pane, click Exchanges.
On the Exchanges page, click the Change button next to vhost. From the drop-down list, select the target vhost. Then, click Create Exchange.
In the Create Exchange panel, configure the following parameters and click OK.
Parameter
Description
Exchange Name
The exchange name. The prefix
amq.is reserved and cannot be used. For example,amq.testis not a valid name.Type
The exchange type.
x-delayed-type
Required when Type is
x-delayed-message. Specifies the underlying routing strategy (direct, topic, fanout, headers, or x-jms-topic).Hash value
Required when Type is
x-consistent-hash. Specifies the hash input source: RoutingKey or Header Value.hash-header
Required when Hash value is set to Header Value. Specifies the header key to use as the hash input.
Internal
Whether the exchange is internal. Default: No. Set to Yes to create an internal exchange that receives messages only from other exchanges (not from producers directly). Set to No for a standard exchange that binds to queues.
Bind an exchange to a queue
Bind an exchange to a queue to define where the exchange routes messages.
On the Exchanges page, find the exchange and click Bound as Source in the Actions column.
On the Bound as Source tab of the Exchange Details page, click Add Binding.
In the Add Binding panel, set Type of Destination to Bind to Queue, select a queue from the Destination to Bind drop-down list, configure the Routing Key, and click OK.
For x-consistent-hash exchanges, the Routing Key value specifies the queue weight (integer from 1 to 20). To change the weight, delete the existing binding and create a new one.
If an exchange is bound to the same queue multiple times, only the first matching binding takes effect.
Bind an exchange to another exchange
Forward messages to another exchange (source binding)
Configure the current exchange as a source to forward its messages to a downstream exchange based on routing rules.
On the Exchanges page, find the exchange and click Bound as Source in the Actions column.
On the Bound as Source tab, click Add Binding.
In the Add Binding panel, set Type of Destination to Bind to Exchange, select the destination exchange from the Destination to Bind drop-down list, configure the Routing Key, and click OK.
Receive messages from another exchange (destination binding)
Configure the current exchange as a destination to receive messages from an upstream exchange.
On the Exchanges page, find the exchange and click Details in the Actions column.
On the Exchange Details page, click the Bound as Destination tab. Click Add Binding.
In the Add Binding panel, select the source exchange from the Source Exchange drop-down list, enter a routing key, keep the default values for Type of Destination to Bind and Destination Exchange, and click OK.
Send a message
After binding an exchange, you can send messages from the exchange to bound queues or from a destination exchange to a source exchange in the ApsaraMQ for RabbitMQ console.
On the Exchanges page, find the exchange and click Details in the Actions column.
In the upper-right corner of the Exchange Details page, click Send Message.
In the Send Message panel, enter the Routing Key, select a value for mandatory (true or false), enter the Message ID and Message Body, then click OK.
View exchange statistics
View exchange metrics to monitor message routing and identify issues.
On the Exchanges page, find the exchange and click Details in the Actions column.
On the Exchange Details page, click the Dashboard tab.
The available metrics depend on your instance edition:
Instance edition | Monitoring source | Details |
Enterprise Edition, Enterprise Platinum Edition | Managed Service for Prometheus and Managed Service for Grafana | See Dashboard. |
Professional Edition, Standard Edition | CloudMonitor |
Standard Edition instances are no longer available for purchase. Existing Standard Edition instances remain functional.
Delete an exchange
After you delete an exchange, producers can no longer send messages to the exchange, and the exchange can no longer route messages to one or more queues or discard messages. Proceed with caution.
Exchanges of the
headerstype cannot be deleted.Built-in vhost exchanges (
amq.direct,amq.topic,amq.fanout,amq.headers, andamq.match) cannot be deleted.
On the Exchanges page, click the name of the exchange to delete.
In the upper-right corner of the Exchange Details page, click Delete.
In the Note confirmation dialog, click OK.
References
For more information about exchange concepts and architecture, see Exchange.