クイックスタート
このガイドでは、簡単な動作例を使用して、KotlinでgRPCを始める方法を説明します。
クイックスタート
前提条件
サンプルコードの入手
サンプルコードはgrpc-kotlinリポジトリの一部です。
リポジトリをzipファイルとしてダウンロードして解凍するか、リポジトリをクローンしてください。
$ git clone --depth 1 https://github.com/grpc/grpc-kotlin
examplesディレクトリに移動します。
$ cd grpc-kotlin/examples
サンプルの実行
examples
ディレクトリから
クライアントとサーバーをコンパイルします。
$ ./gradlew installDist
サーバーを実行します。
$ ./server/build/install/server/bin/hello-world-server Server started, listening on 50051
別のターミナルから、クライアントを実行します。
$ ./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
ディレクトリから次のコマンドを実行します。
クライアントとサーバーをコンパイルします。
$ ./gradlew installDist
サーバーを実行します。
$ ./server/build/install/server/bin/hello-world-server Server started, listening on 50051
別のターミナルから、クライアントを実行します。今回は、コマンドライン引数として名前を追加します。
$ ./client/build/install/client/bin/hello-world-client Alice Received: Hello Alice Received: Hello again Alice