gRPC is a modern, open - source, high - performance Remote Procedure Call (RPC) framework. It allows client and server applications to communicate transparently and simplifies the development of distributed systems. gRPC uses Protocol Buffers to define the service interface and the structure of the request and response messages. It supports multiple programming languages, including Go, Java, Python, and C++.
protoc
compiler, which is used to generate code from .proto
files. You can download it from the official Protocol Buffers releases page (
https://github.com/protocolbuffers/protobuf/releases)
.go install google.golang.org/protobuf/cmd/protoc - gen - [email protected]
go install google.golang.org/grpc/cmd/protoc - gen - go - [email protected]
Let’s start by defining a simple gRPC service in a .proto
file. Create a file named hello.proto
with the following content:
syntax = "proto3";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloResponse {
string message = 1;
}
In this .proto
file, we define a service named Greeter
with a single RPC method SayHello
. The SayHello
method takes a HelloRequest
message and returns a HelloResponse
message.
To generate the Go code from the hello.proto
file, run the following command in the terminal:
protoc --go_out=. --go - grpc_out=. hello.proto
This command will generate two files: hello.pb.go
and hello_grpc.pb.go
. The hello.pb.go
file contains the generated code for the message types, and the hello_grpc.pb.go
file contains the generated code for the gRPC service interface.
Now, let’s implement the gRPC server in Go. Create a file named server.go
with the following content:
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "your_package_path/helloworld"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloResponse{Message: "Hello " + in.GetName()}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
In this code, we define a server struct that implements the GreeterServer
interface. The SayHello
method receives a HelloRequest
message, logs the received name, and returns a HelloResponse
message.
Create a file named client.go
to implement the gRPC client:
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "your_package_path/helloworld"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
name := "World"
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
In this client code, we establish a connection to the server, create a client object, and call the SayHello
method with a HelloRequest
message. Finally, we print the received HelloResponse
message.
In this tutorial, we have covered the basics of using gRPC in Golang. We started by defining a gRPC service using Protocol Buffers, generating Go code from the .proto
file, and then implementing the gRPC server and client in Go. We also discussed some common practices and best practices for using gRPC. By following these steps, you can build efficient and scalable microservices using gRPC and Golang.