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.
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.
Some of the load balancing strategies offered by Java application development company that 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
Example:
ServerListRefreshInterval is the property after which the client will send a heartbeat request to the 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.
ALSO READ
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:
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:
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.
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.