All Products
Search
Document Center

ApsaraMQ for RabbitMQ:Step 2: Create resources

最終更新日:Oct 22, 2024

Before you connect an application to an ApsaraMQ for RabbitMQ broker, you must create the required ApsaraMQ for RabbitMQ resources, such as an instance, a vhost, an exchange, a queue, and a pair of username and password. You must also bind an exchange to the queue and obtain the endpoint of the instance. When you use an SDK to send and receive messages, you must specify the resource information in the SDK code.

Create an instance

In ApsaraMQ for RabbitMQ, an instance is an independent resource entity that includes basic resources such as vhosts, exchanges, and queues.

  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, follow the on-screen instructions to complete the configurations, read and select the terms of service, and then click Buy Now.

  4. Follow the on-screen instructions to complete the payment.

    In the top navigation bar of the Instances page, select the region where the instance was created. On the page that appears, view the instance that you created.

    Note
    • After you purchase a Professional Edition or Enterprise Edition instance, the instance immediately enters the Running state.

    • After you purchase an Enterprise Platinum Edition instance, the instance enters the Deploying state. After the instance is deployed, the instance enters the Running state.

Obtain the endpoint of an instance

Before you send and receive messages, you must specify the endpoint that a producer or a consumer can use to access the ApsaraMQ for RabbitMQ instance.

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

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

  3. On the Endpoint Information tab of the Instance Details page, move the pointer over the type of endpoint that you want to use. Then, click the 复制 icon next to the endpoint to copy the endpoint.

    Type

    Description

    Example

    Public endpoint

    You can access an instance from the Internet to read and write data. By default, pay-as-you-go instances support public endpoints. To use the public endpoint of a subscription instance, you must enable Internet access when you create the subscription instance.

    XXX.net.mq.amqp.aliyuncs.com

    VPC endpoint

    You can access an instance in a virtual private cloud (VPC) to read and write data. By default, pay-as-you-go and subscription instances support VPC endpoints.

    XXX.vpc.mq.amqp.aliyuncs.com

Configure resources

1. Create a vhost

A vhost is used to logically isolate resources. Each vhost manages its own exchanges, queues, and bindings. Applications can run on independent vhosts in a secure manner. This way, the business of an application is not affected by other applications. An instance can contain multiple vhosts, and a vhost can contain multiple exchanges and queues. You must specify a vhost before you connect a producer or a consumer to ApsaraMQ for RabbitMQ.

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

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

  3. In the left-side navigation pane, click vhosts.

  4. On the vhosts page, click Create vhost.

  5. In the Create vhost panel, enter a vhost name in the vhost Name field and click OK.

2. Create an exchange

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. For more information, see Exchange.

  1. In the left-side navigation pane of the Instance Details page, click Exchanges.

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

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

3. Create a queue

In ApsaraMQ for RabbitMQ, a queue is a message queue. All messages from ApsaraMQ for RabbitMQ are delivered to one or more queues.

  1. In the left-side navigation pane of the Instance Details page, click Queues.

  2. On the Queues page, click the drop-down arrow next to vhost. From the Change drop-down list, select the vhost on which you want to create a queue. Then, click Create Queue.

  3. In the Create Queue panel, enter a queue name in the Queue Name field and configure the Auto Delete parameter. Click Advanced Settings and configure the queue parameters that are displayed. Then, click OK.

    Table 1. Parameters

    Parameter

    Description

    Usage notes

    Queue Name

    The queue name.

    • The name can contain letters, digits, hyphens (-), underscores (_), periods (.), number signs (#), forward slashes (/), and at signs (@).

    • The name must be 1 to 255 characters in length.

    • After a queue is created, you cannot change its name. If you want to change the name of a queue, delete the queue and create another queue.

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

    Auto Delete

    Specifies whether the queue is automatically deleted after the last subscription from consumers to the queue is canceled.

    • true: The queue is automatically deleted after the last subscription from consumers to this queue is canceled.

    • false: The queue is not automatically deleted after the last subscription from consumers to this queue is canceled.

    Advanced Settings

    Other parameters for the queue, such as the dead-letter exchange, dead-letter routing key, and message time-to-live (TTL).

    • DeadLetterExchange: the exchange to which dead-letter messages are delivered.

    • DeadLetterRoutingKey: the routing key of dead-letter messages. A dead-letter exchange sends dead-letter messages to the queue whose routing key matches the routing key of the dead-letter messages.

    • MessageTTL: the message TTL, in milliseconds. A message that is not consumed within the specified message TTL is a dead-letter message and is sent to a dead-letter exchange. For more information, see Message TTL.

4. Create a binding

  1. On the Queues page, find the queue that you want to manage 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 Confirm.

    Note

    If the bound exchange is of the x-consistent-hash type, the routing key indicates the weight of the queue. The value of a weight must be an integer that ranges from 1 to 20.

5. Create a pair of username and password

  1. In the left-side navigation pane of the Instance Details page, click Static Accounts.

  2. On the Static Accounts page, click Create Username/Password.

  3. In the Create Username/Password panel, configure the AccessKey ID and AccessKey Secret parameters and click OK.

    Note

    You can obtain the values of the AccessKey ID and AccessKey Secret parameters in the RAM console. For more information, see Create an AccessKey pair.

    On the Static Accounts page, the created pair of static username and password appears. The password is masked.用户名密码

  4. In the Password column of the created pair of static username and password, click Display to view the password.

What to do next

Step 3: Use SDKs to send and receive messages

References

You can also create ApsaraMQ for RabbitMQ resources by calling the following API operations: