【问题标题】:Resilience4j Retry - logging retry attempts from client?Resilience4j Retry - 记录来自客户端的重试尝试?
【发布时间】:2021-02-07 16:37:47
【问题描述】:

请问是否可以使用弹性 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);
    }

}

谢谢

【问题讨论】:

  • 您确定客户端必须重试吗?你怎么知道的?

标签: java spring-webflux resilience4j


【解决方案1】:

如果您在 Google 上搜索“resilience4j 重试示例日志记录”,网上似乎有很多关于此的信息。我发现这是一个潜在的解决方案:

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”。

【讨论】:

  • 基于注释的好吗?
  • @Retry 注释本身只接受一个“名称”参数。我在 Google 上搜索了更多信息,虽然我看到了很多关于设置重试和添加日志记录的信息,但我从来没有同时看到它们。
  • :'( :'( :'( :'(
  • @PatPatPat 使用RetryRegistry,看我的回答
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    使用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'.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2019-10-22
      相关资源
      最近更新 更多