
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: June 2, 2022
Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides client-side load balancing algorithms.
Apart from the client-side load balancing algorithms, Ribbon provides also other features:
Ribbon API works based on the concept called “Named Client”. While configuring Ribbon in our application configuration file we provide a name for the list of servers included for the load balancing.
Let’s take it for a spin.
The Netflix Ribbon API can be added to our project by adding the below dependency to our pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
The latest libraries can be found here.
In order to see the working of Ribbon API, we build a sample microservice application with Spring RestTemplate and we enhance it with Netflix Ribbon API along with Spring Cloud Netflix API.
We’ll use one of Ribbon’s load-balancing strategies, WeightedResponseTimeRule, to enable the client side load balancing between 2 servers, which are defined under a named client in the configuration file, in our application.
Ribbon API enables us to configure the following components of the load balancer:
Let write a simple configuration for the library:
public class RibbonConfiguration {
@Autowired
IClientConfig ribbonClientConfig;
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
@Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}
Notice how we used the WeightedResponseTimeRule rule to determine the server and PingUrl mechanism to determine the server’s availability in real-time.
According to this rule, each server is given a weight according to its average response time, lesser the response time gives lesser the weight. This rule randomly selects a server where the possibility is determined by server’s weight.
And the PingUrl will ping every URL to determine the server’s availability.
Below is the application.yml configuration file we created for this sample application:
spring:
application:
name: spring-cloud-ribbon
server:
port: 8888
ping-server:
ribbon:
eureka:
enabled: false
listOfServers: localhost:9092,localhost:9999
ServerListRefreshInterval: 15000
In the above file, we specified:
Let’s now set up the main application component snippet – where we use the RibbonClient to enable the load balancing instead of the plain RestTemplate:
@SpringBootApplication
@RestController
@RibbonClient(
name = "ping-a-server",
configuration = RibbonConfiguration.class)
public class ServerLocationApp {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/server-location")
public String serverLocation() {
return this.restTemplate.getForObject(
"http://ping-server/locaus", String.class);
}
public static void main(String[] args) {
SpringApplication.run(ServerLocationApp.class, args);
}
}
And here’s the RestTemplate configuration:
@Configuration
public class RestTemplateConfiguration{
@LoadBalanced
@Bean
RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
We defined a controller class with the annotation @RestController; we also annotated the class with @RibbonClient with a name and a configuration class.
The configuration class we defined here is the same class that we defined before in which we provided the desired Ribbon API configuration for this application.
Notice we annotated the RestTemplate with @LoadBalanced which suggests that we want this to be load balanced and in this case with Ribbon.
As we discussed earlier in this article, Ribbon API not only provides client side load balancing algorithms but also it has built in failure resiliency.
As stated before, Ribbon API can determine the server’s availability through the constant pinging of servers at regular intervals and has a capability of skipping the servers which are not live.
In addition to that, it also implements Circuit Breaker pattern to filter out the servers based on specified criteria.
The Circuit Breaker pattern minimizes the impact of a server failure on performance by swiftly rejecting a request to that server that is failing without waiting for a time-out. We can disable this Circuit Breaker feature by setting the property niws.loadbalancer.availabilityFilteringRule.filterCircuitTripped to false.
When all servers are down, thus no server is available to serve the request, the pingUrl() will fail and we receive an exception java.lang.IllegalStateException with a message “No instances are available to serve the request”.
In this article, we discussed Netflix Ribbon API and its implementation in a simple sample application.