【发布时间】:2018-02-15 23:41:26
【问题描述】:
我们正在使用 apache http 客户端连接到外部帮助系统。我们正在使用 Hystrix 命令来执行 http 请求。 当这些请求需要更多时间来响应并且时间超过 Hystrix 超时时间时,Hystrix 将返回为 null 的回退。
由于它返回 null Http 响应,因此无法使用 EntityUtils 消费,因此连接不会返回到连接池。
我们已经尝试使用httpGet.releaseConnection。但它似乎不起作用。
当 http 请求的响应时间比预期时间长时,将连接释放回池的最佳方法是什么?
Hystrix 后退
@Override
protected CloseableHttpResponse getFallback() {
logger.error(" Returning fallback");
return null;
}
执行 REST 查询和处理结果的代码
CloseableHttpClient httpClient = //Get client from pool
HttpGet httpGet = new HttpGet(serverPath);
HystrixTestCommand testCommand = new HystrixTestCommand(httpClient, httpGet);
CloseableHttpResponse httpResponse = testCommand.execute();
if (httpResponse != null
&& httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//Consule entity
} else if (httpResponse != null
&& httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
//Consule entity
} else if(httpResponse == null){
// When http request not responded within anticipated time
httpGet.releaseConnection();
logger.info("Release connection");
return null;
}
【问题讨论】:
标签: java apache-httpclient-4.x hystrix