Node 快速使用 gRPC

更新时间: 2019-07-05 12:52

This guide gets you started with gRPC in Node with a simple working example.

Before you begin

Prerequisites

  • node: version 4.0.0 or higher

Download the example

You’ll need a local copy of the example code to work through this quickstart. Download the example code from our GitHub repository (the following command clones the entire repository, but you just need the examples for this quickstart and other tutorials):

$ # Clone the repository to get the example code
$ git clone -b v1.22.0 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

Run a gRPC application

From the examples/node/dynamic_codegen directory:

  1. Run the server

    $ node greeter_server.js
  2. In another terminal, run the client

    $ node greeter_client.js

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in gRPC Basics: Node. For now all you need to know is that both the server and the client “stub” have a SayHelloRPC method that takes a HelloRequest parameter from the client and returns a HelloReply from the server, and that this method is defined like this:

// 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;
}

Let’s update this so that the Greeter service has two methods. Edit examples/protos/helloworld.proto and update it with a new SayHelloAgain method, with the same request and response types:

// 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;
}

(Don’t forget to save the file!)

Update and run the application

We now have a new service definition, but we still need to implement and call the new method in the human-written parts of our example application.

Update the server

In the same directory, open greeter_server.js. Implement the new method like this:

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.addProtoService(hello_proto.Greeter.service,
                         {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  server.start();
}
...

Update the client

In the same directory, open greeter_client.js. Call the new method like this:

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);
  });
}

Run!

Just like we did before, from the examples/node/dynamic_codegen directory:

  1. Run the server

    $ node greeter_server.js
  2. In another terminal, run the client

    $ node greeter_client.js

What’s next

创新者的解答

创新者的解答

【美】克莱顿•克里斯坦森、【加】迈克尔·雷纳 / 中信出版社 / 2013-10-10 / 49.00

《创新者的解答》讲述为了追求创新成长机会,美国电信巨子AT&T在短短10年间,总共耗费了500亿美元。企业为了保持成功记录,会面对成长的压力以达成持续获利的目标。但是如果追求成长的方向出现偏误,后果往往比没有成长更糟。因此,如何创新,并选对正确方向,是每个企业最大的难题。 因此,如何创新,并导向何种方向,便在于创新结果的可预测性─而此可预测性则来自于正确的理论依据。在《创新者的解答》中,两位......一起来看看 《创新者的解答》 这本书的介绍吧!

HTML 压缩/解压工具

HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具

CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析

JSON 在线解析

在线 JSON 格式化工具