			 	                "Welcome To Ashok IT"				
			                    "Spring Boot and MicroServices"
                                             Topic : Load Balancer
                                             Date  : 03/07/2025
                                               (Session - 80)                               
_____________________________________________________________________________________________________________________________ 
Yesterday Session
=================
* We have completed working service discovery component in Microservices architecture.

  1. How to register the Microservices with Service Discovery i.e.,Eureka Server(SpringCloud)

  2. How to view the dashboard of Eureka Server and getting to know to Service related Information

  3. How to change the default port number of Eureka Server from 8761 to XXXX and also we have seen how to register
     the Microservices with Eureka

* We Started with Load Balancer Concept.


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"

                <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-loadbalancer</artifactId>
		</dependency>

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:<portno>"

  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