WebClient 使用 ExchangeFilterFunctions 将处理程序添加到 Mono
WebClient Adding Handlers to Mono with ExchangeFilterFunctions
我想知道我从 ExchangeFilterFunction return 添加到 Mono 的功能是否会应用于获得 returned 的最终 Mono。基本上我想通过将一些关注点从客户端 class 移到我可以添加到客户端的 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。
我想知道我从 ExchangeFilterFunction return 添加到 Mono 的功能是否会应用于获得 returned 的最终 Mono。基本上我想通过将一些关注点从客户端 class 移到我可以添加到客户端的 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。