クイックスタート
このガイドでは、単純な動作する例を使用して、Kotlin で gRPC を使い始める方法を説明します。
クイックスタート
前提条件
サンプルコードを取得する
サンプルコードは、grpc-kotlin リポジトリの一部です。
リポジトリを zip ファイルとしてダウンロード して解凍するか、リポジトリをクローンしてください。
git clone --depth 1 https://github.com/grpc/grpc-kotlinexamplesディレクトリに移動
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サービスを更新する
このセクションでは、アプリに別のサーバーメソッドを追加します。アプリの gRPC サービスである Greeter は、protocol buffers を使用して定義されています。.proto ファイルでサービスを定義する方法の詳細については、Basics tutorial を参照してください。現時点では、サーバーとクライアントスタブの両方が SayHello() RPC メソッドを持ち、クライアントから HelloRequest パラメータを受け取り、サーバーから HelloReply を返すこと、そしてメソッドが次のように定義されていることを知っておけば十分です。
// 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;
}
helloworld/hello_world.proto を protos/src/main/proto/io/grpc/examples フォルダから開き、同じリクエストおよびレスポンスタイプを持つ新しい 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;
}
ファイルを保存してください!
アプリケーションを更新する
例をビルドすると、ビルドプロセスによって生成された gRPC クライアントおよびサーバークラスを含む HelloWorldProtoGrpcKt.kt が再生成されます。これにより、リクエストおよびレスポンスタイプのポピュレーション、シリアライズ、取得のためのクラスも再生成されます。
ただし、例のアプリケーションの手書き部分で新しいメソッドを実装し、呼び出す必要があります。
サーバーを更新する
helloworld/HelloWorldServer.kt を server/src/main/kotlin/io/grpc/examples フォルダから開きます。新しいメソッドを次のように実装してください。
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}"
}
}
クライアントを更新する
helloworld/HelloWorldClient.kt を client/src/main/kotlin/io/grpc/examples フォルダから開きます。新しいメソッドを次のように呼び出してください。
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
次は何をするか
- gRPCの仕組みをgRPCの紹介とコアコンセプトで学習してください。
- 「基本チュートリアル」を完了してください。
- APIリファレンス(APIリファレンス)を探索してください。