2

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?

WeiTang Lau
  • 103
  • 1
  • 11
  • Sharing raw proto files is recommended. Something like this https://medium.com/namely-labs/how-we-build-grpc-services-at-namely-52a3ae9e7c35 might give you some idea about how to share proto files. – San P Jul 07 '21 at 01:01
  • Duplication of https://stackoverflow.com/questions/55922886/how-to-share-protobuf-definitions-for-grpc/62128550?noredirect=1#comment130466780_62128550. – kkochanski Sep 29 '22 at 10:12

1 Answers1

3

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.

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • Thank you for your amazing explanation! However, it seems like a bit too advanced for me. Do you know of any links with the actual implementation example? – WeiTang Lau Jul 08 '21 at 04:35
  • "Just make a project like normal", but only with src/main/proto. All the examples in grpc-java have the .protos included with the .class files in their jar because that's what the plugins do by default. – Eric Anderson Jul 08 '21 at 14:57