"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Load Balancer Date : 31/01/2025 (Session - 115) _____________________________________________________________________________________________________________________________ Yesterday Session ================= * We have seen how to deploy our spring boot application in multiple ports. * Introduction to Load Balancing Today Session ============= * Spring Cloud LoadBalancer is a component of the Spring Cloud framework, which provides load balancing capabilities for microservices and applications running in a cloud environment. * Load balancing is an essential component of a microservices architecture as it helps distribute incoming network traffic across multiple instances of a service, ensuring high availability, scalability, and reliability. * Here are some key aspects of Spring Cloud LoadBalancer 1.Dynamic Load Balancing ======================== Spring Cloud LoadBalancer offers dynamic load balancing, which means it can adapt to the changing conditions of your service instances. It monitors the health of service instances and adjusts the traffic distribution accordingly. 2.Service Discovery Integration =============================== Spring Cloud LoadBalancer is often used in conjunction with service discovery tools like Netflix Eureka, Consul, or Spring Cloud's native service registration and discovery mechanisms. This integration allows it to discover available service instances. 3.Annotation-Based Approach =========================== Spring Cloud LoadBalancer provides annotations like @LoadBalanced that can be applied to RestTemplate or WebClient beans, enabling automatic load balancing for outbound service calls. Working with Load balancer ========================= Step-1: Add the Spring cloud Load balancer Starter in "Customer-Microservice" org.springframework.cloud spring-cloud-starter-loadbalancer Step-2: We can enable Load Balancer concept for RestTemplate, WebClient, FeignClient calls with help of @LoadBalanced Annotation. Example ======= @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } * In RestTemplate,WebClient calls we have been Hardcoded API Url's for Communicating with Address Microservice from Customer Microservices in those areas we need to replace "Application Name" into "localhost:" RestTemplate,WebClient We used following below API Url's application.properties ====================== #Microservices Configuration #address.service.url=http://localhost:9966/api/address/ address.service.name.url=http://ADDRESS-SERVICE/api/address/ * In CustomerServiceImpl.java Class required the below changes 1. @Autowired @LoadBalanced private RestTemplate restTemplate; 2. @Value("${address.service.name.url}") public String addressServiceUrl; 3. Navigate the following java method i.e.,getCustomerAndAddressById(int customerId); Enable the RestTemplate call from customer-Address Microservices Communication. 4. Navigate the following java method i.e.,callingAddressServiceWithRestTemplate in Address Microservice @Autowired private Environment environment; System.out.println("PortNumber:::::" + environment.getProperty("server.port")); --- Just wanted to Know which instance of Address Microservice survying the request NOTE ==== * When we have applied load balancer to RestTemplate,WebClient,OpenFeign by default load of the application will be distributed in "Round Robin Fashion". Example : We have 3 Instance of Address Microservice and user giving 12 request and these request get distributed equally to all 3 Instances by load balancer * Internally following interface from Spring cloud library having two implementation classes for LoadBalancer Implements ReactiveLoadBalancer <---------------------------> RoundRobinLoadBalancer, RandomLoadBalancer * If we want to switch the LoadBalancer from RoundRobin to Random required the below Changes in Our Code URL: https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#spring-cloud-loadbalancer