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









