Help Center/ ServiceStage/ API Reference/ Examples/ Microservice Registry and Discovery
Updated on 2024-12-16 GMT+08:00

Microservice Registry and Discovery

Scenarios

Microservice is defined by service. If a process provides a service, it is a microservice. Each service has its own service functions. APIs that are not restricted by languages are open to other systems (HTTP frequently used). Multiple microservices form an application.

The following describes how to call the APIs for Creating Static Information for a Microservice, Registering a Microservice Instance, and Querying a Microservice Instance by Filter Criteria.

The preceding microservice names are examples.

For details about how to call APIs, see Calling APIs.

Prerequisites

  • You have obtained the IAM token by calling the API for obtaining a user token, and set the token to an environment variable, that is, export Token={token}.
  • You have obtained the CSE endpoint to replace {cse_endpoint} in the following procedure. For details, see Regions and Endpoints.

Procedure

  1. Call the API for Creating Static Information for a Microservice to register the my-provider microservice. The following is a request example:

    curl -k -H "x-domain-name:default" -H "x-auth-token:$Token" -XPOST "https://{cse_endpoint}/v4/default/registry/microservices" -d '{
        "service": {
            "serviceName": "my-provider",
            "appId": "default",
            "version": "1.0.0",
            "description": "test",
            "level": "MIDDLE",
            "status": "UP"
        }
    }'

    Information similar to the following is displayed.

    {"serviceId":"918282e8562dc5fdc9a8dcd4d1baabb492190aa4"}

    Record serviceId in bold to replace {providerServiceId} in subsequent examples.

  2. Call the API for Registering a Microservice Instance to register the instance of the my-provider microservice.

    The validity period of the instance is 1 hour. The instance is automatically brought offline when the validity period expires. Assume that the listening address of the my-provider microservice instance is 127.0.0.1:8080. The following is a request example:

    curl -k -H "x-domain-name:default" -H "x-auth-token:$Token" -XPOST "https://{cse_endpoint}/v4/default/registry/microservices/{providerServiceId}/instances" -d '{
        "instance": {
            "hostName": "test",
            "endpoints": [
                "rest:127.0.0.1:8080"
            ],
            "status": "UP",
            "healthCheck": {
                "mode": "push",
                "interval": 900,
                "times": 3
            }
        }
    }'

    Information similar to the following is displayed.

    {"instanceId":"2be605a095ed11eabcbe0255ac100fa3"}

  3. Call the API for Creating Static Information for a Microservice to register the my-consumer microservice. The following is a request example:

    curl -k -H "x-domain-name:default" -H "x-auth-token:$Token" -XPOST "https://{cse_endpoint}/v4/default/registry/microservices" -d '{
        "service": {
            "serviceName": "my-consumer",
            "appId": "default",
            "version": "1.0.0",
            "description": "test",
            "level": "MIDDLE",
            "status": "UP"
        }
    }'

    Information similar to the following is displayed.

    {"serviceId":"9db248934c31fc754d6e922b48ede4a5c004d3c1"}

    Record serviceId in bold to replace {consumerServiceId} in subsequent examples.

  4. Call the API for Querying a Microservice Instance by Filter Criteria. The my-consumer microservice queries information about the my-provider microservice instance from the service center based on its own serviceId. The following is a request example:

    curl -k -H "x-domain-name:default" -H "x-auth-token:$Token" -H "X-ConsumerId:{consumerServiceId}" -XGET "https://{cse_endpoint}/v4/default/registry/instances?appId=default&serviceName=my-provider&version=0.0.0%2B"

    Information similar to the following is displayed.

    {
        "instances": [
            {
                "instanceId": "2be605a095ed11eabcbe0255ac100fa3", 
                "serviceId": "918282e8562dc5fdc9a8dcd4d1baabb492190aa4", 
                "endpoints": [
                    "rest:127.0.0.1:8080"
                ], 
                "hostName": "test", 
                "status": "UP", 
                "healthCheck": {
                    "mode": "push", 
                    "interval": 150, 
                    "times": 3
                }, 
                "timestamp": "1589465646", 
                "modTimestamp": "1589465646", 
                "version": "1.0.0"
            }
        ]
    } 

    In actual practice, the my-consumer microservice can obtain the address of the my-provider microservice instance from the endpoint field in the instance query result and initiate service invoking.

    Alternatively, log in to ServiceStage, click Console of a microservice engine, and choose Service Catalog to view the service registry information.