akka http通过nginx将http服务器连接到传出tcp服务器

时间:2017-11-03 15:47:59

标签: akka-stream akka-http

尝试使用akka http like proxy to tcp时出现问题。

启动http服务器,该服务器只负责从客户端获取json请求并通过nginx在Tcp服务器上发送消息,从中获取响应并将响应发送回客户端。所以,只喜欢代理。

问题是服务器在tcp服务器响应之前尝试响应。但是当我删除http和tcp服务器之间的nginx代理时,一切都好。在tcp级别akka http服务器发送RST数据包,然后从nginx获得RST并连接关闭。 Tcp响应nginx但连接已经关闭。用简单的tcp服务器同样的情况。 Akka版本2.4.17,akka http 10.0.10

下面的代码
val connections: Flow[ByteString, ByteString, 
Future[Tcp.OutgoingConnection]] =
Tcp().outgoingConnection(new InetSocketAddress(settings.tcpServerHost, 
settings.tcpServerPort))
def start(): Unit ={
  val requestHandler: HttpRequest => Future[HttpResponse] = {
  case HttpRequest(POST, uri@Uri.Path("/v1/doc"), _, entry, _) =>
   proxyTcp(100, uri, entry)
  case HttpRequest(POST, uri@Uri.Path("/v5/doc"), _, entry, _) =>
   proxyTcp(105, uri, entry)
  case _ => Future(HttpResponse(status = StatusCodes.NotFound))
}
 Http().bind(settings.host, settings.port).runForeach(connection => {
  connection.handleWithAsyncHandler(requestHandler)
}) }
  private def proxyTcp(version: Short, uri: Uri, entry: RequestEntity): 
Future[HttpResponse] = {
  val transform = Transformations(connections, version)
  entry.withoutSizeLimit().dataBytes
  .via(transform.json2tlv)
  .via(transform.connections)
  .runWith(Sink.head)
  .map(result => HttpResponse(status = StatusCodes.OK, entity = 
HttpEntity(ContentTypes.`application/json`, result.get)))
}

0 个答案:

没有答案