Generating Service Code
The .proto
files that you pulled in from the killrvideo-service-protos
project contain all the service definitions for KillrVideo. You can use these service
definitions along with the gRPC compiler plugin to generate code for use in your
implementation. The gRPC library and compiler are available in many programming languges.
The gRPC docs have detailed instructions that you should follow based on the
programming language your implementation is in, but the general steps for generating service
code are:
- Install gRPC (using the install instructions in the docs). This usually involves installing a package using the package manager for your programming language (i.e. Maven or Gradle in Java, NuGet in .NET, pip in Python, etc.).
- Generate the code using the gRPC plugin for the Protobuf compiler. Again, follow the instructions for generating code in the docs. You'll want to generate code for all of the killrvideo-service-protos files that you pulled in as a Git subtree.
Once you've done that, you should have client and server stubs, as well as request/response
objects and event objects. If possible, you'll want to check the generated code into your
implementation's source tree such that the implementation code you write won't conflict with
those generated files. For example, in the .NET implementation, we had the Protobuf
compiler generate files with a .generated.cs
extension and checked those into the source
tree. When creating our service implementations, we then wrote that implementation code in
seperate .cs
files so that we could potentially regenerate the stub code without
overwriting our implementation code.
When to Generate Code
You'll have to decide, based on your programming language's idioms, when to generate code. This could be, for example, every time the project is compiled (which is the default behavior of gRPC in Java for example), or as a separate step executed on-demand (for example, using a separate shell script or build step that you've created). Regardless of how you choose to do it, you want to make sure that you have a repeatable way to generate code checked in to the Git repo so that you can regenerate those files any time the service definitions are updated in the killrvideo-service-protos project.