Once an application has been broken up into microservices there is a need for these processes to communicate with one another. Enter gRPC a consistent, lightweight remote procedure call framework. Loosely speaking, the protocol allows a client and server to communicate via HTTP2 through an interface called a protocol buffer. Refer to this page for a visual breakdown of the call flow. Without getting bogged down in the details, suffice to say grpc’s strict protocol definitions are advantageous to developers by simplifying the hassle around Inter-process Communication (IPC).
To get started:
- Create a new repo
- Define a .proto file
- Generate the associated pb files with
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative example/example.proto
If your using go mods you may notice numerous errors at this point. Not to worry issuing a go mod tidy
should clear up any errors.
Now that you have the buffer defined you need to write a client portion to send the message and a server portion to listen for and respond to the messages. Think of the client as living in your REST endpoint, simply unmarshall the incoming JSON to the protobuf and fire it off towards where ever the server is listening. The server portion might be a database, cache, etc.
Find an extremely basic version of all of this shoehorned into this grpc repo .
References
- https://grpc.io/about/
- https://developers.google.com/protocol-buffers/docs/gotutorial
- https://grpc.io/docs/languages/go/quickstart/