Start with the example files form: grpc-go
- examples/helloworld/greeter_client/main.go
- examples/helloworld/greeter_server/main.go
- examples/helloworld/helloworld/helloworld.proto
Setup your go modules and update imports to reflect changes:
$ go mod init github.com/physcat/bazel-grpc-go-example
Update imports for client and server go programs:
+ pb "github.com/physcat/bazel-grpc-go-example/helloworld"
- pb "google.golang.org/grpc/examples/helloworld/helloworld"
Update the helloworld/helloworld.proto
file go_package:
@@ -17,6 +17,7 @@ syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
+option go_package = "github.com/physcat/bazel-grpc-go-example/helloworld";
Use go to find the latest version of the protobuf and grpc tools: (Here we use the go toolchain to help us later in step 3)
$ go get -u -v github.com/protocolbuffers/protobuf
$ go get -v -u google.golang.org/grpc
Setup bazel for gazelle
- Create WORKSPACE and base BUILD.bazel as per bazel-gazelle
- Use gazelle to update WORKSPACE file form go.mod
bazel run //:gazelle -- update-repos -from_file=go.mod
Check the bazebuild project rules_go under workspace
We need a repository called com_google_protobuf
pointing to our new version of github.com/protocolbuffers/protobuf
.
Gazelle has already added this entry we just need to update it as below:
go_repository(
- name = "com_github_protocolbuffers_protobuf",
+ name = "com_google_protobuf",
importpath = "github.com/protocolbuffers/protobuf",
sum = "h1:D7TuYcQlt7ZiIgMqTuBJxvzPklWjyMhqav/sFpnx23Y=",
version = "v3.11.4+incompatible",
)
+
+load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
+
+protobuf_deps()
We don't need anything extra for the grpc because of step 1 gazelle already added everything.
Now we are ready to let gazelle generate the build files:
bazel run //:gazelle
All ready just build using bazel. (first build may be slower)
$ bazel build //greeter_server:greeter_server
$ bazel build //greeter_client:greeter_client
And run from two different terminals:
$ bazel-bin/greeter_server/linux_amd64_stripped/greeter_server
2020/04/06 22:02:19 Received: world
$ bazel-bin/greeter_client/linux_amd64_stripped/greeter_client
2020/04/06 22:02:19 Greeting: Hello world