当前位置: 首页 > 知识库问答 >
问题:

Resilience4j重试-记录来自客户端的重试尝试?

仲孙超
2023-03-14

差不多吧。是否有一个属性,一些配置,一些设置,可以帮助做到这一点很容易请?不用添加太多锅炉代码。

@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);
    }

}

谢谢。

共有1个答案

慕乐池
2023-03-14

幸运的是(或者不幸的是)有一个未记录的特性:)

您可以添加RegistryEventConsumer Bean,以便将事件使用者添加到任何重试实例中。

    @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'.
 类似资料:
  • 如何在基于网络的 HTTP 客户端中重试 HTTP 请求? 请考虑以下处理程序,如果收到 HTTP 响应代码 503,它将尝试在 1 秒后重试 HTTP 请求: 在本例中,当我写入通道时,管道中的其他处理程序会看到HttpObjects,但实际上不会再次执行HttpRequest——只接收到一个HttpResponse。 我认为在这种情况下我只是滥用了 Channel,我需要创建一个新的通道(表示

  • 我已经为一个服务实现了socket.io,然后删除了它。但是,我仍然有客户机向endpoint/socket.io发出初始连接请求,而endpoint/socket.io已经不存在了。使用各种http状态代码进行响应似乎并不重要。 是否有方法阻止客户端尝试连接?我每分钟收到几千个请求。

  • 我有一个需求,从一个Kafka主题消费,做一些工作的记录和生产到另一个主题与spring-kafka 2.1.7。其他需求是事务性的,只有一次语义,重试和错误处理。当提交一个记录失败时,我应该做3次重试,记录每个重试消息重试主题。当所有重试失败时,将记录发送到一个死信主题。我查看了https://github.com/spring-projects/spring-kafka/issues/575,

  • 我在Spring Boot项目中使用Resilience4J调用REST客户机,如下所示: 看到示例中包含了一个回退方法,我决定添加它,尽管我并不真的想调用不同的方法,我只想再次调用我的原始方法。 不管怎样,我指定了一个回落: 现在,我看到回退方法被重试,但是每次都会抛出HttpServerErrorException,这意味着使用者将收到一个异常作为对其调用的响应。 谢谢

  • Summary This section describes how to check for Client Side URL Redirection, also known as Open Redirection. It is an input validation flaw that exists when an application accepts an user controlled i