All Products
Search
Document Center

ApsaraMQ for RabbitMQ:Step 2: Create resources

Last Updated:Jan 16, 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, 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 an instance endpoint

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 manage and click the 复制 icon to copy the endpoint. The following table describes the endpoint types provided by ApsaraMQ for RabbitMQ instances.

    Type

    Description

    Example

    Public endpoint

    You can access an instance over 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

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

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 by using the routing key and binding keys. Different routing rules are used for different types of exchanges. For more information, see Exchange.

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

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

  5. 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 binding 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 binding 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 this exchange. The working mechanism of a fanout exchange is similar to that of 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-jms-topic: Exchanges of this type are suitable for Java Message Service (JMS) applications that access ApsaraMQ for RabbitMQ by using the JMS API provided by ApsaraMQ for RabbitMQ. An exchange of this type routes a message to queues based on the result of fuzzy match between the routing key and binding keys. Wildcards are used to support fuzzy match. For more information, see Overview.

    • 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 is elapsed. 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 the x-consistent-hash 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.

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

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

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

    Parameter

    Description

    Note

    Queue Name

    The name of the queue.

    • 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 modify its name. If you want to modify its name, delete the queue and create another queue.

    • A name that starts with amq. is used as a reserved field. For example, amq.test is an invalid name.

    Auto Delete

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

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

    • false: The queue is not 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 binding key matches the routing key of the dead-letter messages.

    • MessageTTL: the message Time to Live (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.

Create a binding

If you bind an exchange to a queue, messages in the exchange are routed to the queue based on the specified routing rule.

  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 Binding 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 a positive integer that ranges from 1 to 20.

Create a pair of username and password

If you access an ApsaraMQ for RabbitMQ broker from an open source client, you must specify the username and password for authentication. You can access an ApsaraMQ for RabbitMQ broker only after the verification is passed.

  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 Static Accounts.

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

  5. In the Create Username/Password panel, configure the AccessKey ID and AccessKey Secret parameters. Then, 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 static username and password appear. The password is masked. Username and password

  6. In the Password column of the static username and password that you created, click Display to view the password that corresponds to the username.

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: