如何解决Resilience4j重试-从客户端记录重试尝试?
是否可以通过resilience4j在客户端记录重试尝试?
也许通过某种配置或设置。
当前,我将Resilience4j与Spring boot Webflux 基于注释一起使用。
效果很好,项目很棒。
虽然我们将服务器日志放在服务器端,但是要看到由于重试而进行了相同的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);
}
}
谢谢
解决方法
如果您使用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”。
,幸运的是(或不幸的是)有一个未记录的功能:)
您可以添加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'.
,
使用 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'.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。