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