CLI integration examples

更新时间:
复制 MD 格式

Alibaba Cloud Command Line Interface (Alibaba Cloud CLI) is a universal command-line tool built on OpenAPI that you can use to automate the management and maintenance of ApsaraMQ for RocketMQ. This topic uses an example of calling the OnsInstanceBaseInfo API to query basic instance information to describe the procedure for using Alibaba Cloud CLI to call the OpenAPI of ApsaraMQ for RocketMQ 4.0.

Before you begin

Before you proceed, make sure that you are familiar with Alibaba Cloud CLI. For more information, see What is Alibaba Cloud CLI.

Step 1: Install Alibaba Cloud CLI

To use Alibaba Cloud CLI, you must first install it. Alibaba Cloud CLI provides installation packages for Windows, Linux, and macOS. Select the installation method that matches the operating system of your device.

You can also debug Alibaba Cloud CLI commands in Cloud Shell. Cloud Shell comes pre-installed with Alibaba Cloud CLI and is regularly updated. For more information, see What is Cloud Shell?.

Step 2: Configure Alibaba Cloud CLI

Important

An Alibaba Cloud account has full access to all OpenAPI operations for your services, which poses a high security risk. Create a RAM identity, grant permissions based on the principle of least privilege, and then use that identity to access OpenAPI. For information about the policies supported by ApsaraMQ for RocketMQ 4.x, see System policies for ApsaraMQ for RocketMQ and Custom policies for ApsaraMQ for RocketMQ.

Before using Alibaba Cloud CLI, you must configure your credentials and region ID. Alibaba Cloud CLI supports various types of credentials. For more information, see Configure and manage credentials. The following steps use AccessKey credentials as an example:

  1. Create a RAM user and an AccessKey pair to configure your credentials. For more information, see Create a RAM user and Create an AccessKey.

  2. Grant permissions to the RAM user. For this example, grant the AliyunMQPubOnlyAccess permission for ApsaraMQ for RocketMQ 4.x to the RAM user. This permission allows the RAM user to publish messages. For more information, see Manage RAM user permissions.

  3. Obtain the ID of an available region to configure your credentials. Alibaba Cloud CLI sends OpenAPI requests to the specified region. For information about the regions where ApsaraMQ for RocketMQ 4.x is available, see Endpoints.

    Note

    When you use Alibaba Cloud CLI, you can add the --region option to a command to specify a region for the request. This option overrides the region that is specified in your credential profile and environment variables. For more information, see Command-line options.

  4. Use the AccessKey pair of the RAM user to configure a credential profile named AkProfile. For more information, see Configure and manage credentials.

Step 3: Generate a sample CLI command

  1. In OpenAPI Explorer, go to the OnsInstanceBaseInfo debugging page.

  2. On the Parameters tab, enter the request parameters. Then, click the CLI Example tab to view the generated sample command.

  3. Copy the sample command or run it in Cloud Shell:

    • Click the Run Command image icon to open Cloud Shell and debug the command.

    • Click the Copy image icon to copy the sample command to the clipboard. You can then paste the command into your local shell to run it.

    Note

    If you copy the sample command to a local shell to debug the command, make sure that the parameter format is valid. For more information about the parameter formats for Alibaba Cloud CLI commands, see Parameters.

    OpenAPI Explorer adds the --region option to the sample command by default. If you run the command on your on-premises machine, this option overrides the region information in your default credential profile and environment variables. You can retain or remove this option.

Step 4: Call an ApsaraMQ for RocketMQ 4.0API

Example 1: List supported ApsaraMQ for RocketMQ 4.0APIs

Use the --help option to obtain a list of ApsaraMQ for RocketMQ 4.0 OpenAPI operations that you can call by using Alibaba Cloud CLI. You can also view the supported OpenAPI operations in List of operations by function.

  1. Run the command.

    aliyun ons --help
  2. View the output.

    Product: Ons (ApsaraMQ for RocketMQ 4.0)
    Version: 2019-02-14
    Available Api List:
      ListTagResources              Queries the tags that are bound to a resource.
      OnsConsumerAccumulate          Queries the message backlog for a specified group ID, including the number of backlogged messages and the consumption latency.
      OnsConsumerGetConnection       Queries the connection status of clients under a specified group ID.
      OnsConsumerResetOffset         Resets the consumer offset for a specified group ID to a specific point in time.
      OnsConsumerStatus              Queries the details about a specified group ID, including subscription checks, consumption TPS, load balancing status, and client connections.
      OnsConsumerTimeSpan            Queries the time range within which the consumer offset of a topic can be reset. This is determined by the production time of the earliest and latest messages in the topic.
      OnsDLQMessageGetById           Queries a dead-letter message (DLM) by its message ID. The details include the storage time, message body, key, and tag.
      OnsDLQMessagePageQueryByGroupId Performs a paginated query for all DLMs within a specified time range for a specific group ID.
      OnsDLQMessageResendById        Resends a DLM by its message ID so that the DLM can be consumed again.
      OnsGroupConsumerUpdate         Configures the message read permissions for the consumer cluster of a specified group ID. This operation is typically used to prevent a specified group ID from reading messages.
      OnsGroupCreate                 Creates a client group ID.
      OnsGroupDelete                 Deletes a group.
      OnsGroupList                   Queries all group IDs in a specified instance.

Example 2: Query instance basic information and endpoints

This example shows how to call the OnsInstanceBaseInfo operation of ApsaraMQ for RocketMQ 4.x to query the basic information and messaging endpoints of an ApsaraMQ for RocketMQ instance by its ID.

  1. Run the command.

    aliyun ons OnsInstanceBaseInfo --InstanceId 'MQ_INST_138015630679****_BAAy1Hac'
  2. View the output.

    {
      "InstanceBaseInfo": {
        "Endpoints": {
          "TcpEndpoint": "http://MQ_INST_138015630679****_BAAy1Hac.cn-chengdu.mq-internal.aliyuncs.com:8080",
          "TcpInternetEndpoint": "http://MQ_INST_138015630679****_BAAy1Hac.mq.cn-chengdu.aliyuncs.com:80",
          "HttpInternetEndpoint": "http://138015630679****.mqrest.cn-chengdu.aliyuncs.com",
          "HttpInternalEndpoint": "http://138015630679****.mqrest.cn-chengdu-internal.aliyuncs.com",
          "HttpInternetSecureEndpoint": "https://138015630679****.mqrest.cn-chengdu.aliyuncs.com"
        },
        "IndependentNaming": true,
        "MaxTps": 10000,
        "Remark": "ons-cn-m7r1r5f****",
        "InstanceName": "test",
        "ReleaseTime": 1603555200000,
        "TopicCapacity": 50,
        "InstanceStatus": 5,
        "InstanceId": "MQ_INST_138015630679****_BAAy1Hac",
        "InstanceType": 2,
        "CreateTime": "1570701259403",
        "spInstanceId": "ons-cn-m7r1r5f****",
        "spInstanceType": 1
      },
      "RequestId": "6CC46974-65E8-4C20-AB07-D20D102E****"
    }
    Note

    If an ApsaraMQ for RocketMQ 4.0 OpenAPI operation returns an error, use the error code to validate your request parameters and their values.

    You can also record the returned RequestId or SDK error information and use OpenAPI problem diagnosis to run diagnostics.