微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webflux 和 Resilience4j 重试问题

如何解决Webflux 和 Resilience4j 重试问题

想问一个关于webflux和resilience4j重试的基本问题。 我们正在使用 Webflux 和 Resilience4J(不是 Spring Cloud Circuit Breaker)运行 Java SpringBoot。 在运行应用程序时,它给出了错误(不知何故,我没有完整的堆栈跟踪):

due to exception [reactor.core.publisher.Mono.retrywhen(Ljava/util/function/Function;)Lreactor/core/publisher/Mono;]

构建 Retry bean 的代码如下(我们没有使用基于 YAML 的配置):

    @Bean
public Retry retryConfig(ResilienceCCMConfig resilienceCCMConfig) {
    RetryConfig config = RetryConfig.custom().maxAttempts(5)
            .waitDuration(Duration.of(1,SECONDS))
            .retryExceptions(ServerException.class)
            .ignoreExceptions(ClientException.class)
            .build();
    return RetryRegistry.of(config).retry("retry-config",config);
}

现在我们使用从上面注入的 bean 调用 Web 客户端的 POST 方法,如下所示:

    @Autowired
private Retry retryConfig;


return webClient.post().uri(url)
            .headers(httpHeaders -> getItemHeaders(httpHeaders,tenantId,bannerId,correlationId))
            .body(itemServiceRequestMono,ItemServiceRequest.class)
            .retrieve()
            .onStatus(HttpStatus::isError,clientResponse -> {
                LOGGER.error("Error");
                if (clientResponse.statusCode().is5xxClientError()) {
                    throw new Exception("Something went wrong ");
                           
                }
               
                return Mono.empty();
            })
            .bodyToMono(MyResponse.class)
            .transform(RetryOperator.of(retryConfig))
            .onErrorResume(ex -> myFallbackMethod(ex));

POM 依赖:

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.4.3</version>
        </dependency>

        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-reactor</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId> io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>1.6.1</version>
        </dependency>

请帮忙。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。