Client Side Load Balancing using Spring Ribbon

Technology: Ribbon is the client-side load balancer which gives control over HTTP and TCP Clients. The main purpose of Ribbon is named client, each load balancer is one of the component that work together to connect to remote server.

Spring cloud will provide one name for combination of all these components using RibbonClientConfiguration class. These components will contain LoadBalancer, a RestClient, and a ServerListFilter.

For embedding Ribbon into spring boot application, add below maven dependency

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

Client-Side Load Balancing:

At Server-side Load balancing we used deploy multiple instances of the Java application development andconfigure these instances in external load balancer to balance the load. But for client side balancing we need to pass list of application start URL`s to client, based on the algorithm on client end the in-memory load balancer will balance the requests like Random load balancer or round-robin algorithm.

Configuring the Ribbon Client:

We need to provide the Ribbon client name using @RibbonClient annotation, and using this name we can configure some of the limited properties in application.properties/ application.yml file.

Example: @RibbonClient(name = "server") this annotation has to present at class level, we can use this name for configuring the list of available server urls, so that based configured algorithm it will pick one of the configured server.

spring-ribbon

Some of the load balancing strategies offered are listed below:

  1. Round Robin Load balancing
  2. Software Defined Networking AdaptiveLoad balancing.
  3. Agent based adaptive Load balancing.
  4. Random Load balancing.
  5. Weighted Round Robin Load balancing
  6. Source IP Hash Load balancing
spring-ribbon

Example:

server.ribbon.listOfServers=localhost:8090,localhost:9092,localhost:9999 server.ribbon.ServerListRefreshInterval=15000

ServerListRefreshInterval is property after which client will sent heartbeat request to server.

spring-ribbon

Custom Configuration in Ribbon Client:

We can also provide the custom configuration java class including the below beans.

  1. IClientConfig -  which stores client configuration for a client or load balancer.
  2. IRule- which describes a load balancing strategy.
  3. IPing- which says how periodic pings of a server are performed.
  4. ServerList<Server> - which defines how to get a list of servers to choose from.
  5. ServerListFilter<Server>- This interface provides for shorting the configured or custom obtained List of candidate servers
  6. ILoadBalancer - which executes a software load balancer
  7. ServerListUpdater- This interface allows for updating the configures or dynamically obtained List of candidate servers.

If any of the beans are not present in configuration file, it will switch to default implementation which was provided spring-cloud-ribbon.

The sample Ribbon configuration file as below:

@Configuration public class ServerConfiguration { @Autowired IClientConfigribbonClientConfig; @Bean publicIPingribbonPing(IClientConfig config) { return new PingUrl(); } @Bean publicIRuleribbonRule(IClientConfig config) { return new AvailabilityFilteringRule(); } }

Note: The ServerConfiguration class must be a @Configuration class but in general it will not be present in @ComponentScan for applicationContext, if it available it will be shared by all @RibbonClients. If we use spring boot type of application, we need to make sure the it will be present in different package which should not be in component-scan.

We can also these implementation class names in application.properties file using below keys:

<clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer <clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule <clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing <clientName>.ribbon.NIWSServerListClassName: Should implement ServerList <clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilter

Note: Classes defined in these properties have precedence over beans defined by using @RibbonClient(configuration=ServerConfiguration.class) and the defaults provided by Spring Cloud Netflix.

List of possible keys will be found in com.netflix.client.config.CommonClientConfigKey, need to prefix to it.

If we deploy the server instances using Eureka server, the n Eureka server automatically discover all instances of it, we can use these lists without hard coding the application.properties file.

For now, we are disabling Eureka server in this blog for focusing on Ribbon client.

.ribbon.eureka.enabled=false

Conclusion: Ribbon client-side load balancer, using specified algorithm each time it will be invoked different service, it will send heart-beat to each of the server in configured list or dynamically list. It also provides the retry mechanism, connection timeout to heartbeats. We can provide Ribbon configuration in java class or we can provide using application.properties file.

Run the server application in different ports using SERVER_PORT=mvnspring-boot:run command.

Source code of the application is available at https://github.com/sravan4rmhyd/Spring-Boot-Ribbon.git