Microservice based architecture is widely practiced nowadays. One service has multiple instances running over multiple virtualized containers where network location is dynamic due to various reasons like auto-scaling, upgrades, failures. Unlike monolithic architecture where service runs on the fixed location and can be called using static network address. Client service needs to know the latest service address calling it each time. To overcome this problem service registry helps.
Service registry as a centralized place where information of the microservice instances are stored and updated. When a service is developed and runs on a container it registers itself on the service registry. Now when service A needs to communicate with service B, the network address of B should be available. To find the address is called service discovery
There are two patterns of service discovery-
In this pattern, Client is given only one network address which will act as a router and forward the request to service instances. AWS Elastic Load Balancer is providing server-side discovery which load balances the traffic over multiple EC2 instances running.
In this pattern, Client is responsible to get the service instance address and make calls. Clients know about the all service instance running and make a smart decision to use one of them to balance the load. Netflix Eureka and Consul by Hashicorp are an example of client-side discovery. Netflix Ribbon client provides load balance across the service instances. In my example, in this post, I have used Feign client which provides a very good interface and annotation based web service implementation.
I have successfully implemented client-side load balancing and service discovery using consul and Eureka. Find below examples.