Skip to main content

Ribbon , Hysterix using Spring Feign

The idea about this post is show some concept of Load Balancing  & Circuit Breaker using Spring Cloud Netflix API.

Load Balancing

Load Balancing automatically distributes incoming application traffic between number of nodes running for given application.
Ribbon :
This provide client side load balancing. Its component offers a good set of configuration options such as connection timeouts, retries, retry algorithm  etc.
Strategies offered by ribbon are listed below:
  • Simple Round Robin LB
  • Weighted Response Time LB
  • Zone Aware Round Robin LB
  • Random LB

Circuit Breaker pattern

Netflix’s Hystrix library provides an implementation of the Circuit Breaker pattern: when we apply a circuit breaker to a method, Hystrix watches for failing calls to that method, and if failures build up to a threshold, Hystrix opens the circuit so that subsequent calls automatically fail. While the circuit is open, Hystrix redirects calls to the method, and they’re passed on to our specified fallback method.

Spring provides Spring Cloud Fiegn API which takes care of the Load Balancing using Ribbon API and Fallback mechanism using Hysterix API.

Declarative REST Client: Feign

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
In other POST "Building a RESTful Web Service" we have developed  "auth-service" service application having RESTful web service, there we have also register application to eureka-server.
Now we will develop "auth-client-service" application which implements Load Balancing and Circuit Breaker pattern for "auth-service" application.
In Microservice world client request will be never directly hit "auth-service" endpoints. It will be as below.
Client request -> API Gateway (Zuul) -> Auth-Client-Service (Ribbon & Hysterix) -> Auth-Service.



Config files:
As we are using spring-cloud config below will be bootstarp.yml in application.

All application related configuration will be defined in authclientservice.yml and stored at centralized location.
Java Code:

Below Rest Controller which will be accessed by client, once request comes it will redirect request to respective service application at respective node using ribbon. If in case its not able to communicate any of the code for given application fallback method will be called using Hysterix.

Code using Feign API

Define Service interface have end-point definition which is exactly same end-point defined in “auth-service” .

Define Feign Client, here you need to define service application name and fallback class.

Now define Fallback code for every end-point which is defined in service interface.

Start and execute below commands to see results.

Auth-Client-Service application is started at 7072 port.
Auth-Service application is running on 9092 and 9093.
1 – We will request rest endpoint defined in “auth-client-service” and see how request is send to “auth-service”, which sends back proper response.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’

curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’

Load Balancing:
We will execute below set of curl commands multiple times and check logs of “auth-service” running on both node.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’
We have executed above curl command 5 times. If you look into below screenshot you can see request is routed to both node in Round Robin manner (Feign uses Ribbon internally for load balancing).

2 – Now we will stop both nodes of “Auth-Service” and see how fallback method gets executed.
Eureka – Server showing all the nodes are down for Auth-Service.

Executed below curl command.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’

If you look into response its saying required service is unavailable please try after some time, this response is defined in fallback class which we have defined.
Now we will start auth-service application and send same curl request, you should be getting proper response defined in auth-service end-point.

Next we will take a look in Zuul Edge Server.

Cheers – Happy learning 🙂
Ketan Gote


Popular posts from this blog

Redis Basic CRUD

We have seen how to setup on your linux machine here , now we will see how to perform basic CRUD operations using Spring Data & Redis server We will be creating a simple application that would persist an employee information into redis database. You will be needing certain JARs like jedis.jar, spring-data-redis.jar etc details of which you can download and view at  First of all we will be creating the Employee entity, plz note that we are using the Serializable interface which automatically mapps the objects against the key. import ; public class Employee implements Serializable { private static final long serialVersionUID = - 8243145429438016231L ; public enum Gender { Male , Female } private String id; private String name; private Gender gender; private int age; public String getId () { return id; } public void setId ( String

Eureka-Server with spring cloud netflix

In this write-up we will focus on Service Registry – Eureka Server Rest service (auth-service application, eureka client) which register itself to registry. Web application which consumes Rest service using service registry. Service discovery  allows services to find and communicate with each other without hardcoding hostname and port. Eureka Server In spring-boot application enable the Eureka-Server by adding @EnableEurekaServer annotation to spring boot application. We have put all the configuration on GIT and this is accessed using config-server. To now more about centralized configuration (config-server) click  here eurekaserver.yml Place below bootstrap.yml in application, it basically connects to config-server and gets it required configuration. Start the spring-boot application and access eureka server using http://localhost:8760/ you will get below screen. Right now there is no application which is registered to e

Redis Installation Steps

“ Redis is an in-memory key-value store known for its flexibility, performance, and wide language support” Inorder to install redis on your machine you need ubuntu 16.4 and a non-root user with sudo privileges to perform the administrative functions required for this process. Download and Extract the Source Code Create a tmp directory cd /tmp Download the latest stable version of Redis curl -O untar tar xzvf redis-stable.tar.gz Move into the Redis source directory structure that was just extracted cd redis-stable Build and Install Redis Now, we can compile the Redis binaries by typing make After the binaries are compiled, run the test suite to make sure everything was built correctly. You can do this by typing: make test This will typically take a few minutes to run. Once it is complete, you can install the binaries onto the system by typing: sudo