我使用resilience4j重试策略来调用HttpGet请求,并且为了测试目的,
我已经将retryOnResult设置为当HttpGet请求返回200状态代码时重试。
当maxAttempts设置为2时,它成功地重试。
对于maxAttempts > 2应用程序来说,它处于无限状态。
public class App {
public static void main(String[] args) {
HttpClient client = HttpClients.createDefault();
HttpRequest request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
HttpResponse response;
try {
RetryConfig retryConfig = RetryConfig.<HttpResponse>custom().waitDuration(Duration.ofSeconds(2))
.maxAttempts(3).retryOnResult(s -> {
return s.getStatusLine().getStatusCode() == 200;
}).build();
RetryRegistry registry = RetryRegistry.of(retryConfig);
Retry retry = registry.retry("Http client");
retry.getEventPublisher().onRetry(e -> {
System.out.println("Retrying");
});
CheckedFunction0<HttpResponse> retryableSupplier = Retry.decorateCheckedSupplier(retry,
() -> client.execute((HttpUriRequest) request));
response = Try.of(retryableSupplier).get();
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>发布于 2019-10-24 06:38:03
终于找到了根本原因。问题不在于resiliency4j。但是在上面的场景中,同一个HttpGet请求在重试场景中被多次调用。默认情况下是httpclient creates a pool with size of 2。因此,在使用2之后,它将无限期地等待从池中获取第三个连接。
最后工作守则:
public class App {
public static int retryCounter = 0;
public static void main(String[] args) {
int maxAttempts = 4;
HttpClient client = HttpClients.createDefault();
HttpRequest request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
HttpResponse response;
try {
RetryConfig retryConfig = RetryConfig.<HttpResponse>custom().waitDuration(Duration.ofSeconds(1))
.maxAttempts(maxAttempts).retryOnResult(s -> {
try {
if (s.getStatusLine().getStatusCode() == 200) {
if (retryCounter < maxAttempts -1) {
s.getEntity().getContent().close();
}
return true;
} else {
return false;
}
} catch (UnsupportedOperationException e1) {
return true;
} catch (IOException e1) {
// TODO Auto-generated catch block
return true;
}
}).build();
RetryRegistry registry = RetryRegistry.of(retryConfig);
Retry retry = registry.retry("Http client");
retry.getEventPublisher().onRetry(e -> {
retryCounter ++;
System.out.println("Retrying" + e.getNumberOfRetryAttempts());
});
CheckedFunction0<HttpResponse> retryableSupplier = Retry.decorateCheckedSupplier(retry, () -> {
HttpResponse res = client.execute((HttpUriRequest) request);
return res;
});
response = (CloseableHttpResponse) Try.of(retryableSupplier).get();
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block`enter code here`
e.printStackTrace();
}
}发布于 2019-10-18 12:04:04
那不是真的。如果尝试次数大于maxAttempts,最后的结果将返回给客户端。这不是一个无限循环。
public static void main(String[] args) {
String response;
RetryConfig retryConfig = RetryConfig.<String>custom().waitDuration(Duration.ofMillis(100))
.maxAttempts(3).retryOnResult(s -> {
return s.contains("Hello");
}).build();
RetryRegistry registry = RetryRegistry.of(retryConfig);
Retry retry = registry.retry("Http client");
retry.getEventPublisher().onRetry(e -> {
System.out.println("Retrying");
});
CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry,
() -> "Hello World");
response = Try.of(retryableSupplier).get();
System.out.println(response);
}结果在
Retrying
Retrying
Hello World第三次尝试也“失败”,但结果返回给客户端。
https://stackoverflow.com/questions/58449663
复制相似问题