带有protobuf / gRPC网关/ golang的平台-入门

时间:2019-08-14 18:28:40

标签: bazel grpc-gateway

所以我试图将微服务(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移到父文件夹。

我的工作空间如下:

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"],
)

现在是问题:

  1. 不确定要引用其他原始文件是什么:“ @ com_github_infobloxopen_protoc_gen_gorm // options:proto_library”?并且不确定这是从git引用其他外部库的最佳方法。
  2. 如果我使用bazel build // proto / v1:svc_proto构建以上版本,则会得到:没有这样的目标'@ com_github_grpc_ecosystem_grpc_gateway // protoc-gen-swagger / options:proto_library':目标'proto_library'未在软件包'中声明protoc-gen-swagger / options”。可能与1相关。
  3. 我不确定要使用哪个规则。由于我需要grpc网关,我想我 需要专门使用 https://github.com/stackb/rules_proto/tree/master/github.com/grpc-ecosystem/grpc-gateway 但我也不能使它们工作。
  4. 我使用statik(https://github.com/rakyll/statik)来打包 swagger文件进入服务器。有没有其他选择 否则,我如何在构建过程中调用自定义bash /命令 链中的过程?

总而言之,我很确定我用于构建原型和库的BUILD.bazel文件的结构错误,并且希望了解一些最新指南(github上有很多使用过时规则或根本不使用过时规则的存储库)工作)。

0 个答案:

没有答案