Spring Cloud + Netflix Ribbon(2024) Hello World Example
In this tutorial we will be implementing Spring cloud with Netflix Ribbon with the help of an example.Microservice architecture offers a great benifits. One advantage is that of scalability. One can scale microservice architecture application in both vertical i.e. by increasing the allocation of heap memory as well as horizontal i.e. by making more then one instances available of same service.
Horizontal scaling is done by the help of Load Balancers.
Load Balancers
There may be situation when there is need to create numerous replicas. This is usually done to manage the traffic by the users.As the name suggest, Load Balancers helps in equal distribution of the traffic.Types of Load Balancers
Netflix Ribbon is one of the library of cloud that helps in providing the client side load balancing.
It uses some criteria and then decide which server to send the request.
In this example, we will be implementing Netflix Ribbon in a spring boot application. We will be calling instance of demo application multiple times(here 3 times), and then with the help of LoadBalancer Application, the process of Load Balancing will be done and multiple instances will be managed.
The algorithm uses by Netflix Ribbon for load balancing is Round Robin algorithm.
-
DemoApplication
This will be the standard directory layout for maven project structure for demo application-
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ribbonloadbalancing</groupId> <artifactId>myapp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>myapp</name> <description>Spring cloud Ribbon</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties file is as follows:- In this file we manually change the port and run the application. Here I have used 3 instances -8080, 8081 and 8082 and run the application.server.port=8082
The main class for the DemoApplication is as follows:-package com.codeusingjava; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { @Value("") private String port; @GetMapping("/test") public String getApp() { return "the port is "+port; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
LoadBalancerApplication Configuration
The Netflix Ribbon is configured in this application which will do the loadbalancing with the help of Round-Robin algorithm.
The project structure for the LoadBalancerApplication is as follows-
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.ribbonloadbalancing</groupId> <artifactId>LoadBalancer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>LoadBalancer</name> <description>Spring cloud Ribbon</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR6</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Creating the Configuration file application.properties-demo: ribbon: eureka: enabled: false listOfServers: localhost:8080,localhost:8081,localhost:8082 ServerListRefreshInterval: 15000 server: port: 8989
Creating the Configuration class RibbonConfiguration.
Here all the necessary bean related to the loadbalancing is defined.
Ping Component helps in determining the availability of server.
Rule is the logic component that helps in determining logic component in load balancing.
IClientConfig is used for the inilialisation of LoadBalancer.package com.codeusingjava.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IPing; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.PingUrl; import com.netflix.loadbalancer.WeightedResponseTimeRule; @Configuration public class RibbonConfiguration { @Autowired private IClientConfig iClientConfig; @Bean public IPing getPing() { return new PingUrl(); } public IRule getRule() { return new WeightedResponseTimeRule(); } }
The restTemplate is used for creating RESTful web applications.
To make the instance of restTemplate Load-Balanced, we mark it with @LoadBalanced annotation.
@RibbonClient helps in full configuration by giving control to the client. So now there is no need to provide the domain name and IP address.Instead we provide the name of the RibbonClient and the RibbonClient will take the configuration from the application.yml file.package com.codeusingjava; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.codeusingjava.config.RibbonConfiguration; @SpringBootApplication(scanBasePackages= { "com.netflix.client.config.IClientConfig" }) @RestController @RibbonClient(name="demo",configuration=RibbonConfiguration.class) public class LoadBalancerApplication { @Autowired private RestTemplate restTemplate; @GetMapping("/loadBalancingWithRibbon") public String getData() { return restTemplate.getForObject("http://demo/test", String.class); } @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(LoadBalancerApplication.class, args); } }
Run the Application
Firstly we run the DemoApplication as spring boot application with port 8080.
Now change the port number to 8081 and run again.
Now run the application again using port 8082.
Now we have three instances of the DemoApplication running .
Now we run the LoadBalancer Application and run the url.
The output is as follows:-
Now open the postman and enter the url as mentioned in LoadBalancerApplication
We see that when the request is executed, the round robin implementation approach is used for the purpose of Load Balancing.
Instance 1
Instance 2