我有一个非常简单的gRPC服务定义为:
syntax = "proto3";
package helloworld;
import "annotations.proto";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello(HelloRequest) returns (HelloReply) {
option (google.api.http) = {
post: "/api/v1/hello"
body: "*"
}
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
有趣的是,我正在使用Envoy gRPC <> JSON转码过滤器在HTTP2 / Protobuf <> HTTP1 / JSON之间进行“翻译”。有关详细信息,请参阅https://www.envoyproxy.io/docs/envoy/latest/api-v1/http_filters/grpc_json_transcoder_filter。
此外,我正在使用Bazel构建基于Java的gRPC服务。特使转码过滤器需要一些注释:
option (google.api.http) = {
post: "/api/v1/hello"
body: "*"
}
我正在使用proto_library(https://github.com/cgrushko/proto_library)从.proto定义生成相应的.java文件,但是我无法添加
import "google/api/annotations.proto";
到.proto文件,因为我不知道如何将https://github.com/googleapis/googleapis/blob/master/google/api/annotations.proto导入到bazel项目中。
谢谢。
最诚挚的问候,jj
您通常可以将.proto
复制到您自己的项目中并对其进行构建;只要上游原型没有“太多”变化,它就会继续工作。
例如,如果googleapi
调查谁使用他们的回购,如果您复制了文件,他们将无法找到您的用法。
在repo中有文件后,您可以按照https://github.com/cgrushko/proto_library/blob/04369f0d2ade8c8566727e0b6f3a53f1ba8925c0/src/BUILD中的示例进行操作。
外部存储库(例如http_archive
)允许您依赖另一个Bazel项目。使用http_archive
,它将作为构建的一部分下载和构建。
使用http_archive
,外部项目必须已经使用Bazel构建,这与googleapi
不完全相同:它没有google/api/annotations.proto
的BUILD文件。
一种选择是与他们交谈,看他们是否可以添加这样的BUILD文件(或自己发送PR)。另一种选择是使用new_http_archive
并提供您自己的BUILD文件作为定义的一部分。
将以下内容添加到WORKSPACE
文件(项目的根目录)应该或多或少地起作用:
new_http_archive(
name = "googleapi",
url = "https://github.com/googleapis/googleapis/archive/common-protos-1_3_1.zip",
strip_prefix = "googleapis-common-protos-1_3_1/",
build_file_content = "proto_library(name = 'annotations_proto', srcs = ['google/api/annotations.proto'])"
)
然后,您可以依赖它代码:
proto_library(
name = "hellow_world_proto",
...
deps = ["@googleapi//:annotations_proto"],
)
我遵循了第二个提案(可维护的方式:外部存储库)并最终得到:
工作区:
http_archive(
name = "com_google_protobuf",
sha256 = "cef7f1b5a7c5fba672bec2a319246e8feba471f04dcebfe362d55930ee7c1c30",
strip_prefix = "protobuf-3.5.0",
urls = ["https://github.com/google/protobuf/archive/v3.5.0.zip"],
)
new_http_archive(
name = "googleapi",
url = "https://github.com/googleapis/googleapis/archive/common-protos 1_3_1.zip",
strip_prefix = "googleapis-common-protos-1_3_1/",
build_file="BUILD.googleapi"
)
BUILD.google api
package(default_visibility=['//visibility:public'])
proto_library(
name = 'annotations_proto',
srcs = ['google/api/annotations.proto'],
deps = [
":http_proto",
"@com_google_protobuf//:descriptor_proto"
],
)
proto_library(
name = 'http_proto',
srcs = ['google/api/http.proto'])
建立
最后是对BUILD文件中的annotation.proto的引用:
proto_library(
name = "GreeterServices_proto",
srcs = ["GreeterServices.proto"],
deps = [
"@googleapi//:annotations_proto",
])
问候,dd