gRPC Node.js快速开始

  • 快速开始
  • 先决条件
  • 下载示例
  • 运行gRPC应用程序
  • 更新gRPC服务
  • 更新并运行应用程序
  • 更新服务端
  • 更新客户端
  • 运行!
  • 下一步是什么
  • 参考资料

本文翻译自gRPC官网的Node.js示例,链接地址为:https://www.grpc.io/docs/languages/node/和Quick start

grpc 容器部署 grpc node_客户端

gRPC是一个高性能、开源的通用RPC框架,其官方地址为:https://www.grpc.io/,如下图所示:

grpc 容器部署 grpc node_服务端_02

目前gRPC项目已经托管到Github上面,其Github仓库地址为:https://github.com/grpc/grpc

快速开始

本指南通过一个简单的示例使您开始在Node中使用gRPC。

先决条件

Node版本4.0.0或更高版本

下载示例

您需要本示例代码的本地副本才能完成此快速入门。 从我们的GitHub存储库下载示例代码(以下命令将克隆整个存储库,但是您仅需要有关此快速入门和其他教程的示例):

# Clone the repository to get the example code
$ git clone -b v1.33.2 https://github.com/grpc/grpc
# Navigate to the dynamic codegen "hello, world" Node example:
$ cd grpc/examples/node/dynamic_codegen
# Install the example's dependencies
$ npm install

运行gRPC应用程序

在examples/node/dynamic_codegen目录中:

  • 1.运行服务端:
$ node greeter_server.js
  • 2.从另一个终端,运行客户端:
$ node greeter_client.js

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

注意 在gRPC服务端和客户端程序运行之前需要在本地使用npm或者yarn安装所需的依赖库。针对gRPC v1.33.2的Node版本,需要安装grpc和@grpc/proto-loader依赖库,对于目前最新的gRPC Node版本,则需要安装@grpc/grpc-js和@grpc/proto-loader依赖库。 假如使用的是gRPC v1.33.2的Node版本的源代码,如果使用的是npm,在grpc/examples/node/dynamic_codegen目录下,执行如下命令:

npm init -y
npm install grpc @grpc/proto-loader

如果使用的是yarn,在grpc/examples/node/dynamic_codegen目录下,执行如下命令:

yarn init -y
yarn add grpc @grpc/proto-loader

下面是我在Windows10下使用grpc github仓库的最新grpc代码node示例程序运行的结果,使用之前需要安装Node.js开发环境和npm、cnpm、yarn等,然后在grpc源代码的grpc\examples\node\dynamic_codegen目录下使用yarn安装@grpc/grpc-js和@grpc/proto-loader依赖库,命令如下:

yarn init -y
yarn add @grpc/grpc-js '@grpc/proto-loader

然后分别运行服务端和客户端,如下图所示:

grpc 容器部署 grpc node_服务端_03

grpc 容器部署 grpc node_应用程序_04

更新gRPC服务

现在,让我们看一下如何使用服务器上的其他方法更新应用程序,以供客户端调用。 我们的gRPC服务是使用protocol buffers的; 您可以在基础知识教程中找到更多有关如何在.proto文件中定义服务的信息。 现在,您只需要知道服务器和客户端“stub”都有一个SayHelloRPC方法,该方法从客户端获取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服务具有两种方法。 编辑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.bind('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/node/dynamic_codegen目录:

  • 1.运行服务器:
$ node greeter_server.js
  • 2.从另外一个终端,运行客户端:
$ node greeter_client.js

下面是我在自己购买的阿里云服务器上运行服务端和客户端的演示结果:

grpc 容器部署 grpc node_grpc 容器部署_05

下一步是什么

  • 在gRPC简介和核心概念中了解gRPC的工作原理。
  • 完成基础教程。
  • 探索API参考。
  • 我们为Node提供了多个grpc实现。 有关每个程序包的优缺点,请参阅此程序包功能比较。

参考资料

  • gRPC官网
  • gRPC简介
  • gRPC Github仓库
  • gRPC支持的语言