gRPC之Node Quick Start
2016-09-06 21:02
363 查看
在node环境下我们使用一个小例子作为引导:
1、运行这个服务
2、在另一个终端,运行客户端
恭喜你,你刚才使用gPRC运行了一个客户端-服务端程序.
让我们更新他,这样
别忘了保存这个文件
更新服务端:
在同一个目录下打开greeter_server.js文件,像下面一样扩展新的方法
更新客户端:
在同一个目录下打开greeter_client.js文件,像下面那样调用新的方法:
从examples/node/dynamic_codegen目录中运行,就像我们前面已经死掉了一样。
运行服务端:
运行客户端(在另一个终端下):
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
在开始之前请确认如下:
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 OverviewWork 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
相关文章推荐
- node.js 出现cannot find module 'xxx' 解决办法
- nodejs JS对象和JSON字符串之间的转换
- (译+注解)node.js的C++扩展入门
- nvm 使用攻略
- 222. Count Complete Tree Nodes
- 在Ubuntu 16.04下安装nodejs
- nodejs package.json
- nodejs assert
- leetcode算法——237. Delete Node in a Linked List(基于JAVA)
- node笔记——gulp-imagemin图片压缩
- node.js配置
- NodeJS(node.exe, npm, express, live-server)安装
- node.js 文件上传
- Node.js中Get和Post处理
- LeetCode: Swap Nodes in Pairs
- 遇到问题---Hadoop---java.io.IOException: NameNode is not formatted
- 遇到问题---Hadoop---java.io.IOException: NameNode is not formatted
- Hadoop--NameNode && SecondNameNode
- (九)、ZooKeeper数据模型
- 关于HCA卡的node guid 和port guid