An exchange is used to route a message that is received from a producer to one or more queues or to discard the message. An exchange routes a message to queues based on the routing key. Different routing rules are used for different types of exchanges. This topic describes how to create an exchange, view the statistics of an exchange, bind an exchange to a queue, use a specific exchange as the source exchange, use a specific exchange as the destination exchange, and send messages in an exchange to a bound queue in the ApsaraMQ for RabbitMQ console.
Create an exchange
Log on to the ApsaraMQ for RabbitMQ console. In the left-side navigation pane, click Instances.
In the top navigation bar of the Instances page, select the region where the instance that you want to manage resides. Then, in the instance list, click the name of the instance that you want to manage.
In the left-side navigation pane, click Exchanges.
On the Exchanges page, click the drop-down arrow next to vhost. From the Change drop-down list, select the vhost in which you want to create an exchange. Then, click Create Exchange.
In the Create Exchange panel, configure the Exchange Name, Type, and Internal parameters. Then, click OK. The following table describes the parameters.
Parameter
Description
Exchange Name
The exchange name. amq. is a reserved field and cannot be used as the prefix of an exchange name. For example, you cannot use amq.test as the name of an exchange.
Type
The exchange type. Valid values:
direct: An exchange of this type routes a message to the queue whose routing key is the same as the routing key of the message.
topic: An exchange of this type is similar to an exchange of the direct type. However, the routing rule for a topic exchange is less demanding than that for a direct exchange. A topic exchange routes a message to one or more bound queues based on the result of fuzzy match or multi-condition match between the routing key of the message and the routing keys that are used to bind the queues to the exchange.
fanout: An exchange of this type routes all received messages to all queues that are bound to the exchange. A fanout exchange works in the similar way to the broadcasting feature.
headers: An exchange of this type is similar to an exchange of the direct type. The only difference between a headers exchange and a direct exchange is that a headers exchange routes messages based on the 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. When you send a message to a headers exchange, you must configure the headers attributes in the key-value format for the message. After a headers exchange receives a message, the exchange routes the message based on the matching result between the headers attributes of the message and the binding attributes of the bound queue.
x-delayed-message: You can declare an exchange of this type and configure the x-delay header attribute of a message to specify the period of time after which the message is sent. The period of time is in milliseconds. The message is sent to the corresponding queue after the period of time specified by the x-delay header attribute elapses. The exchange routes messages based on the routing rule that corresponds to the exchange type specified by x-delayed-type.
x-consistent-hash: An exchange of this type allows you to perform hash calculation on routing keys or header values and use consistent hashing to route a message to different queues.
x-delayed-type
If you set the Type parameter to x-delayed-message, you must configure this parameter to specify the routing rule for the exchange.
Hash value
If you set the Type parameter to x-consistent-hash, you must configure this parameter to specify the type of the input value for hash calculation. Valid values:
RoutingKey
Header Value: If you use headers as the input value for hash calculation, you must specify the value for the hash-header parameter.
hash-header
If you set the Type parameter to x-consistent-hash and the Hash value parameter to Header Value, you must configure this parameter as the input value for hash calculation.
Internal
Specifies whether the exchange is an internal exchange. Default value: No. Valid values:
Yes: The exchange is an internal exchange and is bound to another exchange.
No: The exchange is not an internal exchange and is bound to a queue.
View the statistics of an exchange
You can view various metrics to identify and locate issues at the earliest opportunity.
On the Exchanges page, find the exchange whose statistics you want to view and click Details in the Actions column.
On the Exchange Details page, click the Monitoring and Alerting or Dashboard tab.
The statistics of an exchange vary based on the instance edition. The following items describe the details:
Enterprise Edition and Enterprise Platinum Edition: You can view the metrics provided by Managed Service for Prometheus and Managed Service for Grafana. For information about the dashboard and metrics displayed on the dashboard, see Dashboard.
Standard Edition and Professional Edition: You can view the monitoring data provided by CloudMonitor. For information about metrics provided by CloudMonitor, see Monitoring and alerting.
NoteApsaraMQ for RabbitMQ no longer allows you to purchase Standard Edition instances. You can continue using existing Standard Edition instances.
Bind an exchange to a queue
You can bind an exchange to a queue to determine the queue to which the exchange routes messages.
On the Exchanges page, find the exchange that you want to manage 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 the Type of Destination to Bind parameter to Queue, select a queue from the Destination to Bind drop-down list, configure the Routing Key parameter, and then click OK.
NoteIf the type of the exchange to which you want to bind a queue is x-consistent-hash, the Routing Key parameter specifies the weight of the queue. The value of the Routing Key parameter can be an integer from 1 to 20.
If an exchange is bound to a queue multiple times, only the first binding that meets the conditions takes effect.
To change the weight of the queue to which an exchange is bound, you must delete the existing binding before you add a new binding.
Use a specific exchange as the source exchange
You can use a specific exchange as the source exchange and bind the exchange to another exchange. This way, you can forward messages in the exchange to another exchange based on routing rules.
On the Exchanges page, find the exchange that you want to manage 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 the Type of Destination to Bind parameter to Exchange, select an exchange from the Destination to Bind drop-down list, configure the Routing Key parameter, and then click OK.
Use a specific exchange as the destination exchange
You can use a specific exchange as the destination exchange and bind another exchange to the exchange. This way, you can forward messages in another exchange to the exchange based on routing rules.
On the Exchanges page, find the exchange that you want to manage and click Details in the Actions column.
On the Exchange Details page, click the Bound as Destination tab. Then, click Add Binding.
In the Add Binding panel, select an exchange from the Source Exchange drop-down list, enter a routing key in the Routing Key field, use the default values for Type of Destination to Bind and Destination Exchange, and then click OK.
Send a message
After you add a binding to an exchange, you can send messages in the exchange to the queue to which the exchange is bound or send messages in the destination exchange to the source exchange in the ApsaraMQ for RabbitMQ console.
On the Exchanges page, find the exchange that you want to manage and click Details in the Actions column.
On the Exchange Details page, click the Bound as Source or Bound as Destination tab.
In the Bound as Source or Bound as Destination list, select the specified binding and click Send Message in the Actions column.
In the Send Message panel, configure the Message ID and Message Body parameters. Then, click OK.
Delete an exchange
After you delete an exchange, producers can no longer send messages to the exchange or route messages in the exchange to one or more queues. Proceed with caution.
You cannot delete the exchanges of the headers type.
You cannot delete the built-in exchanges in a vhost. These exchanges are amq.direct, amq.topic, and amq.fanout.
On the Exchanges page, click the name of the exchange that you want to delete. In the upper-right corner of the Exchange Details page, click Delete.
In the Note message, click OK.
References
For more information about exchanges, see Exchange.