June 21, 2024
Ribbon Client-Side Load Balancer

Using Ribbon for Client-Side Load Balancing (with Spring Boot)

This topic on client-side load balancing actually follows from the Part 4 of Hystrix Circuit Breaker – Step by Step Configuration With Feign Client .  Actually, the Feign client already uses Ribbon which is  a component of Spring Cloud.

 

What is Load Balancing

Load balancing is way of distributing incoming traffic between microservices. So when several requests are coming, a load balancer would evenly route it to two or more targets depending on some algorithm.

With the use of load balancing, we can achieve fault tolerance in a microservice architecture. It ensures that no server is overloaded while another is idle.

I have summarized the benefits of load balancing below.

 

Benefits of Load Balancing

  • optimizes resource usage
  • maximizes throughput
  • minimizes the response time
  • prevents overloading of a single microservice
  • increases reliability and availability

Ribbon is a client-side load balancer that provides you with control over behavior of HTTP clients.

The architecture as shown below is a modification of the architecture we used for Feign Clients.

Ribbon Load Balancer Architecture

The Architecture

In this architecture, you can see that we have two instances of the same ProductServer running on two different ports. Of course we have have more instances as well.

Also, unlike the previous architecture, we now don’t have any alternate server component or alternate server proxy. Just one product server proxy. Traffic from the product server proxy now goes through the Ribbon load balancer. The Ribbon client-side load balancer would be given the addresses of the instances and it takes care of distributing the traffic to the instances.

 

The Design

Let’s now look at the code changes we need to achieve this. The ProductServer and ProductClient microservices from Part 4 remains the same.

We would create a new Project. I called it productwebribbon – get it on github.

Step 1: Create the new Spring Boot Project and add the following dependevcies:

  • Feign Client
  • Ribbon
  • Web
  • Lombok

Step 2: Create the following four files:

Product.java: This is the model file and I place it in the models package. The content is shown below:

@Data
public class Product {
    @Id
    private String productId;
    private String name;
    private String code;
    private String title;
    private String description;
    private String location;
    private Double price;
}

 

ProductService.java: This is simply an interface that lists the method available to the clients. There are just two interfaces like so

public interface ProductService {

    @GetMapping("products")
    public List<Product> getAllProducts();

    @GetMapping("/products/{productId}")
    public Product getProduct(@PathVariable("productId") String productId);
}

 

ProductServiceProxy.java:  A Feign client. It is also an interface but extends the ProductService. Here is the content

@FeignClient(name = "product-proxy")
public interface ProductServiceProxy extends ProductService {
}

 

ProductController.java: I guess you already know this. This is a Rest Controller that provides the endpoints for requests coming from the browser.

@EnableFeignClients(basePackageClasses = ProductServiceProxy.class)
@ComponentScan(basePackageClasses = ProductServiceProxy.class)
@CrossOrigin
@RestController
public class ProductController implements ProductServiceProxy {

    private ProductServiceProxy productServiceProxy;

    @Autowired
    public ProductController(ProductServiceProxy productServiceProxy) {
        this.productServiceProxy = productServiceProxy;
    }

    @Override
    @GetMapping("/products")
    public List<Product> getAllProducts() {
        return productServiceProxy.getAllProducts();
    }

    @Override
    @GetMapping("/products/{productId}")
    public Product getProduct(@PathVariable("productId") String productId) {
        return productServiceProxy.getProduct(productId);
    }
}

 

Configure the Properties

You now need to configure the properties. You need to provide the ports of the two ProductServers in the application.properties file(or yaml file if that’s what you use). See below:

server.port=8091
product-proxy.ribbon.listOfServers=localhost:8081,localhost:8082
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

 

5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] architecture for this microservice would be a little modification of the earlier one we did with Ribbon Load Balancer. See the diagram […]