java - Resilience4j Retry - 记录来自客户端的重试尝试?
问题描述
请问是否可以使用resilience4j在客户端记录重试尝试?
也许通过某种配置或设置。
目前,我正在使用基于 Spring Boot Webflux注释的弹性 4j 。
它工作得很好,这个项目很棒。
当我们将服务器日志放在服务器端时,看到由于重试而进行了相同的 http 调用(我们记录时间、客户端 IP、请求 ID 等......)我可以拥有客户端日志吗?
我期待看到类似“Resilience4j - 客户端:第一次尝试失败,因为 someException,retying 参加号码 2。第二次尝试失败,因为 someException,retying with参加号码 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'.
推荐阅读
- python - 我正在尝试实施广度优先搜索
- python - How to convert a binary string into bipolar representation in python?
- shopify - Shopify 模版帮助 - 编辑“加入购物车”按钮
- c# - 在 C# 中从 String 转换为 BigInteger,而不切断前导零。
- javascript - Why can't we mutate prototype of Built-in Function Constructors?
- git - nbstripout: what does `diff=ipynb` do in .gitattributes?
- javascript - 在 php DOMDocument 中查询 javascript 值
- r - 自定义 ggscatterstats 中的中断和标签
- php - Display array format image from database in laravel
- java - 找不到使用 JNA 调用的 DLL 所需的文件