首页 > 解决方案 > Resilience4j Retry - 记录来自客户端的重试尝试?

问题描述

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

也许通过某种配置或设置。

目前,我正在使用基于 Spring Boot Webflux注释的弹性 4j 。

它工作得很好,这个项目很棒。

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

我期待看到类似“Resilience4j - 客户端:第一次尝试失败,因为 someException,retying 参加号码 2。第二次尝试失败,因为 someException,retying with参加号码 3。第三次尝试成功!”

类似的东西。是否有一个属性,一些配置,一些设置,可以帮助轻松做到这一点?无需添加过多的锅炉代码。

@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);
    }

}

谢谢

标签: javaspring-webfluxresilience4j

解决方案


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

您可以添加 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'.

推荐阅读