【问题标题】:Proto files using google.protobuf imports - causing issues in NodeJS使用 google.protobuf 导入的 Proto 文件 - 导致 NodeJS 出现问题
【发布时间】:2018-04-16 20:50:01
【问题描述】:

我在 proto 文件中有 google.profobuf.* 导入(由 Go 编写的 GRPC 服务器使用)。当使用相同的 proto 文件在 NodeJS 中实现 GRPC 客户端时 - 我遇到了问题。

详情:

GRPC 服务器使用的 proto 文件(用 go 编写):

tech.proto

syntax = "proto3";

package api;

import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";

message Info {
    string desc = 1;
    google.protobuf.Duration ttl = 2;
}

service Tech {
    rpc BasicInfo(google.protobuf.Empty) returns (Info) {}
}

当它被用 NodeJs 编写的 GRPC 客户端使用时:

getTechInfo.js(前几行)

'use strict';

const PROTO_PATH = __dirname + '/../../api/tech.proto';
const grpc = require('grpc');
const apiProto = grpc.load(PROTO_PATH).api;

我收到以下错误:

/Users/././node_modules/protobufjs/dist/protobuf.js:4720
                            throw Error("failed to import '"+importFilename+"' in '"+filename+"': file not found");
                            ^

Error: failed to import '/Users/././api/google/protobuf/duration.proto' in '/Users/././api/register.proto': file not found
    at Builder.ProtoBuf.Builder.BuilderPrototype.import (/Users/././node_modules/protobufjs/dist/protobuf.js:4720:35)
    at Object.ProtoBuf.loadJson (/Users/././node_modules/protobufjs/dist/protobuf.js:5225:26)
    at Object.ProtoBuf.loadProto (/Users/././node_modules/protobufjs/dist/protobuf.js:5128:25)
    at Object.ProtoBuf.loadProtoFile (/Users/././node_modules/protobufjs/dist/protobuf.js:5174:52)
    at Object.load (/Users/././node_modules/grpc/index.js:135:26)
    at Object.<anonymous> (/Users/././src/api/getTechInfo.js:5:23)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)

问题在于 proto 文件中的导入:

import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";

解决这些导入的推荐方法是什么?提前致谢。

使用:

Node v8.9.4
"google-protobuf": "^3.5.0",
"grpc": "^1.10.1",

【问题讨论】:

    标签: node.js protocol-buffers grpc


    【解决方案1】:

    这是 gRPC 库的一个已知问题,主要记录在 this issue 中。 grpc.load API 不支持轻松加载google/protobuf/*.proto 文件。

    最简单的解决方案是使用@grpc/proto-loader 库,它会在加载proto 文件时自动包含google/protobuf/*.proto 文件。

    另一种解决方案是使用grpc-tools 预生成可与google-protobuf 库一起使用的文件。

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2021-05-16
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多