Sunday, August 18, 2019

Microservices: Service Registry and Discovery


In my previous blog, I have already talked about Netflix Hystrix Circuit Breaker Microservices and explained it by creating two applications i.e. Product Service and Price Service.
In this post, I’ll use both applications to explain what is the Service Registry and Discovery Server.

What is Service Registry and Discovery and why do we need it in the first place?

In our monolithic application, mostly service invoke one another through language methods or procedure calls and even in traditional distributed system deployment, services run at fixed, well-known locations (hosts and ports) and so can easily call one another using HTTP/REST. Over here the network locations of service instances are relatively static.

With the microservice architecture system, this is a much more difficult problem as service instances have dynamically assigned network locations. Service instances change dynamically because of auto-scaling, failure or upgrade. So, you can see that it’s not possible to hard code the locations (hosts and ports) of service instances in our application.

Apart from that, most of the time we have to deploy the same instance on multiple hosts (E.g. During Big billions day sale where 1000’s of instances is running to support high demand) which will be again a tedious task for the load balancer to register and deregister itself for all these services.
 

What is Service Registry and Discovery?

Service Registry is the server instance where all the service provider register itself when it starts up and deregisters itself when it leaves the system. The service instance’s registration is typically refreshed periodically using a heartbeat mechanism.

Whenever a client has to communicate with other service instances, it has to first query to Service registry to get the network location and then uses a load-balancing algorithm to select one of the available service instances and invoke a request. 

The above instance discovery pattern is called a Client-Side discovery pattern and this can be easily implemented using Netflix Eureka as a Service Registry. It provides a REST API for managing service‑instance registration and for querying available instances.

Netflix also provides Netflix Ribbon which is an IPC client that works with Eureka to load balance requests across the available service instances.


Implementation

As we already have two microservices product-service and price-service and we know product-service is internally invoking price-service to get the price detail of the products. We will do minor changes with these two applications to support service registry.

Discovery Server Implementation

  • Create a spring boot application and Eureka Server starter dependency to your pom.xml file 

  • Add @EnableEurekaServer annotation to Enable Eureka Server.
  • By default, Each Eureka Server is also a Eureka Client which needs at least one service URL to locate service registry. We have to disable this feature as we are creating single node Eureka Server. 
  • Add below properties to application.properties file.

  • Now start the discovery-server application and access http://localhost:8761 which will display the UI similar to below screenshot. 

  • As of now, no application is registered with the Eureka Server. 

2. Registering product-service and price-service to Eureka Clients

  • Let’s make out product-service and price-service as Eureka Client which should register to Eureka Server itself on starts up.
  • Add Eureka Client dependency on both pom.xml files.

  • Configure eureka.client.service-url.defaultZone property in application.properties to automatically register with the Eureka Server. 

  • Now start product-service and price service application and hit http://localhost:8761. We will see product-service and price-service is registered with SERVICE ID as PRODUCT-SERVICE and PRICE-SERVICE respectively. We can also notice the status as UP(1) which means the services are up and running and only one instance of product and price-service are running. 

3. Update hard code URL with application name in product-service and restart the application. 


4. Now open browser and hit product URL and you will get the below response.


Congratulation!!! Our application is perfectly working with Discovery Service. 

Spring-boot microservices can be downloaded from GITHUB

Happy Coding...!!!

1 comment:

  1. We regularly adapt and alter to developments in our industry and the needs of our customers. Cost-effective laser slicing, carried out with the optimum techniques and know-how for best results and assured precision. Fabricators also make sure merchandise are high precision machining marked and printed in accordance with each project’s specs.

    ReplyDelete

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...