The Name server is an ideal solution for the preceding situation. The following diagram shows how Name servers work:
All microservices (different microservices and all their instances) will register themselves with the Name server as each microservice starts up. When a service consumer wants to get the location of a specific microservice, it requests the Name server.
A unique microservice ID is assigned to each microservice. This is used as a key in the register request and the lookup request.
Microservices can automatically register and unregister themselves. Whenever a service consumer looks up the Name server with a microservice ID, it will get the list of the instances of that specific microservice.