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

标签 java spring-webflux resilience4j

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

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

目前,我正在使用带有 Spring boot Webflux 基于注释的resilience4j。

效果很好,这个项目很棒。

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

我期待看到类似“Resilience4j - 客户端:第一次尝试因 someException 而失败,以参加号 2 重试。第二次尝试因 someException 而失败,以参加号 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);
    }

}

谢谢

最佳答案

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

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

关于java - Resilience4j Retry - 记录来自客户端的重试尝试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64519894/

相关文章:

java - WebFlux DataBufferLimitException : Part headers exceeded the memory usage limit of 8192 bytes

java - Spring webflux非阻塞响应

Java Recovery4j 重试策略在 2 次重试后变为无限

java - 在 JAX-RS 中使用 API key 进行身份验证

java - 无法使用 API Java SSL 和证书调用 EWS 服务

java - Spring 带注释的 Controller 可以工作,但路由器/处理程序方法似乎无法从 *ServerRequest* 检索 *Mono<>*

java - Resilience4J 断路器配置无法正常工作

java - Spring boot2 提示 CircuitBreakerConfigurationOnMissingBean

java - Lettuce 多个响应式 Redis 存储和跨存储交易

java - CDI Bean 的方法可以返回它创建的托管 Bean 吗?