Close

Spring Cloud - Hystrix Circuit Breaker, Setting Configuration Properties Using @HystrixProperty

[Last Updated: Jul 29, 2019]

The hystrix default properties can selectively be overridden by using @HystrixProperty annotation.

Example

Let's use our previous example, and define our properties, so that the CircuitBreaker will trip in short time period, with few requests and with a low failure percentage.

Using @HystrixProperty

package com.logicbig.example;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class MyService {
  @HystrixCommand(fallbackMethod = "defaultDoSomething",
  commandProperties = {
          @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
          @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "500"),
          @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "1"),
          @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1000")

  })
  public void doSomething(int input) {
      System.out.println("output: " + 10 / input);
  }

  public void defaultDoSomething(int input, Throwable throwable) {
      System.out.printf("fallback, input:%s, exception:%s%n", input, throwable);
  }
}

Main class

package com.logicbig.example;

import com.netflix.hystrix.HystrixCommandProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableCircuitBreaker
public class CircuitBreakerMain {
  public static void main(String[] args) throws InterruptedException {
      ConfigurableApplicationContext ctx = SpringApplication.run(CircuitBreakerMain.class, args);
      MyService myService = ctx.getBean(MyService.class);

      int n = 5;
      for (int i = 0; i < n; i++) {
          myService.doSomething(i < 3 ? 0 : 2);
          TimeUnit.MILLISECONDS.sleep(200);
      }
      TimeUnit.SECONDS.sleep(2);
      System.out.println("-- final call --");
      myService.doSomething(2);
  }
}

Output

fallback, input:0, exception:java.lang.ArithmeticException: / by zero
fallback, input:0, exception:java.lang.ArithmeticException: / by zero
fallback, input:0, exception:java.lang.ArithmeticException: / by zero
fallback, input:2, exception:java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
fallback, input:2, exception:java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
-- final call --
output: 5

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.1.6.RELEASE
    Corresponding Spring Version 5.1.8.RELEASE
  • Spring Cloud Greenwich.SR2
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • spring-cloud-starter-netflix-hystrix 2.1.2.RELEASE: Spring Cloud Starter Netflix Hystrix.
  • JDK 1.8
  • Maven 3.5.4

Hystrix Circuit Breaker, Setting Configuration Properties Select All Download
  • circuit-breaker-hystrix-configuration-properties
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyService.java

    See Also