我正在尝试用我在我的机器上运行的工具创建一个Node.js gRPC连接,这个工具名为Pachyderm。我为该工具生成了一个.proto文件,它将简单地显示该工具的版本。我已经为它生成了一个server.js,但是,它没有成功地通过连接到相应的IP:port来提取工具版本。
如果你能帮我解决这个问题,我会非常感激的。
.proto文件
syntax = "proto3";
import "google/protobuf/empty.proto";
package versionpb;
option go_package = "github.com/pachyderm/pachyderm/src/client/version/versionpb";
message Version {
uint32 major = 1;
uint32 minor = 2;
uint32 micro = 3;
string additional = 4;
}
service API {
rpc GetVersion(google.protobuf.Empty) returns (Version) {}
}server.js
const path = require('path')
const protoLoader = require('@grpc/proto-loader')
const grpc = require('grpc')
const PROTO_PATH = path.resolve(__dirname, './protos/pach.proto')
const pd = protoLoader.loadSync(PROTO_PATH)
const hp = grpc.loadPackageDefinition(pd).pach
function sayHello (call, fn) {
fn(null, { message: 'The tool version is ' + call.request.name })
}
function main () {
const port = 30650
const server = new grpc.Server()
server.bind(`192.161.5.3:${port}`, grpc.ServerCredentials.createInsecure())
server.start()
console.log(`gRPC server started on port: ${port}`)
}
main()发布于 2020-07-08 17:38:53
加载包定义之后,启动服务器后,需要将新服务添加到服务器。
// method name could be API, api or aPI dependes on how this was generated
server.addService(hp.versionpb.service, {api : sayHello});
server.start();https://stackoverflow.com/questions/59673171
复制相似问题