Microservices with Spring Boot - Part 4 - Using Ribbon for Load Balancing


Let’s learn the basics of microservices and microservices architectures. We will also start looking at a basic implementation of a microservice with Spring Boot. We will create a couple of microservices and get them to talk to each other using Eureka Naming Server and Ribbon for Client Side Load Balancing.

Here is the Microservice Series Outline: Microservices with Spring Boot

This is part 4 of this series. In this part, we will focus on using Ribbon for Load Balancing.

You will learn

  • What is the need for Load Balancing?
  • What is Ribbon?
  • How do you add Ribbon to your Spring Boot Project?
  • How do you enable and configure Ribbon to do Load Balancing?

Image

Image

Image

Image

10 Step Reference Courses

Microservices Overview

In the previous two parts, we created the microservices and established communication between them.

GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8000,
}

When we execute the above service, you would see that a request is also sent over to the forex-service.

Thats cool!

We have now created two microservices and established communication between them.

Image

However, we are hardcoding the url for FS in CCS component CurrencyExchangeServiceProxy.

@FeignClient(name="forex-service" url="localhost:8000")
public interface CurrencyExchangeServiceProxy {
  @GetMapping("/currency-exchange/from/{from}/to/{to}")
  public CurrencyConversionBean retrieveExchangeValue
    (@PathVariable("from") String from, @PathVariable("to") String to);
}

That means when new instances of Forex Service are launched up, we have no way to distributing load to them.

In this part, let’s now enable client side load distribution using Ribbon.

Tools you will need

  • Maven 3.0+ is your build tool
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Complete Maven Project With Code Examples

Our Github repository has all the code examples - https://github.com/in28minutes/spring-boot-examples/tree/master/spring-boot-basic-microservice

Enabling Ribbon

Add Ribbon Dependency to pom.xml

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

Enable RibbonClient in CurrencyExchangeServiceProxy

@FeignClient(name="forex-service")
@RibbonClient(name="forex-service")
public interface CurrencyExchangeServiceProxy {

Configure the instances in application.properties

forex-service.ribbon.listOfServers=localhost:8000,localhost:8001

Launch up Forex Service on 8001

In the above step, we configured ribbon to distribute load to instances. However, we do not have any instance of Forex Service running on 8001.

We can launch it up by configuring a launch configuration as shown in the figure below.

Image

Ribbon in Action

Currently we have the following service up and running

  • Currency Conversion Micro Service (CCS) on 8100
  • Two instances of Forex MicroService on 8000 and 8001

Now you would see that the requests to CCS would get distributed between the two instances of Forex Microservice by Ribbon

Request 1

GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8000,
}

Request 2

GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8001,
}

You can see that the port numbers in the two responses are different.

Summary

We have now created two microservices and established communication between them.

Image

We are using Ribbon to distribute load between the two instances of Forex Service.

However, we are hardcoding the urls of both instances of FS in CCS. That means every time there is a new instance of FS, we would need to change the configuration of CCS. Thats not cool.

In the next part, we will use Eureka Naming Server to fix this problem.

Next Steps

Image

Image

Image

Image

Image

Image

Image

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Introduction To Aspect Oriented Programming and Cross Cutting Concerns

Software applications are built in layers. There is common functionality that is sometimes needed across layers - logging, performance tracing etc. How do you implement these common features?

Programming Basics - Introduction To Object Oriented Programming

Object oriented programming (OOP) is all about thinking in terms of objects. Let's dig deeper.

Programming Basics - Five Things To Think About While Programming

You would obviously want to write code that meets your core requirements and provide good performance - choosing right data structures and algorithms to use is the fundamental part of programming. What are the other things that you need to worry about? Here are five things that we think are essential.

Asynchronous communication with queues and microservices - A perfect combination?

In this article, we throw some light on what asynchronous messaging is all about and discuss why you should consider it for your microservices architectures.

Microservice Best Practice - Build an Archetype

In this article, we focus on learning why creating proper archetypes is important for successful microservices architecture.

Microservice Architecture Best Practices - Messaging Queues

In this article, we discuss why Messaging queues are needed, and how they form the cornerstone of communication in microservices architectures.

Microservice Best Practice - Why do you build a Vertical Slice?

In this article, we look at what is a vertical slice, and why we build it. We also discuss the best practices involved in building vertical slices.

Microservices Architectures - Event Driven Approach

In this article, we talk about event driven approach, in the context of microservices architectures. We also discuss what are the advantages of using an event driven approach.

The 12 Factor App - Best Practices In Cloud Native Applications and Microservices

In order that an application be deployed in the cloud and enjoy features such as auto scaling, it first needs to be cloud native. In this article, we have a close look at the best practices for cloud native applications, popularly known as The 12 Factor App.