We can specify Hystrix configurations in Spring's application.properties file.
Overriding defaults globally
To override default properties globally use following pattern in application.properties:
hystrix.command.default.<the-property>
Overriding defaults for specific methods
The properties can also be overridden on Hystrix command instance level by specifying a unique value for @HystrixCommand#commandKey and referring to that value by using following pattern in application.properties:
hystrix.command.<commandKey>.<the-property>
Example
Let's use our last example to demonstrate how to use Hystrix configuration in application.properties.
Using @HystrixCommand
package com.logicbig.example;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
@Service
@DefaultProperties
public class MyService {
@HystrixCommand(fallbackMethod = "defaultDoSomething", commandKey = "doSomethingKey")
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);
}
}
application.properties
src/main/resources/application.propertieshystrix.command.doSomethingKey.circuitBreaker.requestVolumeThreshold=2
hystrix.command.doSomethingKey.metrics.rollingStats.timeInMilliseconds=500
hystrix.command.default.circuitBreaker.errorThresholdPercentage=1
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=1000
x
Main class
package com.logicbig.example;
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 ProjectDependencies 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
|