按照有关如何设置 gRPC 网关的文档,我发现自己陷入了生成 grpc 网关的第四步。
也就是说,当添加以下行时,事情就会崩溃:
import "google/api/annotations.proto";
文档说
You will need to provide the required third party protobuf files to the protoc compiler
- 但实际上并没有说明如何做到这一点。
如何添加
google/api/annotations.proto
作为依赖项?
我通过将 第三方 google apis 及其内容添加到我的项目的根目录中解决了这个问题。
感觉不对,但显然这受到鼓励
我有同样的问题,我按照这个结构解决了它:
proto
├── google
│ └── api
│ ├── annotations.proto
│ └── http.proto
└── helloworld
└── hello_world.proto
并运行命令:
protoc -I ./proto \
--go_out ./proto --go_opt paths=source_relative \
--go-grpc_out ./proto --go-grpc_opt paths=source_relative \
--grpc-gateway_out ./proto --grpc-gateway_opt paths=source_relative \
./proto/helloworld/hello_world.proto
如果您使用protoc生成存根,您需要确保 所需的依赖项在编译时可供编译器使用。 这些可以通过手动克隆和复制相关文件来找到 从 googleapis 存储库,并将它们提供给 protoc 跑步。您需要的文件是:
google/api/annotations.proto
google/api/field_behaviour.proto
google/api/http.proto
google/api/httpbody.proto
来自 grpc-gateway
例如在项目根目录中运行
git submodule add https://github.com/googleapis/googleapis
获取实际版本
我正在使用 Visual Studio。我的原始文件层次结构如下所示:
我必须调整 csproj 文件中项目的
ProtoRoot
属性,如下所示:
<ItemGroup>
<Protobuf Include="Protos\authzed\api\v1\experimental_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\google\api\annotations.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\google\api\http.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\google\rpc\status.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\protoc-gen-openapiv2\options\annotations.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\protoc-gen-openapiv2\options\openapiv2.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\validate\validate.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\core.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\debug.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\error_reason.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\openapi.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\permission_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\schema_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
<Protobuf Include="Protos\authzed\api\v1\watch_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
</ItemGroup>
对我有用的是:
import "google/api/annotations.proto";
import "google/api/http.proto";
但是我的用例是 java 和 IntelliJ。