nodejs use gRPC

gRPC

Code is placed on GitHub

https://github.com/ShisiJu/grpc-node

protobuf

gRPC It will have its own written format; that is, protocol buffers

Specific format definition can Tell me what network (over the wall needed)

https://developers.google.com/protocol-buffers/docs/overview

We need to be in accordance with the format, definitions serviceandmessage

The following example, you can look

syntax = "proto3";

package helloworld;

// 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;
    int32 age = 2;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
    // 这里我没有定义 age,那么response中也不会有
}

Name of the service, and a method call parameters and parameter fields are specified by the above codes;

node

By definition proto, then we can use to try to be an example gRPC

System services is where server
the system is to be calledclient

Whether server or client, both of which need to protofile;
Service and message need to be defined;

The following code is the server and the client at the beginning of a total of

var PROTO_PATH = __dirname + '/../protos/helloworld.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH, {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

server

We first define a server-side and in the same way as proto sayHello,
and create a gRPC the server

function sayHello(call, callback) {
    // call 是 gRPC 给封装好的对象
    // callback 是client要执行的回调
    // request对象中,只有 HelloRequest 中定义的字段
    console.log(call.request)
    // callback 第一个参数,如果报错可以传入 error
    let err = null
    // callback 第二个参数,返回的字段也和 HelloReply 相同
    callback(err, {
        message: 'Hello ' + call.request.name
    });
}

Then we should create a server gRPC

  var server = new grpc.Server();
  server.addService(hello_proto.Greeter.service, {
      sayHello: sayHello
  });
  // 这里绑定的地址要和client请求的一致
  server.bind('0.0.0.0:50055', grpc.ServerCredentials.createInsecure());
  server.start();

Avoid unnecessary space, server and client after all the code on github

https://github.com/ShisiJu/grpc-node

client

After the server defined the method, we have to request that the server

  // 指定地址和端口号
  var client = new hello_proto.Greeter('localhost:50055',
      grpc.credentials.createInsecure());
  var user = 'world';

  // client.sayHello(call,callback)
  client.sayHello({
      name: user,
      age: 'no'
  }, function(err, response) {
      // callback的 err 是server 来返回的 如果无 null 说明无错误
      if (err === null) {
          // 说明server端没有出现错误 (两段式请求,只能通过 err 来判断)
      }
      // server端给返回的数据 response 和 HelloReply 定义的一样
      console.log('Greeting:', response);
  });

In which the parameters passed to note:

Incoming client

{ name: "world", age: 'no'  }

age It should be a number, not a string

server to accept the request will get

{name: "world", age: 0}

Remember: there is no incoming parameters, or the value passed wrong;
will become the default value;

Published 92 original articles · won praise 18 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_34120430/article/details/104147467