Create resources

更新时间:
复制 MD 格式

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

  1. Log on to the ApsaraMQ for RabbitMQ console. In the left-side navigation pane, click Instances.

  2. In the top navigation bar, select a region. On the Instances page, click Create Instance.

  3. On the buy page, configure instance settings, accept the terms of service, and click Buy Now.

  4. 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.

  1. On the Instances page, click the name of your instance.

  2. In the left-side navigation pane, click Vhosts.

  3. On the Vhosts page, click Create Vhost.

  4. 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

  1. In the left-side navigation pane, click Exchanges.

  2. On the Exchanges page, expand the vhost drop-down list, select the target vhost from Change, and click Create Exchange.

  3. 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 amq. are reserved (for example, amq.test).

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 = x-delayed-message

The routing rule for the delayed-message exchange.

Hash value

Type = x-consistent-hash

The input for hash calculation. Valid values: RoutingKey, Header Value.

hash-header

Type = x-consistent-hash and Hash value = Header Value

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-delay header attribute (in milliseconds). After the delay elapses, the message is routed according to the rule defined by x-delayed-type.

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.

  1. In the left-side navigation pane, click Queues.

  2. On the Queues page, expand the vhost drop-down list, select the target vhost from Change, and click Create Queue.

  3. 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.test is not allowed.

Step 5: Create a binding

A binding connects an exchange to a queue so that messages reach the queue.

  1. On the Queues page, find your queue and click Details in the Actions column.

  2. On the Queue Details page, click the Bound as Destination tab. Then, click Add Binding.

  3. 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

Step 3: Use an SDK to send and receive messages

References

You can also create resources by calling API operations: