Implementing RPC services

The gRPC framework leverages the stream multiplexing capabilities of HTTP/2 so that it can handle both synchronous and asynchronous RPCs. The protoc compiler, when invoked with the grpc plugin enabled, will generate the following:

The following short code snippet demonstrates how we can create a new gRPC server, register our implementation for the Foo service, and start serving incoming RPCs:

func serve(addr string, serverImpl FooServer) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
grpcServer := grpc.NewServer()
RegisterFooServer(grpcServer, serverImpl)
return grpcServer.Serve(l)
}

In the following four sections, we will examine the different types of RPC modes that are supported by the gRPC framework.