识别Akka HttpRequest和HttpResponse?

时间:2018-05-27 21:32:26

标签: scala http akka akka-http

在使用 Akka HttpRequest并将请求传递给演员时,我无法识别响应。 演员将处理将收到的每条消息,但它不知道用于获得此响应的请求。有没有办法识别每个请求以匹配响应?

注意:我没有服务器重新发送请求正文的任何​​部分。

提前致谢

MySelf.scala

import akka.actor.{ Actor, ActorLogging }
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.{ ActorMaterializer, ActorMaterializerSettings }
import akka.util.ByteString

class Myself extends Actor with ActorLogging {

import akka.pattern.pipe
import context.dispatcher

final implicit val materializer: ActorMaterializer = 
       ActorMaterializer(ActorMaterializerSettings(context.system))

def receive = {
  case HttpResponse(StatusCodes.OK, headers, entity, _) =>
    entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
      log.info("Got response, body: " + body.utf8String)
  }
  case resp @ HttpResponse(code, _, _, _) =>
    log.info("Request failed, response code: " + code)
    resp.discardEntityBytes()
  }

}

Main.scala

import akka.actor.{ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer

object HttpServerMain extends App {

import akka.pattern.pipe

//  import system.dispatcher
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher

val http = Http(system)

val myActor = system.actorOf(Props[MySelf])

http.singleRequest(HttpRequest(uri = "http://akka.io"))
    .pipeTo(myActor)

http.singleRequest(HttpRequest(uri = "http://akka.io/another-request"))
    .pipeTo(myActor)
Thread.sleep(2000)
system.terminate()

2 个答案:

答案 0 :(得分:1)

您只需使用map转换Future并在将其传输到myActor之前添加某种ID(通常称为相关ID用于此类目的):

http.singleRequest(HttpRequest(uri = "http://akka.io"))
    .map(x => (1, x)).pipeTo(myActor)

您需要更改模式匹配块以进行调整:

case (id, HttpResponse(StatusCodes.OK, headers, entity, _)) =>

如果由于某种原因您不能/不想更改模式匹配块,则可以使用相同的方法,而是在已完成的请求中添加唯一的HTTP标头(使用copy),例如这个(如果编译则不检查):

// make a unique header name that you are sure will not be
// received from http response:
val correlationHeader: HttpHeader = ... // mycustomheader

// Basically hack the response to add your header:
http.singleRequest(HttpRequest(uri = "http://akka.io"))
    .map(x => x.copy(headers = correlationHeader +: headers)).pipeTo(myActor)

// Now you can check your header to see which response that was:
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
  headers.find(_.is("mycustomheader")).map(_.value).getOrElse("NA")

与以前的选项相比,这更像是一种黑客攻击,因为您正在修改响应。

答案 1 :(得分:1)

我认为你不能直接使用pipeTo来执行此操作,因为它基本上只是adds andThen来调用Future。一个选项是map,然后将(request, response)元组发送给actor:

val request = HttpRequest(uri = "http://akka.io")
http.singleRequest(request).map {
  response => myActor ! (request, response)
}

class Myself extends Actor with ActorLogging {
  ...
  def receive = {
    case (request, HttpResponse(StatusCodes.OK, headers, entity, _)) =>
      ...

    case (request, resp @ HttpResponse(code, _, _, _)) =>
      log.info(request.toString)
      ...
  }
}