1

想问一个关于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>

请帮忙 。

4

0 回答 0