所以我正在尝试将微服务(C#、Go、NodeJS)的 monorepo 转换为使用 bazel。现在只是玩它。我专注于一次性服务以开始并将其隔离为工作空间。go 服务显然是使用 protobuf 的 gRPC 服务,带有 protoc-gen-swagger 的 grpc-gateway 和 protoc-gen-gorm(这个不支持 bazel)。
代码使用类似的命令构建go build cmd/server/server.go
我希望获得一些关于如何开始使用所有依赖项构建这个项目的指导。
我看到一些可用于 protobuf/go 的规则,但我还不习惯浏览它们或决定哪个更好(由于 grpc 网关或 protoc gen gorm,我无法正常工作) - https://github.com/stackb/ rules_proto - https://github.com/bazelbuild/rules_go - https://github.com/stackb/rules_proto/tree/master/github.com/grpc-ecosystem/grpc-gateway
代码结构如下所示:
/repo
svc1
svc2
svc3
cmd/server
BUILD.bazel
server.go
pkg
contains folders and some go files and a BUILD.bazel in each
proto
BUILD.bazel
test.proto
WORKSPACE
BUILD.bazel
现在我只在 svc3 上工作。稍后我可能会将 WORKSPACE 移动到父文件夹。
我的 WORKSPACE 看起来像这样:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "96b1f81de5acc7658e1f5a86d7dc9e1b89bc935d83799b711363a748652c471a",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.2/rules_go-0.19.2.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/0.19.2/rules_go-0.19.2.tar.gz",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains()
http_archive(
name = "bazel_gazelle",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
],
sha256 = "be9296bfd64882e3c08e3283c58fcb461fa6dd3c171764fcc4cf322f60615a9b",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_google_protobuf",
commit = "09745575a923640154bcf307fba8aedff47f240a",
remote = "https://github.com/protocolbuffers/protobuf",
shallow_since = "1558721209 -0700",
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
+ a bunch of go_repository() created by Gazelle
运行瞪羚在每个文件夹中为我的 go 项目创建了一堆 build.bazel 文件。
在 .proto 旁边,我有一个生成的 build.bazel 文件:
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "svc_proto",
srcs = ["test.proto"],
visibility = ["//visibility:public"],
deps = [
# the two github below are referenced as go_repository
"@com_github_infobloxopen_protoc_gen_gorm//options:proto_library", # not sure what to put after the colon
"@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:proto_library",
"@go_googleapis//google/api:annotations_proto",
],
)
go_proto_library(
name = "svc_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "src/test/proto/v1",
proto = ":svc_proto",
visibility = ["//visibility:public"],
deps = [
"//github.com/infobloxopen/protoc-gen-gorm/options:go_default_library",
"//github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options:go_default_library",
"@go_googleapis//google/api:annotations_go_proto",
],
)
go_library(
name = "go_default_library",
embed = [":svc_go_proto"],
importpath = "src/test/proto/v1",
visibility = ["//visibility:public"],
)
现在的问题:
- 不确定要引用其他原始文件的内容:“@com_github_infobloxopen_protoc_gen_gorm//options:proto_library”?并且不确定这是从 git 引用其他外部库的最佳方式。
- 如果我使用 bazel build //proto/v1:svc_proto 构建上述内容,我得到:没有这样的目标 '@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:proto_library':目标 'proto_library' 未在包 'protoc-gen 中声明-招摇/选项'。可能与1有关。
- 我不确定使用哪个规则。因为我需要 grpc 网关,所以我想我需要专门使用 https://github.com/stackb/rules_proto/tree/master/github.com/grpc-ecosystem/grpc-gateway 但我也不能让它们工作.
- 我使用 statik ( https://github.com/rakyll/statik ) 将 swagger 文件打包到 go to server the swagger 中。有没有其他选择,或者如果没有,我如何调用自定义 bash/命令作为链中构建过程的一部分?
总之,我很确定用于构建原型和库的 BUILD.bazel 文件的结构是错误的,并且希望得到一些最新的指导(github 上充满了过时的 repos,使用过时的规则或根本不工作)。