各位好,这两天一直在看 grpc 相关的东西,然后 API 网关这块顺便了解了下 grpc-gateway,但是在看官方的例子中,gateway.proto 文件如下:
syntax = "proto3"; package gateway; import "google/api/annotations.proto"; message Message { string msg = 1; } service Gateway { rpc Echo (Message) returns (Message) { option (google.api.http) = { post: "/api/v1/echo" body: "*" }; } }
之前已经顺利安装了 protoc-gen-grpc-gateway 插件,但是在执行如下语句:
protoc --grpc-gateway_out=logtostderr=true:. gateway.proto
老是提示如下的错误:
google/api/annotations.proto: File not found. gateway.proto: Import "google/api/annotations.proto" was not found or had errors.
希望有熟悉的大佬们,麻烦帮忙解答下,如何安装这个缺失的 proto 文件
1 mingqing 2019-08-27 18:32:52 +08:00 这跟 grpc-gateway 没关系,你没设置好 proto 的搜索路径,如:protoc -I./path/find/name.proto xxxxx,建议看下 protoc 的用法 |
2 xsephiroth 2019-08-27 18:34:33 +08:00 via Android 记得是要在 libs 里,忘记了 docker 直接来 namely/protoc-all e.g. docker run --rm \ --user 1000:1000 \ -v $PWD/proto:/defs \ namely/protoc-all \ -l go \ -d /defs \ -o /defs \ --with-gateway |
![]() | 3 jayn1985 OP @mingqing 感谢提醒,我也注意到了这个问题,我看官方文档里设定的搜索路径是这些:-I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis,但是我的项目开启了 go mod 模式,$GOPATH/src 下面并没有这个包,倒是在$GOPATH/pkg/mod/xxxx 相应的目录下面才有。。。然而这个目录页面也是有多个 grpc-gateway 版本的,所以感觉这么写是不是有问题? |
![]() | 4 jayn1985 OP @mingqing 我刚才试了:protoc -I. -I$GOPATH/pkg/mod/github.com/grpc-ecosystem/[email protected]/third_party/googleapis --grpc-gateway_out=logtostderr=true:. gateway.proto,这样就可以了,确实是需要指定 proto 文件的完整路径才行,但是我的问题是,其实 [email protected] 同级目录下还有别的版本的包,如果在实际项目多人协作开发过程中,这么指定目录不会有问题么,以后升级了这个包咋办? |
![]() | 6 hzzhzzdogee 2022-04-17 21:50:22 +08:00 |