快速入門

本指南透過一個簡單的示例帶您入門 Dart 版 gRPC。

快速入門

本指南透過一個簡單的示例帶您入門 Dart 版 gRPC。

先決條件

  • Dart 2.12 或更高版本(透過 Dart 或 Flutter SDK 獲取)

    有關安裝說明,請參閱 安裝 Dart安裝 Flutter

  • Protocol buffer 編譯器protoc版本 3

    有關安裝說明,請參閱 Protocol Buffer 編譯器安裝

  • Dart 外掛(用於協議編譯器)

    1. 使用以下命令安裝適用於 Dart 的協議編譯器外掛 (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

接下來