douzhi2988
2016-10-13 15:50
浏览 1.4k
已采纳

如何在gRPC中使用预定义的protobuf类型(即“ google / protobuf / timestamp.proto”)

I'm trying to use google/protobuf/timestamp.proto in with gRPC plugin and Go. This is how I run protoc:

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

And this is my .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;
    ...
}

I'm seeing the following errors:

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

Am I missing something, or this is not yet supported?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在gRPC插件中使用 google / protobuf / timestamp.proto 去。 这就是我运行 protoc

  protoc -I ./ ./*.proto --go_out = plugins = grpc:。
 <  / code>  
 
 

这是我的 .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 {\  n时间戳ModifyTime = 1; 
 ... 
} 
   
 
 

我看到以下错误:

   domain.proto:找不到导入的“ google / protobuf / timestamp.proto”或有错误。
domain.proto:44:5:未定义“ Timestamp”。
    
 
 

我丢失了什么,还是尚不支持?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dqn8235 2016-10-13 17:39
    已采纳

    It is not fully supported yet, but you can make it work by changing

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

    and by fixing generated file import

    import google_protobuf "google/protobuf/timestamp.pb"
    

    to

    import google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
    
    点赞 评论
  • duangouyan3328 2016-10-21 13:26

    Add /usr/local/include to include paths to use /usr/local/include/google/api/timestamp.proto:

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

    As you can see in timestamp.proto, Timestamp exists in package google.protobuf, so you have to modify to use Timestamp like this:

    message Foo {
        google.protobuf.Timestamp modifiedTime = 1;
        ...
    }
    
    点赞 评论
  • dongtan7998 2017-01-23 13:48

    I work around the problem by passing a Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp option to the Go grpc plugin.

    In other words, I'm calling

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

    It's a bit of a hack. "Fortunately" I'm already using lots of Mprotofile=go/pkg/import/path parameters in my build setup, so it was easy to add.

    点赞 评论
  • dongmei9508 2018-11-26 13:20

    In my case problem was in my Fedora 29 setup.

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

    This was my bad setup. So i fixed it with following steps. Pay attention to grayed out command lines too.

    # 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
    

    After this I am able to use:

    import "google/protobuf/timestamp.proto";
    
    message Session {
        google.protobuf.Timestamp create_time = 1;
    }
    
    点赞 评论
  • dongzanghua8422 2019-04-28 11:19

    If you are facing this inside an alpine docker image, make sure you do an apk add protobuf-dev before generating your files using protoc.

    点赞 评论
  • douzaipou3327 2019-08-19 04:20

    In windows, clone the repository:protobuf.

    And run the command

    protoc -I=$SRC_DIR -I=$YOUR_CLONE_LOCATION/protobuf/src --go_out=$DST_DIR $SRC_DIR/$SRC_FILE
    
    点赞 评论

相关推荐 更多相似问题