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

CRUD in MongoDB & Spring Data

Now, since we have already been introduced to mongo and its server is setup. We shall now move on to performing basic CRUD operations. Lets take a use case example of ‘Company’ database that stores employee information.  We wish to store employee name, email address, age and multiple addresses. Traditionally in RDBMS we would create an Employee table and Address Table having foreign key reference to ‘employee id ‘ Incase of NoSQL, we will be creating Employee documnet which will have store employee information like name, email, age and an array of employes address. Following is a snippet of the schema defined { "name" : "", "email" : "", "age" : “”, "address" : [ ... ] } Thus to begin with, we will first define the entities. We have employee as an aggregate root entity that stores list of address having 1-M relatioship. Address Entity is represend as @Embeddable as it is embaded in another aggregate root entity. Employee is