使用 Resilience4j 重试的 HttpSender
HttpSender
是一个用于 HTTP 客户端的接口,这些客户端用于基于 HTTP 的指标发布。它有两个实现:
-
HttpUrlConnectionSender
: 基于HttpURLConnection
的HttpSender
-
OkHttpSender
: 基于 OkHttp 的HttpSender
Micrometer 不支持重试,但你可以使用 Resilience4j 的重试功能来装饰它,如下所示:
@Bean
public DatadogMeterRegistry datadogMeterRegistry(DatadogConfig datadogConfig, Clock clock) {
return DatadogMeterRegistry.builder(datadogConfig)
.clock(clock)
.httpClient(new RetryHttpClient())
.build();
}
private static class RetryHttpClient extends HttpUrlConnectionSender {
private final RetryConfig retryConfig = RetryConfig.custom()
.maxAttempts(2)
.waitDuration(Duration.ofSeconds(5))
.build();
private final Retry retry = Retry.of("datadog-metrics", this.retryConfig);
@Override
public Response send(Request request) {
CheckedFunction0<Response> retryableSupplier = Retry.decorateCheckedSupplier(
this.retry,
() -> super.send(request));
return Try.of(retryableSupplier).get();
}
}