跳到主要内容

使用 Resilience4j 重试的 HttpSender

DeepSeek V3 中英对照 HttpSender with Resilience4j retry HttpSender with Resilience4j Retry

HttpSender 是一个用于 HTTP 客户端的接口,这些客户端用于基于 HTTP 的指标发布。它有两个实现:

  • HttpUrlConnectionSender: 基于 HttpURLConnectionHttpSender

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

}
java