快速入门

本指南通过一个简单的运行示例,帮助您开始使用 Dart 中的 gRPC。

快速入门

本指南通过一个简单的运行示例,帮助您开始使用 Dart 中的 gRPC。

先决条件

  • Dart 版本 2.12 或更高,通过 Dart 或 Flutter SDKs

    有关安装说明,请参阅安装 Dart安装 Flutter

  • Protocol buffer 编译器protoc版本 3

    有关安装说明,请参阅Protocol Buffer 编译器安装

  • 用于 protocol 编译器的 Dart 插件

    1. 使用以下命令安装 Dart 的 protocol 编译器插件(protoc-gen-dart

      dart pub global activate protoc_plugin
      
    2. 更新您的 PATH,以便 protoc 编译器能够找到该插件

      export PATH="$PATH:$HOME/.pub-cache/bin"
      

获取示例代码

示例代码是 grpc-dart 仓库的一部分。

  1. 将仓库下载为 zip 文件并解压,或克隆该仓库

    git clone https://github.com/grpc/grpc-dart
    
  2. 切换到快速入门示例目录

    cd grpc-dart/example/helloworld
    

运行示例

example/helloworld 目录

  1. 下载软件包依赖项

    dart pub get
    
  2. 运行服务器

    dart bin/server.dart
    
  3. 在另一个终端中,运行客户端

    dart bin/client.dart
    Greeter client received: Hello, world!
    

恭喜!您刚刚运行了一个使用 gRPC 的客户端-服务器应用程序。

更新应用程序

在本节中,您将更新应用程序以使用一个额外的服务器方法。gRPC 服务是使用 protocol buffers 定义的。要了解有关如何在 .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;
}

更新 gRPC 服务

打开 protos/helloworld.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;
}

记得保存文件!

重新生成 gRPC 代码

在使用新的服务方法之前,您需要重新编译更新的 proto 文件。在 example/helloworld 目录中,运行以下命令

protoc --dart_out=grpc:lib/src/generated -Iprotos protos/helloworld.proto

您将在 lib/src/generated 目录中找到重新生成的请求和响应类,以及客户端和服务器类。

现在,分别在服务器和客户端代码中实现并调用新的 RPC。

更新服务器

打开 bin/server.dart,并将以下 sayHelloAgain() 方法添加到 GreeterService 类中

class GreeterService extends GreeterServiceBase {
  @override
  Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
    return HelloReply()..message = 'Hello, ${request.name}!';
  }

  @override
  Future<HelloReply> sayHelloAgain(ServiceCall call, HelloRequest request) async {
    return HelloReply()..message = 'Hello again, ${request.name}!';
  }
}

更新客户端

bin/client.dart 中添加对 sayHelloAgain() 的调用,如下所示

Future<void> main(List<String> args) async {
  final channel = ClientChannel(
    'localhost',
    port: 50051,
    options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
  );
  final stub = GreeterClient(channel);

  final name = args.isNotEmpty ? args[0] : 'world';

  try {
    var response = await stub.sayHello(HelloRequest()..name = name);
    print('Greeter client received: ${response.message}');
    response = await stub.sayHelloAgain(HelloRequest()..name = name);
    print('Greeter client received: ${response.message}');
  } catch (e) {
    print('Caught error: $e');
  }
  await channel.shutdown();
}

运行更新后的应用程序

像之前一样运行客户端和服务器。在 example/helloworld 目录中执行以下命令

  1. 运行服务器

    dart bin/server.dart
    
  2. 从另一个终端运行客户端。这次,添加一个名称作为命令行参数

    dart bin/client.dart Alice
    

    您将看到以下输出

    Greeter client received: Hello, Alice!
    Greeter client received: Hello again, Alice!
    

贡献

如果您遇到 Dart gRPC 的问题或有功能请求,请在 grpc-dart 仓库中创建一个 issue

接下来