- Published on
Hello GRPC
- Authors
- Name
- Moch Lutfi
- @kaptenupi
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:
_15syntax = "proto3";_15_15package helloworld;_15_15service Greeter {_15 rpc SayHello (HelloRequest) returns (HelloReply) {}_15}_15_15message HelloRequest {_15 string name = 1;_15}_15_15message HelloReply {_15 string message = 1;_15}
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:_1protoc --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:
_33package main_33_33import (_33 "context"_33 "log"_33 "net"_33_33 "google.golang.org/grpc"_33 pb "path/to/helloworld"_33)_33_33const (_33 port = ":50051"_33)_33_33type server struct{}_33_33func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {_33 log.Printf("Received: %v", in.Name)_33 return &pb.HelloReply{Message: "Hello " + in.Name}, nil_33}_33_33func main() {_33 lis, err := net.Listen("tcp", port)_33 if err != nil {_33 log.Fatalf("failed to listen: %v", err)_33 }_33 s := grpc.NewServer()_33 pb.RegisterGreeterServer(s, &server{})_33 if err := s.Serve(lis); err != nil {_33 log.Fatalf("failed to serve: %v", err)_33 }_33}
- Build and run the server: Build the server using the Go build command, and then run the server executable.
_2build server.go_2./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:
_28package main_28_28import (_28 "context"_28 "log"_28_28 "google.golang.org/grpc"_28 pb "path/to/helloworld"_28)_28_28const (_28 address = "localhost:50051"_28)_28_28func main() {_28 conn, err := grpc.Dial(address, grpc.WithInsecure())_28 if err != nil {_28 log.Fatalf("did not connect: %v", err)_28 }_28 defer conn.Close()_28 c := pb.NewGreeterClient(conn)_28 name := "world"_28 r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})_28 if err != nil {_28 log.Fatalf("could not greet: %v", err)_28 }_28 log.Printf("Greeting: %s", r.Message)_28}
- 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.
_2go build client.go_2./client
Output:
_12023/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.