liveBook · Manning

Proto file

As explained in gRPC Microservices in Go

A Protocol Buffers file, or proto file, is a schema source file that declares Protocol Buffers message types and service interfaces.

A `.proto` file is the source of a Protocol Buffers contract. It declares messages for structured data and service functions that use those messages. In a microservice system, define the contract before generating client and server stubs, then implement the server logic and call it from a client. section 3.1 section 2.5.1

For an Order service to request a payment, a Payment contract can define `CreatePaymentRequest`, `CreatePaymentResponse`, and a `Create` function that accepts one and returns the other. The request and response types determine what the generated client can send and receive; message declarations alone do not define a callable service. section 2.5.1

syntax = "proto3";                                 #1
 
option go_package="github.com/huseyinbabal/grpc-
 microservices-in-go/listing_2.1/payment";       #2
 
message CreatePaymentRequest {
    float price = 1;
}
 
message CreatePaymentResponse {
    int64 bill_id = 1;
}
 
service Payment {
    RPC Create(CreatePaymentRequest) 
        returns (CreatePaymentResponse) {}
}

The Payment definition shows the proto3 declaration, a Go package option, two messages, and a unary service function. section 2.5.2

Run `protoc` with the Go and Go gRPC output options to generate code from the file. For `proto/order.proto`, message operations go into `order.pb.go`, while gRPC functionality goes into `order_grpc.pb.go`. The generated client exposes service-specific methods, leaving serialization and deserialization to the generated code. section 2.5.2 section 3.2.2

Listing 2.1 Command to generate source code for Go
protoc \
    --go_out=. \
    --go_opt=paths=source_relative \
    --go-grpc_out=. \
    --go-grpc_opt=paths=source_relative \
    payment.proto

This command generates Go message code and gRPC code beside the input proto file using source-relative paths. section 2.5.2

Choose the function signature for the exchange you need: a unary call returns one response, while a stream can carry multiple responses, requests, or both. Proto files can live with a service, but a separate repository lets consumers in other languages generate stubs without depending on a Go production project. section 2.5.2 section 3.3

How gRPC Microservices in Go covers it

How deep it goes
Works through definitions, generation options, and generated Go files, but defers complete client examples.
Examples
Uses Payment creation and Order creation contracts, `protoc` commands, and a generated Order client call.
What it assumes
Assumes a client-server microservice use case while introducing `.proto` message and service definitions from their basic role.
What you can do after
Build it from scratch: define a proto3 service contract and generate Go message and gRPC code with `protoc`.
How it connects
Connects Protocol Buffers serialization, gRPC service functions, unary RPC, streaming RPC, and generated client and server stubs.

Also explained in

Compare how 3 books explain proto file →

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.