クイックスタート

このガイドでは、簡単な動作例を使用して、KotlinでgRPCを始める方法を説明します。

クイックスタート

このガイドでは、簡単な動作例を使用して、KotlinでgRPCを始める方法を説明します。

前提条件

  • Kotlin バージョン1.3以降
  • JDK バージョン7以降

サンプルコードの入手

サンプルコードはgrpc-kotlinリポジトリの一部です。

  1. リポジトリをzipファイルとしてダウンロードして解凍するか、リポジトリをクローンしてください。

    $ git clone --depth 1 https://github.com/grpc/grpc-kotlin
    
  2. examplesディレクトリに移動します。

    $ cd grpc-kotlin/examples
    

サンプルの実行

examplesディレクトリから

  1. クライアントとサーバーをコンパイルします。

    $ ./gradlew installDist
    
  2. サーバーを実行します。

    $ ./server/build/install/server/bin/hello-world-server
    Server started, listening on 50051
    
  3. 別のターミナルから、クライアントを実行します。

    $ ./client/build/install/client/bin/hello-world-client
    Received: Hello world
    

おめでとうございます!gRPCを使ったクライアントサーバーアプリを実行しました。

gRPCサービスの更新

このセクションでは、追加のサーバーメソッドを使用してアプリを更新します。Greeterという名前のアプリのgRPCサービスは、Protocol Bufferを使用して定義されています。.protoファイルでサービスを定義する方法の詳細については、基本チュートリアルを参照してください。ここでは、サーバーとクライアントスタブの両方に、クライアントからHelloRequestパラメーターを受け取り、サーバーからHelloReplyを返すSayHello() RPCメソッドがあり、そのメソッドが次のように定義されていることを理解するだけで十分です。

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

protos/src/main/proto/io/grpc/examplesフォルダからhelloworld/hello_world.protoを開き、同じリクエストとレスポンスタイプを持つ新しいSayHelloAgain()メソッドを追加します。

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

ファイルを保存することを忘れないでください!

アプリの更新

例をビルドすると、ビルドプロセスによってHelloWorldProtoGrpcKt.ktが再生成されます。これには、生成されたgRPCクライアントとサーバークラスが含まれています。これにより、リクエストとレスポンスタイプの入力、シリアル化、取得のためのクラスも再生成されます。

ただし、例アプリの手書き部分で新しいメソッドを実装して呼び出す必要があります。

サーバーの更新

server/src/main/kotlin/io/grpc/examplesフォルダからhelloworld/HelloWorldServer.ktを開きます。新しいメソッドを次のように実装します。

private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
  override suspend fun sayHello(request: HelloRequest) = helloReply {
    message = "Hello ${request.name}"
  }

  override suspend fun sayHelloAgain(request: HelloRequest) = helloReply {
    message = "Hello again ${request.name}"
  }
}

クライアントの更新

client/src/main/kotlin/io/grpc/examplesフォルダからhelloworld/HelloWorldClient.ktを開きます。新しいメソッドを次のように呼び出します。

class HelloWorldClient(
    private val channel: ManagedChannel
) : Closeable {
  private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)

  suspend fun greet(name: String) {
    val request = helloRequest { this.name = name }
    val response = stub.sayHello(request)
    println("Received: ${response.message}")
    val againResponse = stub.sayHelloAgain(request)
    println("Received: ${againResponse.message}")
  }

  override fun close() {
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
  }
}

更新されたアプリの実行

以前と同様にクライアントとサーバーを実行します。examplesディレクトリから次のコマンドを実行します。

  1. クライアントとサーバーをコンパイルします。

    $ ./gradlew installDist
    
  2. サーバーを実行します。

    $ ./server/build/install/server/bin/hello-world-server
    Server started, listening on 50051
    
  3. 別のターミナルから、クライアントを実行します。今回は、コマンドライン引数として名前を追加します。

    $ ./client/build/install/client/bin/hello-world-client Alice
    Received: Hello Alice
    Received: Hello again Alice
    

次のステップ