我想以编程方式强制断路器为特定组开放。我想我可以通过在组中的命令上设置配置来强制打开并运行该命令来做到这一点。但是,这似乎不起作用。这可能吗?我应该采取不同的方法吗?这是我试过的第二次assertEquals调用失败的测试。
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;}
}
}
答案 0 :(得分:15)
我已使用
设置自定义属性,例如"hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen"
import com.netflix.config.ConfigurationManager;
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen",
true);
ConfigurationManager
是内部使用的Archaius实例。
答案 1 :(得分:0)
您不一定要使用ConfigurationManager
。该测试需要说:
@Test
public void testForceOpen() {
assertEquals(Boolean.TRUE, new FakeCommand().execute());
assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute());
}
答案 2 :(得分:0)
这是使用Senthilkumar Gopal的答案
对测试的修改@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());
}