gpt4 book ai didi

java - Spring webflux : how to publish event from sync call for async processing?

转载 作者:行者123 更新时间:2023-12-01 22:29:12 27 4
gpt4 key购买 nike

我有一个正在同步调用的方法。通过这个方法,我想发布另一个将异步处理的事件。例如:

正在使用参数调用同步方法 M,该参数需要发送到其他服务器 X。M 应该尽快返回,但 X 可能会宕机。如果 X 关闭,异步处理应在几秒后重试几次

所以我有处理单个事件/参数的代码:

 return webClient
.post()
.uri(...)
.syncBody(...)
.retrieve().bodyToMono(...)
.retryWhen {...}
.subscribeOn(Schedulers.boundedElastic())

但是我如何从同步调用中发布新事件/参数?

最佳答案

Spring 提供的一个很好的开箱即用的解决方案称为 Spring Cloud Gateway这正是您所要求的:

它是一个无代码应用程序,您可以立即启动并使用 yaml 配置配置所有路由,并且可以通过配置轻松地为每个路由应用所需的重试退避等:

它提供了一个内置的重试 GatewayFilter Factory,可以执行所需的解决方案。

The Retry GatewayFilter factory supports the following parameters:

retries: The number of retries that should be attempted.

statuses: The HTTP status codes that should be retried, represented by using org.springframework.http.HttpStatus.

methods: The HTTP methods that should be retried, represented by using org.springframework.http.HttpMethod.

series: The series of status codes to be retried, represented by using org.springframework.http.HttpStatus.Series.

exceptions: A list of thrown exceptions that should be retried.

backoff: The configured exponential backoff for the retries. Retries are performed after a backoff interval of firstBackoff * (factor ^ n), where n is the iteration. If maxBackoff is configured, the maximum backoff applied is limited to maxBackoff. If basedOnPreviousValue is true, the backoff is calculated byusing prevBackoff * factor.

The following defaults are configured for Retry filter, if enabled:

retries: Three times

series: 5XX series

methods: GET method

exceptions: IOException and TimeoutException

backoff: disabled

示例 Yaml 配置再次无需代码:) :

spring:
cloud:
gateway:
routes:
- id: retry_test
uri: http://localhost:8080/flakey
predicates:
- Host=*.retry.com
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
methods: GET,POST
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false

关于java - Spring webflux : how to publish event from sync call for async processing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com