liveBook · Manning

Stub

As explained in gRPC Microservices in Go

A client stub is a generated client-side proxy that exposes remote service operations as local callable methods.

A client stub is a generated proxy for a remote service. It gives a consuming service callable methods that follow the service contract, while gRPC handles the underlying request and response exchange. The call still crosses a network boundary: a method that looks local can fail or time out. section 1.0 section 2.5.3

To connect an Order service to a Payment service:

  • Define request and response messages and a Payment service method in a .proto file. Generate Go message types and client and server stubs with protoc. section 2.5.1 section 3.2.2
  • Add the generated Payment client as an Order service dependency, dial the Payment endpoint, and construct the client from the connection. section 2.5.3 section 5.2
  • Call Create with a context and request, then handle its error. Set a timeout and choose retry criteria rather than assuming every remote call succeeds. section 2.5.3
Listing 2.2 Payment stub usage on client side
var opts []grpc.DialOption
 
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
  // handle connection error
}
defer conn.Close()
 
payment := pb.NewPaymentClient(conn)
result, err := payment.Create(ctx, &CreatePaymentRequest{})
if err != nil {
    // handle Payment create error
}

The Order-side call dials Payment, creates a generated NewPaymentClient, invokes Create, checks the error, and closes the connection. section 2.5.3

Generated stubs let services use a shared interface without maintaining handwritten clients for each language. They do not remove operational decisions: the consumer needs an endpoint, a managed dependency version, and handling for failed calls. In the Order service, a configured Payment endpoint initializes an adapter that is injected into the application. section 1.1.2 section 5.2 section 5.2.5

How gRPC Microservices in Go covers it

How deep it goes
Works through contract generation, client construction, connection, and error handling; leaves endpoint auto-discovery for later.
Examples
Uses generated Order and Payment clients, a Payment Create call, protoc output, and an environment-configured Payment adapter.
What it assumes
Introduces .proto messages, service definitions, and generation steps while using Go imports, connections, and contexts in the client examples.
What you can do after
Build it from scratch with protoc, then operate/configure it as a Go service dependency that makes bounded remote calls.
How it connects
Links protocol buffers, service contracts, Go modules, dependency injection, contexts, timeouts, and retries.

Also explained in

Compare how 10 books explain stub →

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.