WeiTang Lau
WeiTang Lau

Reputation: 123

Sharing Proto or Generated Files across Microservices

I am building a Spring Boot Microservices using protobuf and gRPC for communication. However, I realised that I will need to define the entities in all microservices that requires it.

Instead of the straight forward method of copy and paste the raw file (not recommended), I can think of 2 methods:

  1. sharing raw proto file
  2. sharing proto generated files

Which is the proper way of doing? If I am sharing raw proto files, how can I share the proto files properly across microservices?

Upvotes: 2

Views: 1704

Answers (1)

Eric Anderson
Eric Anderson

Reputation: 26434

When sharing across languages, copying raw protos between repositories is typical. Some build systems like Bazel don't need to copy the protos, but most do. When copying protos it is important there there is a single well-known canonical copy of the protos and all other copies are bit-identical to a version of the canonical copy.

But when sharing in a Java-centric collection of projects, creating a canonical Java package for the generated code is superior as it is easier to use and it helps make sure only one copy of the generated code is in the classpath.

A typical Java protobuf Jar will include both the raw protos and the generated code; the raw protos are automatically included by both the Maven and Gradle Protobuf plugins. You then depend on that Jar like normal and it provides the dependency for Java code and Protobuf definitions. The Maven and Gradle Protobuf plugins automatically find .proto files in dependencies and add them to the include path (-I) of protoc when generating code.

Upvotes: 3

Related Questions