您的位置:首页 > Web前端 > Node.js

gRPC之Node Quick Start

2016-09-06 21:02 363 查看
在node环境下我们使用一个小例子作为引导:

在开始之前请确认如下:

1、node:版本在0.12以上

下载这个例子

为了更好地开始这个例子,你需要在本地对这个例子代码做一个备份。下载这个例子的代码从我们的GitHub代码仓库(下面的命令克隆整个代码库,但是你却只需要这个例子作为快速的引导以及其他的教程)

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


运行一个gRPC程序

从examples/node/dynamic_codegen目录中

1、运行这个服务

$ node greeter_server.js


2、在另一个终端,运行客户端

$ node greeter_client.js


恭喜你,你刚才使用gPRC运行了一个客户端-服务端程序.

更新一个gPRC服务

现在来看一下怎样通过客户端调用来更新服务器上带有额外方法的一个程序,我们的gPRC服务使用protocol buffers来定义。在gRPC Basics: Node的.proto文件中你会发现许多关于怎么样去定义一个服务。现在你所需要知道的是服务端与客户端“stub”都有一个SayHello方法在RPC中。这个方法在客户端中具有一个
HelloRequest参数,在服务端中会返回一个
HelloResponse,这个个方法的定义很像下面的定义:


[code]


// 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.addProtoService(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目录中运行,就像我们前面已经死掉了一样。

运行服务端:

$ node greeter_server.js


运行客户端(在另一个终端下):

$ node greeter_client.js


What’s next

Read a full explanation of this example and how gRPC works in our Overview

Work through a more detailed tutorial in gRPC Basics: Node

Explore the gRPC Node core API in its reference documentation

原文:http://www.grpc.io/docs/quickstart/node.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: