akka流将akka-htpp Web请求调用集成到流中

时间:2018-12-16 20:19:26

标签: scala akka akka-stream akka-http

Akka Streams入门我想执行一个简单的计算。通过调用静态Web api扩展基本的QuickStart https://doc.akka.io/docs/akka/2.5/stream/stream-quickstart.html

val source: Source[Int, NotUsed] = Source(1 to 100)
source.runForeach(println)

已经可以很好地打印数字了。但是,根据https://doc.akka.io/docs/akka/2.5.5/scala/stream/stream-integrations.html

尝试创建Actor来执行HTTP请求时(实际上是否有必要?)。
  import akka.pattern.ask
  implicit val askTimeout = Timeout(5.seconds)
  val words: Source[String, NotUsed] =
    Source(List("hello", "hi"))

  words
    .mapAsync(parallelism = 5)(elem => (ref ? elem).mapTo[String])
    // continue processing of the replies from the actor
    .map(_.toLowerCase)
    .runWith(Sink.ignore)

由于未定义?运算符,因此无法进行编译。据我所知,这个角色只能在演员内部定义。 我还不知道我的自定义角色到底在mapAsync里面什么地方需要被调用。

编辑

https://blog.colinbreck.com/backoff-and-retry-error-handling-for-akka-streams/至少包含示例的一部分。 看来创建演员不是强制性的,即

implicit val system = ActorSystem()
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()


val source = Source(List("232::03::14062::19965186", "232::03::14062::19965189"))
    .map(cellKey => {
      val splits = cellKey.split("::")
      val mcc = splits(0)
      val mnc = splits(1)
      val lac = splits(2)
      val ci = splits(3)
      CellKeySource(cellKey, mcc, mnc, lac, ci)
    })
    .limit(2)
    .mapAsyncUnordered(2)(ck => getResponse(ck.cellKey, ck.mobileCountryCode, ck.mobileNetworkCode, ck.locationArea, ck.cellKey)("<<myToken>>"))

  def getResponse(cellKey: String, mobileCountryCode:String, mobileNetworkCode:String, locationArea:String, cellId:String)(token:String): Future[String] = {
    RestartSource.withBackoff(
      minBackoff = 10.milliseconds,
      maxBackoff = 30.seconds,
      randomFactor = 0.2,
      maxRestarts = 2
    ) { () =>
      val responseFuture: Future[HttpResponse] =
        Http().singleRequest(HttpRequest(uri = s"https://www.googleapis.com/geolocation/v1/geolocate?key=${token}", entity = ByteString(
          // TODO use proper JSON objects
          s"""
             |{
             |  "cellTowers": [
             |    "mobileCountryCode": $mobileCountryCode,
             |    "mobileNetworkCode": $mobileNetworkCode,
             |    "locationAreaCode": $locationArea,
             |    "cellId": $cellId,
             |  ]
             |}
          """.stripMargin)))

      Source.fromFuture(responseFuture)
        .mapAsync(parallelism = 1) {
          case HttpResponse(StatusCodes.OK, _, entity, _) =>
            Unmarshal(entity).to[String]
          case HttpResponse(statusCode, _, _, _) =>
            throw WebRequestException(statusCode.toString() )
        }
    }
      .runWith(Sink.head)
      .recover {
        case _ => throw StreamFailedAfterMaxRetriesException()
      }
  }

val done: Future[Done] = source.runForeach(println)
done.onComplete(_ ⇒ system.terminate())

已经是该问题的(部分)答案,即如何集成Akka-streams + akka-http。但是,它不起作用,即仅引发错误400s并且永不终止。

2 个答案:

答案 0 :(得分:2)

  1. 我认为您已经找到了api的如何调用akka-http客户端

  2. 关于第一个无效的代码段。我认为示例本身发生了一些误解。您希望示例中的代码在复制后即可正常工作。但是该文档的目的只是演示一个示例/概念,如何将一些长期运行的任务从流中委派出去,然后在准备就绪时使用结果。因为对akka actor进行了ask调用,因为对ask方法的调用返回了Future。该文档的作者可能只是省略了actor的定义。您可以尝试以下示例:

    import java.lang.System.exit
    
    import akka.NotUsed
    import akka.actor.{Actor, ActorRef, ActorSystem, Props}
    import akka.pattern.ask
    import akka.stream.ActorMaterializer
    import akka.stream.scaladsl.{Sink, Source}
    import akka.util.Timeout
    
    import scala.concurrent.ExecutionContext.Implicits.global
    import scala.concurrent.duration._
    import scala.language.higherKinds
    
    object App extends scala.App {
    
      implicit val sys: ActorSystem = ActorSystem()
      implicit val mat: ActorMaterializer = ActorMaterializer()
    
      val ref: ActorRef = sys.actorOf(Props[Translator])
    
      implicit val askTimeout: Timeout = Timeout(5.seconds)
      val words: Source[String, NotUsed] = Source(List("hello", "hi"))
    
      words
        .mapAsync(parallelism = 5)(elem => (ref ? elem).mapTo[String])
        .map(_.toLowerCase)
        .runWith(Sink.foreach(println))
        .onComplete(t => {
          println(s"finished: $t")
          exit(1)
        })
    }
    
    class Translator extends Actor {
    
      override def receive: Receive = {
        case msg => sender() ! s"$msg!"
      }
    }
    

答案 1 :(得分:0)

您必须从akka导入询问模式。

导入akka.pattern.ask

编辑:好的,抱歉,我可以看到您已经导入了。您的代码中的ref是什么? ActorRef?