Spring Cloud Eureka and Hystrix Circuit Breaker using Microservices

In this tutorial, we will use a microservice application created in previous post ( Microservices Example using Spring Cloud Eureka ) and add circuit breaker pattern using Hystrix Spring library in java.

Using Hystrix in your application helps to add defensive mechanism and makes applications more resilient and fault tolerant.

Tools Required –

  • Java 8
  • IntelliJ IDE

We have created three different applications as below –

  1. Eureka Service–  This Service will register every microservice and then the client microservice will look up the Eureka server to get a dependent microservice to get the job done.This Eureka Server is owned by Netflix and in this, Spring Cloud offers a declarative way to register and invoke services by using Java annotation.
  2. demo-server – This service will return a simple hello message.
  3. demo-client – It is similar to the standalone client service created in Bootiful Development with Spring Boot. It will consume the APIs provided by demo-server through Eureka Service .

Hystrix Documentationhttps://github.com/Netflix/Hystrix/wiki

Microservices are deployed on Cloud . As cloud provides a distributed environment , there are more chances that some of your services may be down at some point of time. You can have several micro-services in your application which are dependent on each other. So one service can call to other service. If destination service is down then source will get an exception in normal scenario. But with the help of Hystrix annotations , you can add fallback mechanism and handle the exception in services. Thus it makes your service more fault tolerant, resilient .

You need to add below dependency in your demo-client service application to enable Hystrix circuit breaker pattern –


We just have to add few annotations to handle fallback or break the service call in case your destination service(demo-server) is down. We need to change main class to enable Hystrix circuit breaker –

package com.myjavablog.democlient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

public class DemoClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoClientApplication.class, args);

class Config{
    public RestTemplate restTemplate(){
        return new RestTemplate();


Also we need to change controller class to add fallback mehod as below –

package com.myjavablog.democlient;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

public class TestController {

    public RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "handleFallback")
    public String test(){
        String url = "http://demo-server/demo/hello/server";
        return restTemplate.getForObject(url, String.class);

    public String handleFallback(){
        return "Fallback hello service";

By default hystrix has timeout of 1 second for every request. So we need to disable timeout by setting below property in application.properties file –


So now i am intentionally stopping demo-server microservice and then we will make call to api to see fallback is working properly .You can see demo-server is not registered with Eureka below –

So now when you call the service ,it should show you a fallback message as below –

Github Downlod Link:

Leave a Comment