反应堆流链,如果一个成功就中断和继续。

huangapple go评论67阅读模式
英文:

Reactor Flux chaining, break and continue if one succeeds

问题

使用案例是尝试连接到其中一个后端服务器,如果成功连接到其中一个服务器就足够了。有没有办法在不阻塞的情况下使用 Reactor 框架来实现这一点?

例如:

List<String> servers = ...
Flux.fromIterable(servers)

这个 Flux 应该逐个尝试连接并向服务器发送数据,直到成功连接为止。

有没有办法实现这个?

提前致谢。

英文:

Use case is to try to connect to one of the backend servers, if one succeeds then thats sufficient. Is there any way to accomplish this using Reactor framework without blocking ?

For example:

List&lt;String&gt; servers = ...
Flux.fromIterable(servers)

This flux Should try to connect and send data to servers one by one until if there is a success.

Is there any way to accomplish this ?

Thanks in advance.

答案1

得分: 1

以下是翻译好的内容:

正是为了这个目的,存在着 Flux#first 方法。
因此,您需要查询服务器,如果没有结果,则返回空的 flux,这样反应堆将尝试下一个查询,依此类推:

    Flux.first(
        Flux.fromIterable(servers)
        .flatMap(this::queryServer)
    )
英文:

Exactly for this purpose there is Flux#first method.
So you need to query server and return empty flux if there is no result, so reactor will try next query and so on:

    Flux.first(
        Flux.fromIterable(servers)
        .flatMap(this::queryServer)
    )

huangapple
  • 本文由 发表于 2020年9月24日 15:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64041410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定