For making a request to a service a client must know its ip and port but in microservices architecture the number of instances of a service can dynamically change (scale up/scale down) based on load on a service . Therefore the service instances have dynamically assigned network location. For this reason service discovery mechansim is required to enable client and service interaction. Note that like a load balancer in a horizontally scalable monolithic architecture the service registry (which is a key component of service discovery mechanism) is avaliable on static ip or through domain name.
Service discovery is achieved service registry which is a database of network locations of service instances. The service instances must be added /removed from service registry as the number of service instances increase / decrease to manage the variation in load the service is experiencing.For service discovery to work
- Service registration the service must be registered in the service registry .this can be achieved via
- self registration
- the service calls service registry api to register its network locaation.it may also supply a health check url. Service registry may call this url periodically to inspect if service is running.
- 3rd party registration
- service instances are aumatically registered with the service reg by third party (registrar). the service does not need to do this.
- self registration
- Service discovery
- client side discovery - client of the service invokes service by first querying the service registry to get list of service instances and then make request to one of those instances.(load balancing is done here ). client might cache the service instances to reduce hits to service registry . App level/client side service discovery is popularized by netflix and pivotal . Netflix eureka is service registry . Netflix also provides eureka client and ribbon (a http clinet which supports eureka client). Advantage is client side service discovery will work across multiple deployment platforms but the downside is that service discovery lib will be required in every language used in the microservices architecture.
- server side discovery - instead of client querying the service registry, it makes a request to a dns name which resolves to a request router, that queries the service registry and load balances. Advantage of server side discovery is that service discovery entirely handled by deployment platform. Neither services nor clients have any discovery code,hence sd mechanism is readily avaliable to all services and client regardless of language.
- Note that microservices deployment platforms like kubernetes will use 3rd party registration and server side discovery.