Updated on 2022-06-20 GMT+08:00

Example

This section describes how to call the API for querying the service list. In the following example, instances are deployed on SpringCloudDemo. Spring Cloud is a microservice architecture development tool based on Spring Boot. It facilitates configuration management, service governance, circuit breakers, intelligent routing, micro-proxy, control bus, global locks, leadership election, distributed sessions, and cluster state management involved in the microservice architecture.

To call the API used to query the service list, do as follows:

  1. Obtain the token, which will be put into the request header for authentication in a subsequent request.
  2. Construct and initiate a request to call the API used to query the service list.
  3. View the obtained response, and check whether the API used to query the service list is successfully called.

Preparations:

  1. The SpringCloudDemo application has been deployed on the server.
  2. The endpoints of IAM and APM have been obtained. In this example, the endpoint of IAM is iam_Endpoint, and the endpoint of APM is apm_Endpoint.
  1. Obtain the token for authentication.

    1. Enter POST https://IAM endpoint/v3/auth/tokens. In this example, IAM endpoint is iam_Endpoint. Therefore, enter POST https://iam_Endpoint/v3/auth/tokens.
    2. After a response is returned, the value of X-Subject-Token in the response header is the token value.
      Example request:
      {
        "auth": {
          "identity": {
            "methods": [
              "password"
            ],
            "password": {
              "user": {
                "name": "username",//Replace the username as required.
                "password": "password",//Replace the password as required.
                "domain": {
                  "name": "domainname"//Replace the domainname as required.
                }
              }
            }
          },
          "scope": {
            "project": {
              "name": "ap-southeast-1" //ap-southeast-1 indicates the IAM region. Replace it as required.
      
            }
          }
        }
      }

  2. Construct a request and send it.

    A request consists of three parts: a request line, request header, and request body (optional). Construct a request, send it, and wait for the response.

    1. Request line

      GET https://Endpoint/v1/{project_id}/ats/applications

      Endpoint indicates the endpoint of APM, which is apm_Endpoint in this example.

      {project_id} indicates the path parameter.

      Parameter

      Type

      Description

      project_id

      String

      Project ID.

      During token authentication, the response body contains the value of {project_id}, which is id: 12ff18574dfe4b92...... in this example.

      Request line:

      https://apm_Endpoint/v1/12ff18574dfe4b92....../ats/applications?monitorGroup=SpringCloudDemo

    2. Request header

      X-Auth-Token. The value is the value of X-subject-token obtained in token authentication.

      X-Auth-Token: MIIRpQYJKoZIhvcNAQcCoIIRljCCEZICAQExDTALBglghkgBZQMEAgEwgg-zBgkqhkiG9w0BBwGggg...

      Content-Type: application/json;charset=utf8

    3. Request body (not involved in this example)

  3. Receive the response.

    After the request is sent, a response is returned.

    Response:

    {
      "errorCode": "SVCSTG.ATS.2000",
      "errorMessage": null,
      "responseInfo": [
        "spring-cloud-service-eureka-server",
        "spring-cloud-testservice-consumer",
        "spring-cloud-testservice-provider",
        "springboot1.2",
        "springboot1.3.8",
        "springboot1.4.7",
        "springboot1.5.13"
      ]
    }

    The response consists of errorCode, errorMessage, and responseInfo.

    responseInfo indicates all the services that are queried on SpringCloudDemo.