快速入门

本指南通过一个简单的示例,让您开始使用 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 服务

在本节中,您将使用一个额外的服务器方法更新应用程序。 应用程序的 gRPC 服务名为 Greeter,使用 协议缓冲区 定义。要了解有关如何在 .proto 文件中定义服务的更多信息,请参阅 基础教程。现在,您只需要知道服务器和客户端存根都有一个 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;
}

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
    

下一步

上次修改时间:2024 年 11 月 25 日:feat: 将 $ shell 行指示符移动到 scss (#1354) (ab8b3af)