好吧...看来我将不得不为此添加一个示例。您需要了解的一件事是 BackPressure 由 GraphStages 中的 AsyncBoundries 处理。它实际上与其他地方存在的组件无关。另外......它不依赖于动脉,它只不过是新的远程传输。
这是一个可能是最简单的跨 jvm 流的示例,
首次申请,
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.actor.Actor.Receive
import com.typesafe.config.{Config, ConfigFactory}
class MyActor extends Actor with ActorLogging {
override def receive: Receive = {
case msg @ _ => {
log.info(msg.toString)
sender() ! msg
}
}
}
object MyApplication extends App {
val config = ConfigFactory.parseString(
"""
|akka{
| actor {
| provider = remote
| }
| remote {
| enabled-transports = ["akka.remote.netty.tcp"]
| untrusted-mode = off
| netty.tcp {
| hostname="127.0.0.1"
| port=18000
| }
| }
|}
""".stripMargin
)
val actorSystem = ActorSystem("my-actor-system", config)
var myActor = actorSystem.actorOf(Props(classOf[MyActor]), "my-actor")
}
第二个应用程序......实际上“运行”了在第一个应用程序中使用演员的流。
import akka.actor.{ActorPath, ActorSystem}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Keep, Sink, Source}
import akka.pattern.ask
import com.typesafe.config.ConfigFactory
import scala.language.postfixOps
import scala.concurrent.duration._
object YourApplication extends App {
val config = ConfigFactory.parseString(
"""
|akka{
| actor {
| provider = remote
| }
| remote {
| enabled-transports = ["akka.remote.netty.tcp"]
| untrusted-mode = off
| netty.tcp {
| hostname="127.0.0.1"
| port=19000
| }
| }
|}
""".stripMargin
)
val actorSystem = ActorSystem("your-actor-system", config)
import actorSystem.dispatcher
val logger = actorSystem.log
implicit val implicitActorSystem = actorSystem
implicit val actorMaterializer = ActorMaterializer()
val myActorPath = ActorPath.fromString("akka.tcp://my-actor-system@127.0.0.1:18000/user/my-actor")
val myActorSelection = actorSystem.actorSelection(myActorPath)
val source = Source(1 to 10)
// here this "mapAsync" wraps the given T => Future[T] function in a GraphStage
val myRemoteComponent = Flow[Int].mapAsync(2)(i => {
myActorSelection.resolveOne(1 seconds).flatMap(myActorRef =>
(myActorRef.ask(i)(1 seconds)).map(x => x.asInstanceOf[Int])
)
})
val sink = Sink.foreach[Int](i => logger.info(i.toString))
val stream = source.via(myRemoteComponent).toMat(sink)(Keep.right)
val streamRun = stream.run()
}