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
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.
Some of the load balancing strategies offered are listed below:
- Round Robin Load balancing
- Software Defined Networking AdaptiveLoad balancing.
- Agent based adaptive Load balancing.
- Random Load balancing.
- Weighted Round Robin Load balancing
- Source IP Hash Load balancing
ServerListRefreshInterval is property after which client will sent heartbeat request to server.
Custom Configuration in Ribbon Client:
We can also provide the custom configuration java class including the below beans.
- IClientConfig - which stores client configuration for a client or load balancer.
- IRule- which describes a load balancing strategy.
- IPing- which says how periodic pings of a server are performed.
- ServerList<Server> - which defines how to get a list of servers to choose from.
- ServerListFilter<Server>- This interface provides for shorting the configured or custom obtained List of candidate servers
- ILoadBalancer - which executes a software load balancer
- 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:
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:
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.
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=
Source code of the application is available at https://github.com/sravan4rmhyd/Spring-Boot-Ribbon.git