Create the required ApsaraMQ for RabbitMQ resources -- an instance, a vhost, an exchange, a queue, and a binding -- before you connect a producer or consumer.
Resource hierarchy
Instance
└── Vhost (logical isolation)
├── Exchange (routes messages)
└── Queue (stores messages)
└── Binding (connects an exchange to a queue)Resource | Role |
Instance | An independent resource entity that holds all other resources. |
Vhost | A logical boundary that isolates its own exchanges, queues, and bindings. An instance can contain multiple vhosts. |
Exchange | Receives messages from producers and routes them to queues based on a routing key. |
Queue | Holds messages until consumers retrieve them. |
Binding | Links an exchange to a queue with a routing key so that messages reach the correct destination. |
Data flow: Producer > Exchange > (routing key + binding) > Queue > Consumer
Prerequisites
If you use a Resource Access Management (RAM) user, grant the required ApsaraMQ for RabbitMQ permissions first. For more information, see Step 1: (Optional) Grant permissions to a RAM user.
Step 1: Create an instance
Log on to the ApsaraMQ for RabbitMQ console. In the left-side navigation pane, click Instances.
In the top navigation bar, select a region. On the Instances page, click Create Instance.
On the buy page, configure the instance settings, read and accept the terms of service, and then click Buy Now.
Complete the payment.
After the purchase, return to the Instances page and select the region where you created the instance to view it.
Step 2: Create a vhost
A vhost provides logical isolation of resources. Specify a vhost before you connect a producer or consumer.
On the Instances page, click the name of your instance.
In the left-side navigation pane, click Vhosts.
On the Vhosts page, click Create Vhost.
In the Create Vhost panel, enter a name in the vhost Name field and click OK.
Step 3: Create an exchange
An exchange routes messages from producers to queues based on a routing key. For more information, see Exchange.
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 following parameters and click OK.
Exchange parameters
Parameter | Required | Description |
Exchange Name | Yes | The name of the exchange. |
Type | Yes | The exchange type. |
Internal | No | Whether the exchange is internal. Default: No. Set to Yes to bind this exchange to another exchange instead of a queue. |
The following parameters appear only for specific exchange types:
Parameter | When to configure | Description |
x-delayed-type | Type = | The routing rule for the delayed-message exchange. |
Hash value | Type = | The input for hash calculation. Valid values: RoutingKey, Header Value. |
hash-header | Type = | The header used as the input for hash calculation. |
Exchange types
Type | Routing logic | Description |
direct | Exact match | Routes a message to the queue whose routing key exactly matches the routing key of the message. |
topic | Pattern matching | Routes a message to one or more queues based on pattern matching between the message routing key and the routing keys used to bind queues to the exchange. |
fanout | Broadcast | Routes all received messages to every queue bound to the exchange, regardless of routing keys. |
headers | Header matching | Routes messages based on headers attributes (key-value pairs) instead of routing keys. Binding attributes and message headers must both be in key-value format. The exchange matches message headers against binding attributes to determine routing. |
x-delayed-message | Delayed delivery | Delays message delivery by the duration specified in the |
x-consistent-hash | Consistent hashing | Performs hash calculation on routing keys or header values and uses consistent hashing to distribute messages across queues. |
Step 4: Create a queue
A queue stores messages delivered by exchanges until consumers retrieve them.
In the left-side navigation pane, click Queues.
On the Queues page, click the drop-down arrow next to vhost. From the Change drop-down list, select the vhost in which you want to create a queue. Then, click Create Queue.
In the Create Queue panel, configure the following parameters and click OK.
Queue parameters
Parameter | Required | Description |
Queue Name | Yes | The name of the queue. |
Auto Delete | No | Whether the queue is automatically deleted after the last consumer unsubscribes. Default: No. |
Click Dead-letter Policy to configure optional parameters:
Parameter | Description |
DeadLetterExchange | The exchange to which dead-letter messages are delivered. |
DeadLetterRoutingKey | The routing key for dead-letter messages. The dead-letter exchange routes messages to the queue whose routing key matches this value. |
MessageTTL | The message time-to-live (TTL), in milliseconds. A message that is not consumed within the TTL becomes a dead-letter message and is sent to the dead-letter exchange. For more information, see Message TTL. |
Queue naming rules
Allowed characters: letters, digits, hyphens (-), underscores (_), periods (.), number signs (#), forward slashes (/), and at signs (@).
Length: 1 to 255 characters.
The name cannot be changed after creation. To rename a queue, delete it and create a new one.
amq.is a reserved prefix and cannot be used at the beginning of a queue name. For example,amq.testis not allowed.
Step 5: Create a binding
A binding connects an exchange to a queue so that messages can be routed from the exchange to the queue.
On the Queues page, find your queue and click Details in the Actions column.
On the Queue Details page, click the Bound as Destination tab. Then, click Add Binding.
In the Add Binding panel, configure the Source Exchange and Routing Key parameters. Then, click OK.
If the source exchange is of the x-consistent-hash type, the routing key represents the weight of the queue. The weight must be an integer from 1 to 20.Next steps
References
You can also create resources by calling the following API operations:
CreateInstance: Create an instance.
ListInstances: Get an instance endpoint.
CreateVirtualHost: Create a vhost.
CreateExchange: Create an exchange.
CreateQueue: Create a queue.
CreateBinding: Create a binding.
CreateAccount: Create a static username and password for an instance.