JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Software Development Team? Take two weeks Trial! Try Now

A Complete Guide about Spring Cloud Feign and Hystrix Circuit Breaker


Hystrix Circuit Breaker

I am going to explain how you can be able to create declarative Rest Clients with Feign. And will illustrate how you will be able to call REST services using the Feign libraries. The interesting thing is that you can easily understand from this article that how Feign, Ribbon, and Eureka collaborate. If you do not know about Ribbon and Eureka, then please refer to our specific blogs on Eureka Service Discovery and Spring Ribbon.

What is Feign?

Feign is another part of the Netflix open-source software library i.e. easily usable within Spring Cloud. Feign allows us to write calls to Restful Services using a declarative style that results in no actual implementation code. Please be clear that here I am not talking about server-side code. I am referring to the client-side code that makes a call to server-side code that might be written in any technology or any programming languages.

Feign declarative client is even easier than the “RestTemplate” that we normally use to call rest services. Spring Cloud provides an easy wrapper for using Feign. Now, I want to give you an example of RestClient i.e. part of the Spring framework. Please look at the below example below code

RestTemplate tp = new RestTemplate(); String url = “http://inventoryService/{0};// This is the target url with the placeholder String s = tp.getForObject(url, String.class, 11)// Calls the URL, placeholder value is provided 11. // Template takes care of all HTTP and type conversion

Now, Feign is an alternative to technology like the RestTemplate.

How Feign Works?

Feign Runtime Implementations:

We have to enable Feign functionality via the “@EnableFeignClients” annotation in one of our spring configuration classes. When the application starts up, the Feign libraries will see the annotations and provide runtime implementations of exactly what we told it to build. Please see the below example:

@SpringBootApplication @EnableFeignClients // Spring will seek interfaces and implement them Public class Application{ }

What does @EnableFeignClients annotation do Internally?

Before the application is running, what you would see in your codebase are the interfaces annotated with the “@FeignClient”. But when you run the application, Feign takes over and automatically provides the run time implementation (illustrated in the blue box in the below image). Please look at the below image.

Hystrix Circuit Breaker

Feign dependency:

Hystrix Circuit Breaker

Demo on Feign Declarative REST clients:

Before starting with Feign, first, you have to start your common-config-server and the common-eureka-server. You can visit our separate blog for the Eureka server setup and config server setup. Check the Eureka server running in your localhost.

NOTE: You have to create one more spring boot application with the Profiles called “profiles: noun” in your bootstrap yaml file and then you have to provide the implementation for the getPerson() method with @GetMapping annotation. In this method, you can implement some logic. So, this method will

Spring Hystrix Overview:

Let’s explore a scenario known as “Cascade failure”:

Hystrix -The Software Circuit Breaker Implementation:

Hystrix Setup:

Failure/Recovery behavior can be easily customizable with Hystrix

Hystrix allows us is a good deal of fine-tuning regarding failure detection and recovery behavior. In the below example, I have adjusted the error threshold.

Customizable Recovery behavior:

// Based on the recent failures Hystrix will call one of these below two methods // Here, after 1 sec, it will try closing breaker // when 20% failure rate in 10sec period then open the breaker. @HystrixCommand(fallbackMethod = ”defaultStores”, commandProperties = { @ HystrixProperty(name = ”circuitBreaker.errorThresholdPercentage”, value = ”20”), @HystrixProperty(name = ”circuitBreaker.sleepWindowInMilliseconds”, value = ”1000”) }) public Object getStores(Map < String, Object > params) { // … } }

In the above example, if Hystrix detects a 20% failure rate over a 10-sec moving window of time, it will trip the breaker. In our example, I have determined that 1sec reset time. Hystrix commands give us nice options for how we want the target logic to be invoked. The default behavior is “Synchronous Execution”. We execute the command and when it is done then we get the control back.

We also have the option of “Asynchronous Execution” where we can fire the command in a separate thread. This is the same that we can do with the spring’s “@Async” annotation except that we are adding the Hystrix circuit breaker features to it.

Also, Hystrix provides a “Reactive” model which is also asynchronous. It is better because here we do not need to query a future object to see if it is done unlike in the Asynchronous case. We can have Observable that will be executed and as soon as the work is done the responding logic will be fired and therefore all we have to do is listen to the event.

Asynchronous Command Execution Example:

I am giving you an example of Asynchronous command execution via Hystrix. See the below code snippet:

@HystrixCommand(…) public Future < Store > getStores(Map < String, Object > param) { Return new AsyncResult < Store > () { @Override public Store invoke() { //do stuff that might fail} }; }

The main difference is the Return type. Any return type based on a Java future tells Hystrix to invoke the method in a separate thread. The caller of the method gets the future immediately and has the option of checking the future to see if it is done.

Reactive Command Execution Example:

I am giving you an example of Reactive Command Execution via Hystrix. See the below code snippet:

@HystrixCommand(…) public Observable < Store > getStores(Map < String, Object > param) { Return new ObservableResult < Store > () { @Override public Store invoke() { //do stuff that might fail} }; }

Notice that in the above code, the return value is kind of observable. This instructs hystrix to use the reactive model for invocation. The caller of this code will get the observable and can subscribe to it as a listener. The listening code will be invoked automatically as soon as the call is complete. The method needs to return an observable result. This “Observable” is from JAX-RS. There is many useful properties available that you can set to fine-tune the behavior


Resetting the Circuit Breaker:

The Hystrix circuit breaker is designed to reset itself. The main thing is to control is how long we would like the circuit breaker to stay open before Hystrix tries to close it again. This is controlled by the “circuitBreaker.sleepWindowinMilliseconds” properties. We can also force the circuit breaker open using the “circuitBreaker.forceClosed” property. This is for manual purposes.


We can monitor everything with Hystrix Dashboard and Turbine. Hystrix provides a built-in dashboard to check the status of the circuit breakers. For a large number of microservices, Hystrix dashboard is not really practical. So, Turbine is the solution for this. It aggregates the streams of all of the other Hystrix enabled services.

DEMO: Implementing Hystrix Circuit Breaker (with Feign client and Eureka) In your Project:

Then create a Rest controller class called “”. An added string array of names with @Value annotation. So that these names value can be read from our application.yml file. And in the controller class, I have added a controller method called “getNames()” with Get mapping. This method returns a string value from the “names” array with a dynamically chosen index. Example:

Hystrix Circuit Breaker


So, finally, at the end of our discussion, you learned how Feign provides a very easy way to call RESTful Services. Feign integrates with Ribbon and Eureka automatically. Also, you learned how Hystrix circuit breakers protect against cascade failure and how easy it is to add a Hystrix circuit breaker to our application.


Ast Note

Some of our clients