クイックスタート
このガイドでは、簡単な動作例を使用して、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
ディレクトリから
サーバーを実行
$ node greeter_server.js
別のターミナルから、クライアントを実行
$ node greeter_client.js
おめでとうございます!gRPCを使用したクライアントサーバーアプリケーションを実行しました。
gRPCサービスの更新
次に、クライアントが呼び出すことができるサーバーに追加メソッドを更新する方法を見てみましょう。gRPCサービスはプロトコルバッファを使用して定義されています。サービスを`.proto`ファイルで定義する方法の詳細については、基本チュートリアルを参照してください。今のところ、サーバーとクライアントの「スタブ」の両方に、クライアントから`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;
}
`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`ディレクトリから
サーバーを実行
$ node greeter_server.js
別のターミナルから、クライアントを実行
$ node greeter_client.js
次のステップ
- gRPCの動作方法については、gRPCの概要と基本概念をご覧ください。
- 基本チュートリアルを試してみてください。
- APIリファレンスを参照してください。
- Node.jsには複数のgRPC実装があります。各パッケージの長所と短所については、このパッケージ機能比較をご覧ください。