How to create Client Load Balancing with Spring Cloud Ribbon + Spring Boot

In the tutorial, JavaSampleApproach will show you the steps of creating a Client Load Balancing with Spring Cloud Ribbon.

Related articles:
How to configure SpringBoot Zuul – Routing and Filtering
How to start with Spring Cloud Centralized Configuration


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Load Balancer

1. Proxy Load Balancer

spring cloud ribbon - proxy load balancer

Advantage & Disadvantage:
– Centralized Load Balancing
– Can be bottle neck
– Single point of failure

2. Client Load Balancer

Spring Cloud Ribbon is a solution for Client Load Balancing.

spring cloud ribbon - ribbon load balancer

Advantage & Disadvantage:
– Decentralized Load Balancing
– No bottle neck
– Resilent
– Data can be inconsistent

III. Practice

Create 2 projects:
– a Spring Boot project for simple restful services, running at addresses: {localhost:8090,localhost:8091,localhost:8092}
– a Spring Boot project for Load BalancingRibbon Client, running at address: localhost:8080

Step to do:
– Create Spring Boot project for simple Restful service
– Create Spring Boot project for Ribbon client
– Config Ribbon client
– Create a Load Balancing Rest controller
– Run & check results

1. Create Spring Boot project for simple Restful service

– Using SpringToolSuite, create a SpringBoot project. Then add web dependency:

– Create a simple RestController with 2 requestmappings:

Why having @RequestMapping(value = "/")?
-> The requestmapping is used by Ribbon client to detect alive servers. If not having, we may meet an exception – No instances available:

2. Create Spring Boot project for Ribbon client

– Using SpringToolSuite, create a SpringBoot project. Then add ribbon & web dependencies:

3. Config Ribbon client

Open application.properties file, configure Ribbon client:

listOfServers defines a list of servers to load balancing.
ServerListRefreshInterval is the interval, between refreshes of Ribbon’s service list.

Create a Configuration class:

IClientConfig is used to store client configuration.
IRule is used to define load balancing strategy
IPing is used to defines the way to ping a server to check if its alive.

In the main Spring Boot class, configure Ribbon client by @RibbonClient annotation:

4. Create a Load Balancing RestController

Use @LoadBalanced annotation to configure RestTemplate bean as a LoadBalancerClient.

5. Run & check results

Build the projects with SpringBoot App mode.
– Run Restful Services at: 8090, 8091, 8092:
mvn spring-boot:run -Dserver.port=8090
mvn spring-boot:run -Dserver.port=8091
mvn spring-boot:run -Dserver.port=8092

– Then run Ribbon Client.

-> Ribbon Client starts successfully with Log:

– Make some GET requests: http://localhost:8080/helloworld. We can see sequence messages from services at {8090, 8091, 8092} by Round Robin rule for load balancing:

spring cloud ribbon - 8090

spring cloud ribbon - 8091

spring cloud ribbon - 8092

Some common exceptions you may meet:
-> If you turn down a restful server (they are running at ports {8090, 8091, 8092}), you will meet a connection refused exception:

-> If you turn down all servers, you will meet a No up servers available exception:

IV. SourceCode

RestfulService
SpringCloudRibbon


Related Posts


1 thought on “How to create Client Load Balancing with Spring Cloud Ribbon + Spring Boot”

Got Something To Say:

Your email address will not be published. Required fields are marked *

*