liveBook · Manning

Protocol buffer

As explained in gRPC Microservices in Go

Protocol Buffers is a language- and platform-neutral mechanism for defining and serializing structured messages and service interfaces.

Protocol Buffers (Protobuf) defines structured messages and service functions in .proto files, then serializes message data for exchange between services. Its definitions are language- and platform-neutral; generated code lets clients and servers use them in their own languages. section 3.1 section 2.5.1

To connect an Order service to a Payment service over gRPC:

  • Define request and response messages, plus a Payment service function that accepts and returns them, in a .proto file. section 2.5.1
  • Use protoc to generate client and server stubs. Implement the Payment-side logic, then call it from Order through the generated client stub. section 2.5.1
message CreatePaymentRequest {
    int user_id = 1;
    float64 price = 2;
}
 
message CreatePaymentResponse {
    int bill_id = 1;
}
 
service Payment {
    RPC Create(CreatePaymentRequest) 
    returns (CreatePaymentResponse) {}
}

The listing shows how a payment request, a response containing a bill ID, and a service function fit into one interface definition. section 2.5.1

Generated Go structs represent the messages in application code. Marshalling converts a request to bytes for transmission; unmarshalling converts received bytes back into usable data. The binary encoding carries field metadata as well as values. Small field numbers can use less metadata, while larger integer values can require additional bytes. section 2.5.1 section 3.1.2

Compact binary messages suit interservice communication with strict latency or multilanguage requirements. They are not, by themselves, a reason to replace REST: browser support favors REST, and changes to a gRPC message contract must follow rules rather than treating requests as freely editable JSON. section 1.1.1 section 1.2

How gRPC Microservices in Go covers it

How deep it goes
Works through .proto definitions, stub generation, and basic binary encoding, but does not detail compatibility rules here.
Examples
Uses Order-to-Payment requests and responses, generated Go structs, and an integer-field encoding example.
What it assumes
Assumes a microservice client–server setting while introducing .proto message definitions and the stub-generation workflow.
What you can do after
Apply it via protoc to generate Go stubs for a defined gRPC service interface.
How it connects
Connects gRPC, RPC, client and server stubs, marshalling, and binary encoding.

Also explained in

Compare how 10 books explain protocol buffer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.