In the node environment we use a small example as a guide:
Before you begin, confirm the following:
1, Node: version above 0.12
Download this example
To get started with this example, you need to make a backup of this example code locally. Download the code for this example from our GitHub code repository (the following command clones the entire codebase, but you only need this example as a quick guide and other tutorials)
-B v1. 0.0 https://github.com/grpc/grpc"Hello,world" Node example:$ CD grpc/examples/node/dynamic_codegen$ # Install The example's dependencies Install
Run a GRPC program
From the Examples/node/dynamic_codegen directory
1. Run the service
$ node Greeter_server.js
2. On the other terminal, run the client
$ node Greeter_client.js
Congratulations, you just ran a client-server program using GPRC.
Update a GPRC service
Now look at how to update a program with additional methods on the server via a client call, our GPRC service is defined using protocol buffers. In the Grpc basics:node. proto file you will find a lot about how to define a service. Now all you need to know is that the server and client "stub" have a SayHello method in RPC. This method has one in the clientHelloRequest参数,在服务端中会返回一个HelloResponse,这个个方法的定义很像下面的定义:
// The greeting service definition. service Greeter { sends a greeting RPC SayHello (hellorequest) returns (Helloreply) {}} Span style= "color: #008000;" >// message hellorequest {string name = 1;} // message helloreply {string message = 1;}
Let's update him, soGreeter服务就有了两个方法,编辑examples/protos/helloworld.proto并且使用SayHelloAgain
方法来更新他,带有同样的请求和响应类型。
//The greeting service definition.service Greeter {//sends a greetingRPC SayHello (hellorequest) returns (Helloreply) {}//sends another greetingRPC Sayhelloagain (hellorequest) returns (Helloreply) {}}//The request message containing the user ' s name.message hellorequest {string name= 1;}//The response message containing the greetingsmessage helloreply {string message= 1;}
Don't forget to save this file.
Update and run this program
Now we are going to make a new service, but we need to expand and call the new method in the human part of our example program.
Update Service side:
Open the Greeter_server.js file in the same directory, extending the new method as follows
functionSayHello (call, callback) {callback (NULL, {message: ' Hello ' +call.request.name});}functionSayhelloagain (call, callback) {callback (NULL, {message: ' Hello again, ' +call.request.name});}functionMain () {varServer =NewGrpc. Server (); Server.addprotoservice (Hello_proto. Greeter.service, {Sayhello:sayhello, sayhelloagain:sayhelloagain}); Server.bind (' 0.0.0.0:50051 ', Grpc. Servercredentials.createinsecure ()); Server.start ();} ...
Update client:
Open the Greeter_client.js file in the same directory and invoke the new method as follows:
function Main () { varnew Hello_proto. Greeter (' localhost:50051 ', grpc.credentials.createInsecure ()); function (Err, response) { console.log (' greeting: ', response.message); }); function (Err, response) { console.log (' greeting: ', response.message); });
Run from the Examples/node/dynamic_codegen directory, just as we were dead before.
To run the service side:
$ node Greeter_server.js
Run the client (under another terminal):
$ node Greeter_client.js
What ' s Next
- Read A full explanation of this example and what GRPC works in our overview
- Work through a to detailed tutorial in Grpc basics:node
- Explore the Grpc Node core API in its reference documentation
Original: http://www.grpc.io/docs/quickstart/node.html
GRPC node Quick Start