使用 Akka Streams 的一个来源到多个接收器

One Source to Many Sink using Akka Streams

我正在为我已经使用 Rx Scala 的项目之一尝试使用 Akka Streams 的一些东西。我很想看看 Akka Streams 如何适合取代我们现有的 Rx Scala 库。我认为 Akka Streams 无法实现的一件事是拥有一个源和多个汇的可能性。比如说,在这个直接取自 Akka Streams 文档的示例中:

val source = Source(1 to 10)
val sink = Sink.fold[Int, Int](0)(_ + _)

// connect the Source to the Sink, obtaining a RunnableGraph
val runnable: RunnableGraph[Future[Int]] = source.toMat(sink)(Keep.right) // how could I materialize to a Seq of Sinks?

// materialize the flow and get the value of the FoldSink
val sum: Future[Int] = runnable.run()

当使用 Rx 库时,我将 Source (Observable) 和 Sink (Observer) 完全解耦,这让我可以灵活地映射 1 个 Source (Observable) 和 n 个 Sinks (Observer)。我怎样才能通过 Akka Streams 实现这一目标?任何指示都会有所帮助!

这适用于 Graphs,特别是 Broadcast:

Broadcast[T] – (1 input, N outputs) given an input element emits to each output

文档中的一些示例代码:

val in = Source(1 to 10)
val out = Sink.ignore

val bcast = builder.add(Broadcast[Int](2))
val merge = builder.add(Merge[Int](2))

val f1, f2, f3, f4 = Flow[Int].map(_ + 10)

in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out
            bcast ~> f4 ~> merge
ClosedShape