【问题标题】:Increase Hystrix Circuit Breaker Timeout?增加 Hystrix 断路器超时?
【发布时间】:2016-08-06 11:54:52
【问题描述】:

我有一个 Hystrix 断路器的实现,在测试时我得到一个 Hystrix 运行时异常,错误是 CircuitBreker 超时和回退失败。我需要增加 CircutBreaker 的超时时间吗?如果代码超时,它应该只是跳闸吗?

我的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());
}

我的课程只是使用 CircuitBreaker 运行 web 获取/放置/等,以防万一发生故障。我的班级如下:

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;
}
}

【问题讨论】:

  • 您也可以在环境变量中更改超时时间,check here

标签: java http hystrix circuit-breaker


【解决方案1】:

您可以尝试增加 CircuitBreaker 的超时时间,看看会发生什么:

HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(5000)

因为根据Hystrix Wiki,HystrixCommand 的默认超时时间是 1 秒,你的 HttpGet 可能需要超过 1 秒才能返回一些东西。

【讨论】:

    【解决方案2】:

    您无需增加超时时间即可向 Google 发出简单的 get 请求。试试这个。

    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);
      }
    
    }
    

    还有一个简单的测试

    @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());
      }
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 2020-10-29
      • 2016-12-11
      • 2018-10-05
      • 2019-06-27
      • 2015-12-06
      • 2015-06-19
      • 2016-01-11
      • 2020-11-27
      相关资源
      最近更新 更多