如何在akka类型的actor中发出客户端http请求



似乎http客户端请求API仅适用于经典参与者:

val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))

responseFuture
.onComplete {
case Success(res) => println(res)
case Failure(_)   => sys.error("something wrong")
}

我有一个akka类型的actor,不知道如何向我的API发出外部http请求。

当我在我键入的actor中添加上述内容时,我得到了以下错误:

找不到参数系统的隐式值:akka.actor.ClassicActorSystemProvider

我有什么选择?

类型化的actor系统可以很容易地转换为经典系统,并用于通过akka-http发送http请求。

例如,若有一个类型为akka.actor.typed.ActorSystem的val,您可以通过以下方式将其转换为经典值

val system: akka.actor.typed.ActorSystem[_] = ???
implicit val classic = system.classicSystem

所以现在,任何需要implicit actorSystem: akka.actor.ActorSystem方法参数的方法都可以工作。

在一个类型化的actor中,您可以获得对上下文的引用,然后是对类型化actor系统的引用,该系统然后转换为经典的

val behavior = Behaviors.setup[Int] { ctx =>
implicit val classic = ctx.system.classicSystem
???
}

最新更新