Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Client Side Load Balancing using Spring Ribbon

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

Spring Cloud will provide one name for the 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 to deploy multiple instances of the application development and configure these instances in an external load balancer to balance the load. But for client-side balancing, we need to pass a list of application start URLs to the client, based on the algorithm on the 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 the 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 servers.

spring-ribbon

Some of the load balancing strategies offered by Java application development company that 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 the property after which the client will send a heartbeat request to the 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 the 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 is available it will be shared by all @RibbonClients. If we use the spring boot type of application, we need to make sure it will be present in the different package which should not be in component-scan.

We can also these implementation class names in the 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 the Eureka server, the n Eureka server automatically discovers all instances of it, we can use these lists without hard coding the application.properties file.

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

.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 servers in the 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 the application.properties file.

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

Download Source code

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients