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.

Code

Dependencies:






























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


Comments

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 https://github.com/meta-magic/RedisCRUDexample.git  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. Employee.java import java.io.Serializable ; 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

Function Point Analysis : ISO/IEC 20926:2009

This blog focuses on explaining the Function Point calculations. Software Sizing Background Function Point Rules for Counting FP Deep Dive - Function Point Analysis Case Study General Software Characteristics Details History - Measurement Methodologies Lines of Code (Oldest) Use case based Software Sizing IPFUG Function Point Analysis (ISO) Need for Software Sizing. Estimation and Budgeting Phasing Development Work Prioritization of Work Monitoring the Progress Bidding for Projects Allocating Testing Resources To measure and Manage Productivity Risk Assessment Software Asset Valuation CMMi Level 2 and 3 require that a valid sizing method be used. Software Sizing - Lines of Code The easiest and historically the most common method in Sizing Software project has been counting the number of lines of code and / or the number of screens. Advantages Automation of the counting process can be done Intuitive as the measurements are easily u

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 http://download.redis.io/redis-stable.tar.gz 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