Hello GRPC
February 23, 2023
In recent years, gRPC has gained popularity as a high-performance remote procedure call (RPC) framework that allows communication between microservices, mobile devices, and web applications. With its support for multiple programming languages and efficient binary serialization, gRPC has become a go-to option for building distributed systems.
Prerequisites
Before you can set up gRPC with Go, you will need to install the following tools on your system:
- Go (version 1.16 or higher)
- Protocol Buffers (version 3 or higher)
Steps to Setup gRPC with Go
- Define the service: Define the service using the Protocol Buffers language in a
.proto
file. This file will contain the methods that will be used for communication between the client and server. Here is an example:
syntax = "proto3";package helloworld;service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {}}message HelloRequest { string name = 1;}message HelloReply { string message = 1;}
-
Generate code: Generate the gRPC code from the
.proto
file using theprotoc
command-line tool and the gRPC Go plugin. The command to generate the Go code is:protoc --go_out=. --go-grpc_out=. path/to/your_service.protoThis command generates two files:
your_service.pb.go
(contains the generated code for the message types) andyour_service_grpc.pb.go
(contains the generated code for the gRPC server and client). -
Implement the service: Implement the service in Golang by providing definitions for the methods declared in the
.proto
file. Here is an example implementation:
package mainimport ( "context" "log" "net" "google.golang.org/grpc" pb "path/to/helloworld")const ( port = ":50051")type server struct{}func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Printf("Received: %v", in.Name) return &pb.HelloReply{Message: "Hello " + in.Name}, nil}func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) }}
- Build and run the server: Build the server using the Go build command, and then run the server executable.
build server.go./server
- Build and run the client: Build the client using the Go build command, and then run the client executable. Here is an example client implementation:
package mainimport ( "context" "log" "google.golang.org/grpc" pb "path/to/helloworld")const ( address = "localhost:50051")func main() { conn, err := grpc.Dial(address, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) name := "world" r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message)}
- Test the service: Test the service by running the client executable. If everything is set up correctly, the client will communicate with the server and output a greeting message.
go build client.go./client
Output:
2023/02/06 16:10:56 Greeting: Hello world
And that's it! You have successfully set up gRPC with Go. You can now build scalable and performant APIs using gRPC and Go.