0
我有以下基本代码来启动一个akk-http服务,并且我想将客户端IP传递到我的路由处理程序中。Akka-http,获取Java中的客户端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);
...
我发现这个职位:
Obtaining the client IP in Akka-http
但是它使用的底层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) { .... }
- (更新)这是什么工作,从斯特凡诺 - 博内蒂的帮助后,下方。
ompletionStage<ServerBinding> binding =
serverSource.to(Sink.foreach(connection -> {
connection.handleWith(MyRoute(connection.remoteAddress())
.flow(system, mat), mat);
}
)).run(mat);
我试过,但我得到: 错误:(131,30)的java:方法runWith类akka.stream.javadsl.Source不能被应用到给定的类型; 必需:akka.stream.Graph ,M>,akka.stream.Materializer 找到的:akka.stream.javadsl.Sink > 原因:无法推断类型变量M (实际和正式参数列表的长度不同) –
我确实已经完成了它的工作,您的建议有所帮助,但我不得不做出微小的更改。我将用代码更新原始问题。 –