Resilience4j Retry - 记录来自客户端的重试尝试?

Resilience4j Retry - logging retry attempts from client?

是否可以使用 resilience4j 在客户端记录重试尝试?

可能是通过某种配置或设置。

目前,我正在使用 resilience4j 和 Spring 引导 Webflux 基于注释

效果很好,项目很棒。

当我们将服务器日志放在服务器端时,为了看到由于重试而进行了相同的 http 调用(我们记录时间、客户端 IP、请求 ID 等...)我是否可以拥有客户端日志?

我原以为会看到类似“Resilience4j - 客户端:第一次尝试因 someException 而失败,重新参加 2 号。第二次尝试因 someException 而失败,重新参加 3 号。第三次尝试成功!”。 =11=]

类似的东西。是否有 属性、一些配置、一些设置可以帮助轻松完成此操作?无需添加太多锅炉代码。

@RestController
public class TestController {

    private final WebClient webClient;

    public TestController(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8443/serviceBgreeting").build();
    }

    @GetMapping("/greeting")
    public Mono<String> greeting() {
        System.out.println("Greeting method is invoked ");
        return someRestCall();
    }

    @Retry(name = "greetingRetry")
    public Mono<String> someRestCall() {
        return this.webClient.get().retrieve().bodyToMono(String.class);
    }

}

谢谢

如果您Google“resilience4j retry example logging”,网络上似乎有很多关于此的信息。我发现这是一个潜在的解决方案:

RetryConfig config = RetryConfig.ofDefaults();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("flightSearchService", config);

...

Retry.EventPublisher publisher = retry.getEventPublisher();
publisher.onRetry(event -> System.out.println(event.toString()));

您可以在其中注册回调以在重试发生时获取事件。这个。来自“https://reflectoring.io/retry-with-resilience4j”。

幸好(或不幸)有一个未记录的功能:)

您可以添加 RegistryEventConsumer Bean,以便将事件使用者添加到任何 Retry 实例。

    @Bean
    public RegistryEventConsumer<Retry> myRetryRegistryEventConsumer() {

        return new RegistryEventConsumer<Retry>() {
            @Override
            public void onEntryAddedEvent(EntryAddedEvent<Retry> entryAddedEvent) {
                entryAddedEvent.getAddedEntry().getEventPublisher()
                   .onEvent(event -> LOG.info(event.toString()));
            }

            @Override
            public void onEntryRemovedEvent(EntryRemovedEvent<Retry> entryRemoveEvent) {

            }

            @Override
            public void onEntryReplacedEvent(EntryReplacedEvent<Retry> entryReplacedEvent) {

            }
        };
    }

日志条目如下所示:

2020-10-26T13:00:19.807034700+01:00[Europe/Berlin]: Retry 'backendA', waiting PT0.1S until attempt '1'. Last attempt failed with exception 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.

2020-10-26T13:00:19.912028800+01:00[Europe/Berlin]: Retry 'backendA', waiting PT0.1S until attempt '2'. Last attempt failed with exception 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.

2020-10-26T13:00:20.023250+01:00[Europe/Berlin]: Retry 'backendA' recorded a failed retry attempt. Number of retry attempts: '3'. Giving up. Last exception was: 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.

使用 application.properties 配置并使用 @Retry 注释,我设法通过

获得了一些输出
resilience4j.retry.instances.myRetry.maxAttempts=3
resilience4j.retry.instances.myRetry.waitDuration=1s
resilience4j.retry.instances.myRetry.enableExponentialBackoff=true
resilience4j.retry.instances.myRetry.exponentialBackoffMultiplier=2
resilience4j.retry.instances.myRetry.retryExceptions[0]=java.lang.Exception
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import io.github.resilience4j.retry.RetryRegistry;
import io.github.resilience4j.retry.annotation.Retry;

@Service
public class MyService {
    private static final Logger LOG = LoggerFactory.getLogger(MyService.class);

    public MyService(RetryRegistry retryRegistry) {
        // all
        retryRegistry.getAllRetries()
          .forEach(retry -> retry
            .getEventPublisher()
            .onRetry(event -> LOG.info("{}", event))
        );

       // or single
       retryRegistry
            .retry("myRetry")
            .getEventPublisher()
            .onRetry(event -> LOG.info("{}", event));
    }

    @Retry(name = "myRetry")
    public void doSomething() {
        throw new RuntimeException("It failed");
    }
}

例如

2021-03-31T07:42:23 [http-nio-8083-exec-1] INFO  [myService] - 2021-03-31T07:42:23.228892500Z[UTC]: Retry 'myRetry', waiting PT1S until attempt '1'. Last attempt failed with exception 'java.lang.RuntimeException: It failed'.
2021-03-31T07:42:24 [http-nio-8083-exec-1] INFO  [myService] - 2021-03-31T07:42:24.231504600Z[UTC]: Retry 'myRetry', waiting PT2S until attempt '2'. Last attempt failed with exception 'java.lang.RuntimeException: It failed'.