2017-02-28 2 views
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) { .... } 

- (UPDATE)これは、以下、ステファノBonettiからの助けを借りた後、働いていたものです。

ompletionStage<ServerBinding> binding = 
      serverSource.to(Sink.foreach(connection -> { 
        connection.handleWith(MyRoute(connection.remoteAddress()) 
        .flow(system, mat), mat); 
       } 
      )).run(mat); 

答えて

1

あなたはFlow[HttpRequest, HttpResponse, NotUsed]にあなたRouteを変換しhandleWith関数に渡すことができるはずです。

connection.handleWith(myRoute(connection.remoteAddress()).flow(actorSystem, mat), mat); 
+0

私はそれを試したが、私は得る: エラー:(131、30)は、Java:クラスakka.stream.javadsl.Sourceにおける方法runWith が所与のタイプに適用することができないと、 必須:akka.stream.Graph 、M>、akka.stream.Materializer が見つかりました:akka.stream.javadsl.Sink 理由:型変数を推測できませんM (実際の引数リストと長めの引数リストの長さが異なります) –

+1

私はそれをうまく動作させてくれました。あなたの提案は助けになりましたが、 。元の質問をコードで更新します。 –

関連する問題