Service Discovery in Microservices— Spring Boot Web Flux
Spring cloud Discovery Server with web flux
As we lest in our last article we are continue the micro-services case study. In this thread we are discussing about the service discovery in micro services.
Why Need Service Discovery
First why we need service discovery? In the real world when we dealing with communicate with micro services needs to check whether service is up and ruining ? If the service is not reachable we can communicate to other instance.
In above example first instance of service B will be not available to access. So when request originated from service A It will be failed to connect to service B if it access the first instance. So there came up design pattern to keeps the registry of instances in separate server. So that one will give the instance details when connecting to service B.
Here when start the Service B instance It will be register in Service Registry. Then there are no hard coded instance details in Service A. It will helps to dynamically connects to the Service B. Assume if the one of the Service B instance got failed, So service registry prevent to connect with that one with Service A.
Spring Boot With Netflix Eureka
Lets implement the discovery pattern using netflix eureka library.
First Our web bff module act as discovery server. These are the main configs changes needs to add to pom.xml & Annotations.
Web Bff Module
Dependencies
Config Changes
Shop Service Module
Here This module needs to register as eureka client modules. So They will automatically register with the eureka server.
Here we have to set Application Name & Instance ID for the micro service.
And add following annotation for the spring boot application.
Those registered micro-services will be display in discovery server admin portal.
Load Balancing with Webclient
Here is the tricky part. Most of the times we are using RestTemplate to connect micro-services. But In this scenario we have to use WebClient.
Make sure to add spring-cloud-loadbalancer to the Project.
And We can’t use @LoadBalanced
Annotation as we used in RestTemplate.In Order to use Load Balancing behavior in WebClient needs to use ReactorLoadBalancerExchangeFilterFunction class as filter config in WebClient.
This will demonstrate how to use spring webflux with Spring Cloud Discovery Service.
Here you can access the code base.