I would like to programmatically force a circuit breaker to open for a particular group. I thought I might be able to do that by setting the config on a command in a group to force open, and running that command. However, that doesn't seem to work. Is this possible? Should I take a different approach? Here's the test I tried that fails on the 2nd assertEquals call.
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ForceCircuitBreakerCommandTest {
@Test
public void testForceOpen(){
assertEquals(Boolean.TRUE, new FakeCommand().execute());
new OpenCircuitBreakerCommand().execute();
assertEquals(Boolean.FALSE, new FakeCommand().execute());
}
private class FakeCommand extends HystrixCommand<Boolean> {
public FakeCommand(){
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup")));
}
@Override
public Boolean run(){return Boolean.TRUE;}
@Override
public Boolean getFallback() {return Boolean.FALSE;}
}
private class OpenCircuitBreakerCommand extends HystrixCommand<Boolean> {
public OpenCircuitBreakerCommand(){
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerForceOpen(true)));
}
@Override
public Boolean run(){return Boolean.TRUE;}
@Override
public Boolean getFallback() {return Boolean.FALSE;}
}
}
When in 5 seconds you will receive at least 20 requests (volume threshold) and the certain percentage of them will fail (error threshold) circuit will be opened for consecutive 5 seconds.
The CircuitBreaker can open when too many calls exceed a certain response time threshold, even before the remote system is unresponsive and exceptions are thrown. Hystrix only performs a single execution when in half-open state to determine whether to close a CircuitBreaker.
First, we need to add the Spring Cloud Starter Hystrix dependency in our build configuration file. Now, add the @EnableHystrix annotation into your main Spring Boot application class file. The @EnableHystrix annotation is used to enable the Hystrix functionalities into your Spring Boot application.
It implements the circuit breaker pattern which work on circuit-breaker transitions from CLOSED to OPEN when a circuit meets a specified threshold and error percentage exceeds the threshold error percentage. While it is open, it short-circuits all requests made against that circuit-breaker.
I have set custom properties such as "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen"
using
import com.netflix.config.ConfigurationManager;
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen",
true);
ConfigurationManager
is the Archaius instance that is used internally.
This is an edit to the test using Senthilkumar Gopal's answer
@Test
public void testForceOpen() {
assertEquals(Boolean.TRUE, new OpenCircuitBreakerCommand().execute());
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.OpenCircuitBreakerCommand.circuitBreaker.forceOpen",
true);
assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With