2016-04-14 53 views
2

我有一個Hystrix Circuit Breaker的實現,當測試時我得到一個Hystrix運行時異常,CircuitBreker超時和回退失敗。我需要增加CircutBreaker的超時時間嗎?如果代碼超時,是否應該讓斷路器跳閘?增加Hystrix斷路器超時?

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

我的班級就是運行網絡獲取/看跌/等失敗的情況下使用的斷路器莫名其妙。我的等級如下表所示:

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

回答

1

你可以嘗試增加你的斷路器超時,看看會發生什麼:

HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(5000) 

因爲根據Hystrix Wiki,HystrixCommand默認的超時時間爲1秒,並且您的HttpGet可能需要超過1秒纔會返回一些內容。

0

您不應該增加超時來向Google發送簡單的獲取請求。嘗試這個。

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