クイックスタート

このガイドでは、簡単な動作例で Node.js の gRPC の入門方法を説明します。

クイックスタート

このガイドでは、簡単な動作例で Node.js の gRPC の入門方法を説明します。

前提条件

  • Node.js バージョン 8.13.0 以上

サンプルをダウンロードする

このクイックスタートを進めるには、サンプルコードのローカルコピーが必要です。GitHub リポジトリからサンプルコードをダウンロードしてください(以下のコマンドはリポジトリ全体をクローンしますが、このクイックスタートや他のチュートリアルには例のみが必要です)。

# Clone the repository to get the example code
git clone -b @grpc/grpc-js@1.9.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc-node
# Navigate to the node example
cd grpc-node/examples
# Install the example's dependencies
npm install
# Navigate to the dynamic codegen "hello, world" Node example:
cd helloworld/dynamic_codegen

gRPCアプリケーションを実行する

examples/helloworld/dynamic_codegen ディレクトリから

  1. サーバーを実行する

    node greeter_server.js
    
  2. 別のターミナルで、クライアントを実行する

    node greeter_client.js
    

おめでとうございます!gRPCを使用したクライアント・サーバーアプリケーションを実行できました。

gRPCサービスを更新する

次に、クライアントが呼び出せるように、サーバーにさらにメソッドを追加してアプリケーションを更新する方法を見てみましょう。gRPC サービスはプロトコルバッファを使用して定義されます。.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;
}

これを更新して、`Greeter` サービスが 2 つのメソッドを持つようにしましょう。`examples/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;
}

ファイルを保存してください!

アプリケーションを更新して実行する

新しいサービス定義はできましたが、例のアプリケーションの手書き部分で新しいメソッドを実装して呼び出す必要があります。

サーバーを更新する

同じディレクトリにある greeter_server.js を開きます。新しいメソッドを次のように実装します。

function sayHello(call, callback) {
  callback(null, {message: 'Hello ' + call.request.name});
}

function sayHelloAgain(call, callback) {
  callback(null, {message: 'Hello again, ' + call.request.name});
}

function main() {
  var server = new grpc.Server();
  server.addService(hello_proto.Greeter.service,
                         {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
    server.start();
  });
}

クライアントを更新する

同じディレクトリにある greeter_client.js を開きます。新しいメソッドを次のように呼び出します。

function main() {
  var client = new hello_proto.Greeter('localhost:50051',
                                       grpc.credentials.createInsecure());
  client.sayHello({name: 'you'}, function(err, response) {
    console.log('Greeting:', response.message);
  });
  client.sayHelloAgain({name: 'you'}, function(err, response) {
    console.log('Greeting:', response.message);
  });
}

実行!

先ほどと同様に、examples/helloworld/dynamic_codegen ディレクトリから

  1. サーバーを実行する

    node greeter_server.js
    
  2. 別のターミナルで、クライアントを実行する

    node greeter_client.js
    

次は何をするか