クイックスタート

このガイドでは、簡単な実行例で Dart における gRPC の利用を開始する方法を説明します。

クイックスタート

このガイドでは、簡単な実行例で Dart における gRPC の利用を開始する方法を説明します。

前提条件

  • Dart バージョン 2.12 以降、Dart または Flutter SDK 経由

    インストール手順については、Dart のインストール または Flutter のインストール を参照してください。

  • Protocol buffer コンパイラprotocバージョン 3

    インストール手順については、Protocol Buffer Compiler Installation を参照してください。

  • Protocol compiler 用の Dart プラグイン

    1. 次のコマンドを使用して、Protocol Compiler 用の Dart プラグイン (protoc-gen-dart) をインストールします。

      dart pub global activate protoc_plugin
      
    2. protoc コンパイラがプラグインを見つけられるように、PATH を更新してください。

      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 ファイルでサービスを定義する方法の詳細については、Basics tutorial を参照してください。現時点では、サーバーとクライアントスタブの両方に、クライアントから 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;
}

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 を開き、GreeterService クラスに次の sayHelloAgain() メソッドを追加します。

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 を作成してください。

次は何をするか