【问题标题】:How to use predifined protobuf type (i.e. "google/protobuf/timestamp.proto") with gRPC如何在 gRPC 中使用预定义的 protobuf 类型(即“google/protobuf/timestamp.proto”)
【发布时间】:2017-02-22 20:22:58
【问题描述】:

我正在尝试将google/protobuf/timestamp.proto 与 gRPC 插件和 Go 一起使用。这就是我运行protoc的方式:

protoc -I  ./   ./*.proto --go_out=plugins=grpc:.

这是我的.proto

#domain.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.viant.xyz";
option java_outer_classname = "domain";

import "google/protobuf/timestamp.proto";

message Foo {
    Timestamp modifiedTime = 1;
    ...
}

我看到以下错误:

domain.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
domain.proto:44:5: "Timestamp" is not defined.

是我遗漏了什么,还是目前还不支持?

【问题讨论】:

    标签: go protocol-buffers grpc


    【解决方案1】:

    添加/usr/local/include 以包含使用/usr/local/include/google/api/timestamp.proto 的路径:

    protoc -I/usr/local/include -I. --go_out=plugins=grpc:. *.proto
    

    正如您在timestamp.proto 中看到的,Timestamp 存在于包google.protobuf 中,因此您必须修改为使用Timestamp,如下所示:

    message Foo {
        google.protobuf.Timestamp modifiedTime = 1;
        ...
    }
    

    【讨论】:

      【解决方案2】:

      目前还没有完全支持,但是你可以通过改变让它工作

      message Foo {
          google.protobuf.Timestamp modifiedTime = 1;
          ...
      }
      

      并通过修复生成的文件导入

      import google_protobuf "google/protobuf/timestamp.pb"
      

      import google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
      

      【讨论】:

        【解决方案3】:

        在我的情况下,问题出在我的 Fedora 29 设置中。

        # Install Protoc compiler. By default it is 3.5.0 version
        sudo dnf -y install protoc
        

        这是我的错误设置。所以我通过以下步骤修复了它。也要注意灰显的命令行。

        # Uninstall old 3.5.0 version
        sudo dnf remove protobuf
        
        # Make sure you grab the latest version
        curl -OL  
        https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip
        # Unzip
        unzip protoc-3.6.1-linux-x86_64.zip -d protoc3
        # Move protoc to /usr/local/bin/
        sudo mv protoc3/bin/* /usr/local/bin/
        # Move protoc3/include to /usr/local/include/
        sudo mv protoc3/include/* /usr/local/include/
        # Optional: change owner
        sudo chown $USER /usr/local/bin/protoc
        sudo chown -R $USER /usr/local/include/google
        

        之后我就可以使用了:

        import "google/protobuf/timestamp.proto";
        
        message Session {
            google.protobuf.Timestamp create_time = 1;
        }
        

        【讨论】:

          【解决方案4】:

          如果您在 alpine docker 映像中遇到此问题,请确保在使用 protoc 生成文件之前执行 apk add protobuf-dev

          【讨论】:

            【解决方案5】:

            在摸索了几个小时后,我发现了问题所在。

            我的 /usr/local/include 目录没有 /google/protobuf 文件,没有这些文件就无法使用预定义的类型。解决这个问题。

            现在你可以简单地使用这个命令了

            protoc -I/usr/local/include -I. --go_out= {output_directory_path} {proto_file_path}

            【讨论】:

              【解决方案6】:

              我通过将 Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp 选项传递给 Go grpc 插件来解决此问题。

              换句话说,我在打电话

              protoc --go_out=plugins=grpc,Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp:outputdir input.proto
              

              这有点小题大做。 “幸运的是”我已经在构建设置中使用了很多 Mprotofile=go/pkg/import/path 参数,所以很容易添加。

              【讨论】:

                【解决方案7】:

                在 windows 中,克隆存储库:protobuf

                然后运行命令

                protoc -I=$SRC_DIR -I=$YOUR_CLONE_LOCATION/protobuf/src --go_out=$DST_DIR $SRC_DIR/$SRC_FILE
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-05-31
                  • 2021-05-07
                  • 1970-01-01
                  • 2020-05-29
                  • 1970-01-01
                  • 2018-06-28
                  • 2019-07-07
                  • 2017-09-03
                  相关资源
                  最近更新 更多