Updated on 2023-11-08 GMT+08:00

Registry and Discovery

Scenarios

This section describes how to register and discover microservices by calling APIs. For details about how to call APIs, see Calling APIs.

Process

  1. Register the my-provider microservice.
  2. Register the my-provider microservice instance.
  3. Register the my-consumer microservice.
  4. The my-consumer microservice discovers the my-provider microservice instance.

The preceding microservice names are examples.

Prerequisites

Obtain the registry center address of the ServiceComb engine instance by referring to Service Center Address. The following uses {cse_endpoint} as an example.

Procedure

  1. Register the my-provider microservice.

    Call the Creating Static Information for a Microservice API. The following is a request example:

    curl -k -H "x-domain-name:default" -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"
        }
    }'

    Response:

    {"serviceId":"918282e8562dc5fdc9a8dcd4d1baabb492190aa4"}

    serviceId will be replaced with {providerServiceId} in the following example.

  2. Register the my-provider microservice instance.

    Call the Registering a Microservice Instance API. 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 shows an example request.

    curl -k -H "x-domain-name:default" -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
            }
        }
    }'

    Response:

    {"instanceId":"2be605a095ed11eabcbe0255ac100fa3"}

  3. Register the my-consumer microservice.

    Call the Creating Static Information for a Microservice API. The following is a request example:

    curl -k -H "x-domain-name:default" -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"
        }
    }'

    Response:

    {"serviceId":"9db248934c31fc754d6e922b48ede4a5c004d3c1"}

    serviceId will be replaced with {consumerServiceId} in the following example.

  4. The my-consumer microservice discovers the my-provider microservice instance.

    Call the Querying a Microservice Instance by Filter Criteria API. The my-consumer microservice queries information about the my-provider microservice instance from the service center based on its own service ID. The following shows an example request.

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

    Response:

    {
        "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, choose Service Catalog on the CSE console to view the service registry information.