Nerd For Tech
Published in

Nerd For Tech

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.

service registry design pattern

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.

Pet-Web-Bff

Pet-Shop-Service

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

How to Manage MySQL Services in WHM?

The Power of the Open Cloud

Optional in Swift— Learning the Why

Individual Personalised Lanyards in Heribusta #Unique #Personalized #Lanyard #Heribusta https://t.co

Creating a Data Pipeline with DPR

Data pipelines are the oil for the new digital economy.

Data Pipeline Infrastructure at Meetup with Fewer Nightmares: Running Apache Airflow on Kubernetes

Pin Food Web App — Flask (part 1)

How companies are using GraphQL and what they migrated from?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sajith vijesekara

Sajith vijesekara

Technical Lead. Passionate about cloud computing & web security | Freelance Mobile Developer | AWS Community Builder

More from Medium

RSocket using Spring Boot

Spring Boot Webflux MongoDB multi-tenancy implementation

Spring Data JDBC: Implementing Domain Driven Design Aggregate

Building better GUIs using DDD and Spring HATEOAS