Spring boot webClient onErrorMap 在发生超时异常时从不调用

问题描述 投票:0回答:0

我有两个微服务 user-ms 和 post-ms user-ms 调用 post-ms 来获取某个用户的帖子数。
在 post-ms 中,我强制延迟 7 秒以查看 user-ms 的行为。

这里是user-ms里面的方法


    public Mono<PostsCountResponse> getPostsCountByUserUuid(UUID userUuid, int delay, int faultPercent) {

        URI url = UriComponentsBuilder.fromUriString(propertiesConfig.getVirtualPostServiceUrl()
                  + "/posts/count?userUuid={userUuid}&delay={delay}&faultPercent={faultPercent}").build(userUuid, delay, faultPercent);

        logger.info("Will call the findPostsCount API on URL: {}", url);
        
        return webClient.get().uri(url).accept(MediaType.APPLICATION_JSON).retrieve()
                .bodyToMono(PostsCountResponse.class)
                .onErrorMap(WebClientResponseException.class, ex -> handleException(ex));
                
    }

    private Throwable handleException(Throwable ex) {

        logger.info("starting handleException()..");

        if (!(ex instanceof WebClientResponseException)) {
            logger.warn("Got a unexpected error: {}, will rethrow it", ex.toString());
            return ex;
        }

        WebClientResponseException wcre = (WebClientResponseException) ex;

        switch (wcre.getStatusCode()) {

        case NOT_FOUND:
            return new NotFoundException(getErrorMessage(wcre));

        case UNPROCESSABLE_ENTITY:
            return new InvalidInputException(getErrorMessage(wcre));

        default:
            logger.warn("Got an unexpected HTTP error: {}, will rethrow it", wcre.getStatusCode());
            logger.warn("Error body: {}", wcre.getResponseBodyAsString());
            return ex;
        }
    }

超时异常在 user-ms 中按预期发生,但我不知道为什么永远不会调用 onErrorMap ?
所以我的问题是为什么不调用 onErrorMap 因为我想使用 handleException 方法处理超时异常?


这是来自 user-ms 的日志:-
instagram-clone-insta-ms-user-info-1  | 2023-05-09 21:50:10.358  INFO 1 --- [nio-8080-exec-5] c.j.i.u.i.PostServiceIntegrationImpl     : Will call the findPostsCount API on URL: http://post-ms/posts/count?userUuid=aeb7f3e9-3e66-4ad8-9bd4-15f2b6a09a7a&delay=7&faultPercent=0
instagram-clone-insta-ms-user-info-1  | 2023-05-09 21:50:12.363 ERROR 1 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 2000ms in 'onErrorResume' (and no fallback has been configured)] with root cause
instagram-clone-insta-ms-user-info-1  |
instagram-clone-insta-ms-user-info-1  | java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 2000ms in 'onErrorResume' (and no fallback has been configured)
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.handleTimeout(FluxTimeout.java:294) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.doTimeout(FluxTimeout.java:279) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.FluxTimeout$TimeoutTimeoutSubscriber.onNext(FluxTimeout.java:418) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.MonoDelay$MonoDelayRunnable.propagateDelay(MonoDelay.java:270) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.publisher.MonoDelay$MonoDelayRunnable.run(MonoDelay.java:285) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28) ~[reactor-core-3.4.7.jar:3.4.7]
instagram-clone-insta-ms-user-info-1  |         at java.base/java.util.concurrent.FutureTask.run(Unknown Source) ~[na:na]
instagram-clone-insta-ms-user-info-1  |         at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[na:na]
instagram-clone-insta-ms-user-info-1  |         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:na]
instagram-clone-insta-ms-user-info-1  |         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:na]
instagram-clone-insta-ms-user-info-1  |         at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
instagram-clone-insta-ms-user-info-1  |
java spring-boot asynchronous spring-webflux spring-webclient
© www.soinside.com 2019 - 2024. All rights reserved.