Akka-http,在 Java 中获取客户端 IP
Akka-http, getting client IP in Java
我有以下启动 akk-http 服务的基本代码,我想将客户端 IP 传递到我的路由处理程序中。
...
final Flow<HttpRequest, HttpResponse, NotUsed> myFlow= myRoute().flow(actorSystem, actorMaterializer);
final CompletionStage<ServerBinding> binding = akkaHttp.bindAndHandle(myFlow,
ConnectHttp.toHost(configurationInstance.getBindAddress(), configurationInstance.getBindPort()), actorMaterializer);
...
我找到这个 post:
然而它使用低电平API。
到目前为止我得到了这个,但我不想更改为低级别API。有没有办法让它与高级别 API 一起工作?我在 myRoute() 上遇到编译错误,我不确定如何为这种方法创建处理程序。
...
Http.get(system)
.bind(ConnectHttp.toHost(configurationInstance.getBindAddress(),
configurationInstance.getBindPort()), mat);
CompletionStage<ServerBinding> binding =
rverSource.runWith(Sink.foreach(connection -> {
connection.handleWithAsyncHandler(
myRoute(connection.remoteAddress()), mat); // ERROR
})).run(mat);
...
public Route finalRoute(InetSocketAddress client) { .... }
--
(更新)在下面的 Stefano Bonetti 的帮助下,这是有效的方法。
ompletionStage<ServerBinding> binding =
serverSource.to(Sink.foreach(connection -> {
connection.handleWith(MyRoute(connection.remoteAddress())
.flow(system, mat), mat);
}
)).run(mat);
您应该能够将 Route
转换为 Flow[HttpRequest, HttpResponse, NotUsed]
并将其传递给 handleWith
函数。
connection.handleWith(myRoute(connection.remoteAddress()).flow(actorSystem, mat), mat);
我有以下启动 akk-http 服务的基本代码,我想将客户端 IP 传递到我的路由处理程序中。
...
final Flow<HttpRequest, HttpResponse, NotUsed> myFlow= myRoute().flow(actorSystem, actorMaterializer);
final CompletionStage<ServerBinding> binding = akkaHttp.bindAndHandle(myFlow,
ConnectHttp.toHost(configurationInstance.getBindAddress(), configurationInstance.getBindPort()), actorMaterializer);
...
我找到这个 post:
然而它使用低电平API。
到目前为止我得到了这个,但我不想更改为低级别API。有没有办法让它与高级别 API 一起工作?我在 myRoute() 上遇到编译错误,我不确定如何为这种方法创建处理程序。
...
Http.get(system)
.bind(ConnectHttp.toHost(configurationInstance.getBindAddress(),
configurationInstance.getBindPort()), mat);
CompletionStage<ServerBinding> binding =
rverSource.runWith(Sink.foreach(connection -> {
connection.handleWithAsyncHandler(
myRoute(connection.remoteAddress()), mat); // ERROR
})).run(mat);
...
public Route finalRoute(InetSocketAddress client) { .... }
-- (更新)在下面的 Stefano Bonetti 的帮助下,这是有效的方法。
ompletionStage<ServerBinding> binding =
serverSource.to(Sink.foreach(connection -> {
connection.handleWith(MyRoute(connection.remoteAddress())
.flow(system, mat), mat);
}
)).run(mat);
您应该能够将 Route
转换为 Flow[HttpRequest, HttpResponse, NotUsed]
并将其传递给 handleWith
函数。
connection.handleWith(myRoute(connection.remoteAddress()).flow(actorSystem, mat), mat);