Monday, April 12, 2021

Microservices - Client Side Load Balancing

https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer


Spring Cloud Load Balancer, we need to have a service registry up and running. 
A service registry makes it trivial to programmatically query for the location of a given service in a system. There are several popular implementations for service registries, including 
  • Apache Zookeeper, 
  • Netflix’s Eureka, 
  • Hashicorp Consul
  • Kubernetes 
  • Cloud Foundry
---------------------------------------------------------------------------------------------------------



Microservices don't use server side load balancing. They use client side load balancing.

Client Side Load Balancing

To understand client side load balancing, let's recap microservices architecture. We generally create a service discovery like Eureka or Consul, where each service instance registers when bootstrapped. Eureka server maintains a service registry; it maintains all the instances of the service as a key/value map, where the {service id} of your microservice serves as the key and instances serve as the value. Now, if one microservice wants to communicate with another microservice, it generally looks up the service registry using DiscoveryClient and Eureka server returns all the instances of the calling microservice to the caller service. Then it was a caller service headache which instance it calls. Here, client side load balancing stepped in. Client side load balancing maintains an algorithm like round robin or zone specific, by which it can invoke instances of calling services. The advantage is s service registry always updates itself; if one instance goes down, it removes it from its registry, so when the client side load balancer talks to the Eureka server, it always updates itself, so there is no manual intervention- unlike server side load balancing- to remove an instance.

Another advantage is, as the load balancer is in the client side, you can control its load balancing algorithm programmatically. Ribbon provides this facility, so we will use Ribbon for client side load balancing.

----------------------------------------------------------------------------------------------------------

https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer

The @LoadBalanced Annotation

In this final example, we’ll have Spring Cloud configure the WebClient instance for us. This approach is excellent if all requests that pass through that shared WebClient instance require load balancing. Just define a provider method for the WebClient.Builder and annotate it with @LoadBalanced. You can then use that WebClient.Builder to define a WebClient that’ll load balance automatically for us.


@Bean @LoadBalanced WebClient.Builder builder() { return WebClient.builder(); } @Bean WebClient webClient(WebClient.Builder builder) { return builder.build(); }

No comments:

Post a Comment

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass