Create resources
Create the ApsaraMQ for RabbitMQ resources your applications need: an instance, vhost, exchange, queue, and binding.
Resource hierarchy
Instance
└── Vhost (logical isolation)
├── Exchange (routes messages)
└── Queue (stores messages)
└── Binding (connects an exchange to a queue)
|
Resource |
Role |
|
Instance |
Top-level container for all other resources. |
|
Vhost |
Isolates exchanges, queues, and bindings. One instance can hold 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 using a routing key. |
Data flow: Producer > Exchange > (routing key + binding) > Queue > Consumer
Prerequisites
If you use a Resource Access Management (RAM) user, grant the required permissions. 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 instance settings, accept the terms of service, and click Buy Now.
-
Complete the payment.
After payment, return to the Instances page and select your region to view the instance.
Step 2: Create a vhost
A vhost isolates resources. Specify a vhost before connecting 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 producer messages to queues by routing key. Exchange
-
In the left-side navigation pane, click Exchanges.
-
On the Exchanges page, expand the vhost drop-down list, select the target vhost from Change, and click Create Exchange.
-
In the Create Exchange panel, configure the following parameters and click OK.
Exchange parameters
|
Parameter |
Required |
Description |
|
Exchange Name |
Yes |
The exchange name. Names starting with |
|
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 messages to queues whose routing key exactly matches the message routing key. |
|
topic |
Pattern matching |
Routes messages to queues whose binding key matches the message routing key by pattern. |
|
fanout |
Broadcast |
Routes messages to all bound queues, ignoring routing keys. |
|
headers |
Header matching |
Routes messages by matching header key-value pairs against binding attributes instead of routing keys. |
|
x-delayed-message |
Delayed delivery |
Delays message delivery by the duration specified in the |
|
x-consistent-hash |
Consistent hashing |
Hashes routing keys or header values and uses consistent hashing to distribute messages across queues. |
Step 4: Create a queue
Queues store messages until consumers retrieve them.
-
In the left-side navigation pane, click Queues.
-
On the Queues page, expand the vhost drop-down list, select the target vhost from Change, and 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 |
Auto-deletes the queue when 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 |
Routing key for dead-letter messages. |
|
MessageTTL |
Message time-to-live (TTL) in milliseconds. Unconsumed messages become dead letters after the TTL expires. Message TTL |
Queue naming rules
-
Allowed characters: letters, digits, hyphens (-), underscores (_), periods (.), number signs (#), forward slashes (/), and at signs (@).
-
Length: 1 to 255 characters.
-
Names cannot be changed after creation. Delete and recreate to rename.
-
amq.is a reserved prefix. For example,amq.testis not allowed.
Step 5: Create a binding
A binding connects an exchange to a queue so that messages reach 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 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.