[英]Increase Hystrix Circuit Breaker Timeout?
I have an implementation of Hystrix Circuit Breaker and when testing I'm getting a Hystrix Runtime Exception with the error that The CircuitBreker timed-out and fallback failed.我有一个 Hystrix Circuit Breaker 的实现,在测试时我收到了一个 Hystrix 运行时异常,错误是 The CircuitBreker 超时和回退失败。 Do I need to increase a timeout on the CircutBreaker?我是否需要增加 CircutBreaker 的超时时间? Should it just trip the circuit breaker if the code times out?如果代码超时,它是否应该使断路器跳闸?
My junit test is as follows:我的junit测试如下:
@Test
public void test4(){
client = new DefaultHttpClient();
httpget = new HttpGet("http://www.google.com:81");
resp = new CircuitBreaker(client, "test4", httpget).execute();
//assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, resp.getStatusLine().getStatusCode());
System.out.println(resp.getStatusLine().getStatusCode());
}
My Class is just to run web gets/puts/etc using the CircuitBreaker in case of failure somehow.我的班级只是在以某种方式失败时使用断路器运行 web 获取/放置/等。 My class is as follows:我的课如下:
public class CircuitBreaker extends HystrixCommand<HttpResponse> {
private HttpClient client;
private HttpRequestBase req;
protected String key;
//Set up logger
private static final Logger logger = (Logger)LoggerFactory.getLogger(CircuitBreaker.class);
/*
* This method is a constructor and sets http client based on provides args.
* This version accepts user input Hystrix key.
*/
public CircuitBreaker (HttpClient client, String key, HttpRequestBase req, int threshold) {
super(HystrixCommandGroupKey.Factory.asKey(key));
this.client = client;
this.key = key;
this.req = req;
logger.info("Hystrix Circut Breaker with Hystrix key:" + key);
logger.setLevel(Level.DEBUG);
HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(threshold);
//HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(50);
}
/*
* This method is a constructor and sets http client based on provides args.
* This version uses the default threshold of 50% failures if one isn't provided.
*/
public CircuitBreaker (HttpClient client,String key, HttpRequestBase req){
this(client, key, req, 50);
}
/*
* This method runs the command and returns the response.
*/
@Override
protected HttpResponse run() throws Exception {
HttpResponse resp = null;
resp = client.execute(req);
if (resp != null)
logger.info("Request to " + req.getURI() + " succeeded!");
return resp;
}
/*
* Fallback method in in the event the circuit breaker is tripped.
* Overriding the default fallback implemented by Hystrix that just throws an exception.
* @see com.netflix.hystrix.HystrixCommand#getFallback()
*/
@Override
protected HttpResponse getFallback() {
//For later expansion as needed.
logger.error("Circuit Breaker has " + getExecutionEvents() + ". Reason: "+ getFailedExecutionException().getMessage());
return null;
}
}
You can try to increase the timeout on your CircuitBreaker and see what happens:您可以尝试增加 CircuitBreaker 的超时时间,看看会发生什么:
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(5000)
Because according to the Hystrix Wiki , the default timeout of HystrixCommand is 1 second, and it might take more than 1 second for your HttpGet return something.因为根据Hystrix Wiki ,HystrixCommand 的默认超时时间是 1 秒,而您的 HttpGet 返回内容可能需要 1 秒以上。
You shouldn't need to increase the timeout to make a simple get request to google.您不需要增加超时时间来向 google 发出简单的 get 请求。 Try this.试试这个。
public class HttpCommand extends HystrixCommand<HttpResponse> {
private final HttpClient client;
private final HttpRequestBase req;
public HttpCommand(HttpClient client, HttpRequestBase req) {
super(HystrixCommandGroupKey.Factory.asKey("HttpCommandGroup"));
this.client = client;
this.req = req;
}
@Override
protected HttpResponse run() throws Exception {
return client.execute(req);
}
}
And a simple test和一个简单的测试
@Test
public void executeCommandTest(){
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet("http://www.google.com");
HttpResponse resp = new HttpCommand(client, httpget).execute();
assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.