Implemention of Netflix Ribbon Client in Microservice

Posted By : Prajesh Rai | 30-Oct-2018

The idea behind to this Blog is Show some of the basic concepts about load balancing, Ribbon and Feign, and step by step process of working of Ribbon client with Feign Client. We can use spring cloud Netflix Feign Client (Open feign) to call another microservice and also use Ribbon client to provide the load balancing in the calls to microservice.

Load Balancing

Load Balancing is used to automatically distribute the incoming application request to the several other instances of the same application. Load Balancing is used to achieve the fault tolerance in your applications. The main use of load balancing to optimize the resource use and maximize throughput, minimize the response time and avoid the overhead of any single resource. Load Balancing is achieved when the single resource has multiple instance or copy, due to this we can distribute the request of the same request of this resource on multiple instances to increase reliability and availability through redundancy.

Feign - Load Balancer using Eureka 

Feign Client is a Netflix product which is used by microservices to communicate with each other.
If we want to use restTemplate to call other services then we need to programmatically construct the URL of dependent service, which is certainly not part of the business logic. To overcome this problem Netflix provides its own product know as Feign client.
Feign client works on declarative principle. We have to create an interface and annotated with Feign Client annotation.

Using Ribbon - Code:

 

First Service is example-service 


 

@SpringBootApplication @EnableEurekaServer public class ApplicationServer {     public static void main(String[] args) {         new SpringApplicationBuilder(ApplicationServer.class).web(true).run(args);     } }

File: application.properties


 

#Server Specifics
server.port=4200
error.whitelabel.enabled=false
spring.application.name: eureka-server
#Eureka Specifics
eureka.instance.hostname=localhost
eureka.client.registerWithEurek=false
eureka.client.fetchRegistry: false
eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/

Service is example-service 

 


 

@SpringBootApplication @EnableDiscoveryClient @RestController public class ExampleApplication {     @Autowired     DiscoveryClient client;     @RequestMapping("/")     public String helloWorld() {         ServiceInstance localInstance = client.getLocalServiceInstance();         return "Hello World: "+localInstance.getServiceId()+":"+localInstance.getHost()+":"+localInstance.getPort();     }     public static void main(String[] args) {         SpringApplication.run(HelloApplication.class, args);     } }

File: application.properties


 

spring.application.name=example-service server.port=4201 eureka.client.healthcheck.enabled=true eureka.client.serviceUrl.defaultZone=http://localhost:4200/eureka/

Should create other example-service on Port 4202
And the service that will consume this example-service is ribbon-service.


 

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class RibbonApplication {
    @Autowired
    HelloClient client;
    @RequestMapping("/")
    public String helloWorld() {
        return client.helloWorld();
    }
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
    @FeignClient("example-service")
    interface HelloClient {
        @RequestMapping(value = "/", method = GET)
        String helloWorld();
    }
}

File: application.properties


 

spring.application.name= ribbon-service server.port=4203 eureka.client.serviceUrl.defaultZone=http://localhost:4200/eureka/ eureka.instance.leaseRenewalIntervalInSeconds=10 eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}} endpoints.restart.enabled=true endpoints.shutdown.enabled=true endpoints.health.sensitive=false This service will be available on http://localhost:4203

First Service is example-service 

ribbon-service will ask Eureka the example-service address and will receive some address that is registered.
After all these configurations, our microservice easily communicate with each other.

Related Tags

About Author

Author Image
Prajesh Rai

Prajesh Rai is a Java Developer. Currently, he is working on Spring-boot. He always interested to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..