Resilience4j重试-记录来自客户端的重试尝试? [英] Resilience4j Retry - logging retry attempts from client?

查看:0
本文介绍了Resilience4j重试-记录来自客户端的重试尝试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用弹性4j记录客户端的重试尝试?

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

目前,我正在使用基于Spring Boot Webflow批注的Resilience4j。

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

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

我期望看到类似";Resilience4j-客户端:第一次尝试失败,原因是出现异常,正在重试,出席人数为2。第二次尝试失败,原因是出现异常,重试出席人数为3。第三次尝试成功!&q;

差不多吧。有没有一个属性,一些配置,一些设置,可以帮助轻松做到这一点,请?而不添加太多锅炉代码。

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

}

谢谢

推荐答案

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

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

    @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'.

这篇关于Resilience4j重试-记录来自客户端的重试尝试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆