簡體   English   中英

Resilience4j 重試 - 從客戶端記錄重試嘗試?

[英]Resilience4j Retry - logging retry attempts from client?

是否可以使用resilience4j在客戶端記錄重試嘗試?

也許通過某種配置或設置。

目前,我使用的是基於Spring boot Webflux注釋的彈性4j。

它工作得很好,這個項目很棒。

當我們將服務器日志放在服務器端時,為了查看由於重試(我們記錄時間、客戶端 IP、請求 ID 等)而進行了相同的 http 調用,我可以擁有客戶端日志嗎?

我期待看到類似“Resilience4j - 客戶端:第 1 次嘗試因 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'.

如果您在 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”。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM