Github地址(Nodejs 版本):GitHub - justajwolf/GrpcDemo
一、编写pb文件(.proto),例:chat.proto
注:1.protocol buffers 维基百科:https://zh.wikipedia.org/wiki/Protocol_Buffers
2.protocol buffers 语法详见:https://developers.google.com/protocol-buffers
syntax = "proto3"; // 使用proto3语法,因为从3.0.0 Beta2版,开始支持JavaScript
package grpcTest; // 定义包名
// 定义服务名
service chat {
// 定义远程调用方法
rpc getMessage(HelloRequest) returns (HelloResponse) {}
rpc getAge(HelloRequest) returns (HelloResponse) {}
}
// 定义消息类型
message HelloRequest{
int32 id = 1;
string name = 2;
int32 age = 3;
string year = 4;
}
// 定义消息类型
message HelloResponse{
string reply = 1;
}
二、demo代码:index.js
const path = require('path');
const grpc = require(