Registry and Discovery
Scenario
This section describes how to register and discover microservices by calling APIs. For details about how to call APIs, see Making an API Request.
Process
- Register the my-provider microservice.
- Register the my-provider microservice instance.
- Register the my-consumer microservice.
- The my-consumer microservice discovers the my-provider microservice instance.
The preceding microservice names are examples.
Prerequisites
- You have called the API to obtain the Identity and Access Management (IAM) token for authentication. You have also set the token to an environment variable. That is, export Token={token}.
- You have obtained the endpoint of Cloud Service Engine (CSE). For details, see Regions and Endpoints. The following uses {cse_endpoint} as an example.
Procedure
- Register the my-provider microservice.
Call the API for creating static information for a microservice. The following shows an example request.
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"}serviceId will be replaced with {providerServiceId} in the following example.
- Register the my-provider microservice instance.
Call the API for registering a microservice instance. 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" -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"} - Register the my-consumer microservice.
Call the API for creating static information for a microservice. The following shows an example request.
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"}serviceId will be replaced with {consumerServiceId} in the following example.
- The my-consumer microservice discovers the my-provider microservice instance.
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 service ID. The following shows an example request.
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, you can log in to ServiceStage and choose Infrastructure > Cloud Service Engines in the navigation pane. On the page that is displayed, click Control to go to the CSE console. Then, choose Service Catalog in the navigation pane to view the service registration information.
Last Article: Component Management
Next Article: Application Management APIs
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.