【发布时间】:2020-08-05 15:00:47
【问题描述】:
我想知道向我从 ExchangeFilterFunction 返回的 Mono 添加功能是否将应用于返回的最终 Mono。基本上,我想通过将一些关注点从客户端类移到可以添加到客户端的 ExchangeFilterFunctions 来清理我的一些客户端代码,但我的情况与单个调用有点不同。
基本上我有类似下面的代码:
Flux.fromIterable(uris)
.parallel()
.runOn(Schedulers.parallel())
.flatMap(uri -> webClient.get()
.uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.retrieve()
.onStatus(
status -> status.is4xxClientError() || status.is5xxServerError(),
response -> response.bodyToMono(String.class)
.map(body -> new MyCustomException(body, response.statusCode())))
.bodyToMono(MyResponse.class)
.map(x -> Tuple.of(x, null))
.onErrorMap(ReadTimeoutException.class, th ->
new MyCustomException(ErrorResponseBuilder.buildError(th), HttpStatus.INTERNAL_SERVER_ERROR)))
.doOnEach(this::log)
.sequential();
我的问题是我是否可以移出其中的一些逻辑,例如错误处理和可能重试逻辑以及对 ExchangeFilterFunctions 的附加日志记录,如果我这样做,它们在放入 Flux 时仍然会被应用吗?
此外,在 ExchangeFilterFunctions 中,我如何能够访问 SubscriberContext,因为在记录时我们需要的上下文中有数据,例如用于跟踪的请求的唯一 ID。
【问题讨论】:
标签: java spring-boot spring-webflux spring-webclient