mor*_*dew 5 protocol-buffers proto grpc
GRPC 相对较新,并且在我的 proto 文件中出现了一个我似乎无法理解的错误。我想使用“google.protobuf.Timestamp”在消息中发送时间。我似乎无法导入它。我究竟做错了什么?
syntax = "proto3";
import "google/protobuf/timestamp.proto";
service ProfileService {
rpc ConstructProfileStructFromUser (ConstructProfileStructFromUserRequest) returns (ConstructProfileStructFromUserResponse);
}
message ConstructProfileStructFromUserRequest {
string transactionID = 1;
string User = 2;
}
message ConstructProfileStructFromUserResponse {
string UID = 1;
string ContactEmail = 2;
google.protobuf.Timestamp DateOfBirth = 3;
}
Run Code Online (Sandbox Code Playgroud)
在我的 IDE 和我的编译器(使用下面的命令)然后我得到错误
google/protobuf/timestamp.proto: File not found.
profile.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
profile.proto:21:5: "google.protobuf.Timestamp" is not defined.
Run Code Online (Sandbox Code Playgroud)
运行命令:
protoc -I profile/ profile/profile.proto --go_out=plugins=grpc:profile
Run Code Online (Sandbox Code Playgroud)
协议 --version
libprotoc 3.0.0
Run Code Online (Sandbox Code Playgroud)
Swi*_*ftD 13
protoc我在使用 apt 包管理器(Ubuntu)安装编译器后遇到了这个问题,它将protoc编译器放在类似/usr/local/bin.
默认情况下,似乎protoc期望导入存在于include相对于protoc安装目录的路径中。例如:
protoc地点:/usr/local/bin/protocinclude地点:/usr/local/bin/include/*下载如下所示的预编译二进制文件将包含所需的include目录。
来自grpc.io/docs/protoc-installation的说明
\nPB_REL="https://github.com/protocolbuffers/protobuf/releases"\ncurl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip\nRun Code Online (Sandbox Code Playgroud)\n$HOME/.local到您选择的目录下。例如:unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local\nRun Code Online (Sandbox Code Playgroud)\nexport PATH="$PATH:$HOME/.local/bin"\nRun Code Online (Sandbox Code Playgroud)\n
如果您使用包管理器安装了 protoc,则只需安装libprotobuf-dev(Ubuntu) 或protobuf-devel(Fedora) 包。
一般来说,您可以在 Ubuntu 上
apt-file find google/protobuf/timestamp.proto
或 Fedora 上
找到文件的包含包dnf repoquery --file "**/google/protobuf/timestamp.proto"(这就是我找到所需包的方法)。其他包管理器可能有类似的命令。
我的问题很简单...
我没有在本地下载 timestamp.proto,因此找不到它。
我克隆了:
https://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf
Run Code Online (Sandbox Code Playgroud)
然后,当我运行编译器时,我必须为其提供定位 timestamp.proto 文件的位置。
对我来说这是...
protoc -I profile/ -I MY_CLONED_REPO_LOCATION/protobuf/src profile/profile.proto --go_out=plugins=grpc:profile
Run Code Online (Sandbox Code Playgroud)
一旦它知道哪里有通往源头的路径,那么它就可以毫无问题地找到它。