如何管理Http到Akka Stream中并将消息发送到Kafka?

时间:2018-10-10 08:59:13

标签: scala apache-kafka akka akka-stream akka-http

我从Akka Streams开始,我想构建一个服务器作为Stream来接收Http.IncomingConnection,并将接收到的消息作为plainSink发送到Kafka。

我声明了我的消息来源,如下所示:

val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] =
    Http().bind(interface = "localhost", port = "8080")

然后,我想从HttpRequest的正文中提取消息(字符串),最后将其发送给Kafka。流程如下所示:

val bindingFuture: Future[Http.ServerBinding] = serverSource
     .map(???) //Here, I need to extract the message
     .map(message => new ProducerRecord[String, String](topic, message.result(2 seconds)))
     .runWith(akka.kafka.scaladsl.Producer.plainSink(producerSettings))

但是,我不知道如何提取消息。我想做这样的事情:

val requestHandler: HttpRequest => HttpResponse = {
    case HttpRequest(POST, Uri.Path("/publish"), _, _, _) => {
      HttpResponse(202, entity = "Message sent to Kafka!")
    }
    case r: HttpRequest =>
      r.discardEntityBytes() // important to drain incoming HTTP Entity stream
      HttpResponse(404, entity = "Unknown resource!")
  }

但是,使用connection handleWithSyncHandler requestHandler无法使消息跟上流处理。而且,我也想在/publish URI下获得任何请求,或者在流中以其他方式返回404。

可以这样做吗?

1 个答案:

答案 0 :(得分:1)

改为使用指令

Routing DSL比尝试手动处理HttpRequest更容易使用:

val route : Route = 
  post {
    path("publish") {
      extractRequestEntity { entity =>
        onComplete(entity.toStrict(10.seconds).map(_.data.utf8String){ message =>
          Producer.plainSink(producerSettings)(
            new ProducerRecord[String, String](topic, message.result(2 seconds))
          )
          complete(StatusCodes.OK)
        } 
      }
    }
  }

现在可以将其传递给处理传入的请求:

Http().bindAndHandle(
  route,
  "localhost",
  8080
)