Published on

Hello GRPC

Authors

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

  1. 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:

_15
syntax = "proto3";
_15
_15
package helloworld;
_15
_15
service Greeter {
_15
rpc SayHello (HelloRequest) returns (HelloReply) {}
_15
}
_15
_15
message HelloRequest {
_15
string name = 1;
_15
}
_15
_15
message HelloReply {
_15
string message = 1;
_15
}

  1. Generate code: Generate the gRPC code from the .proto file using the protoc command-line tool and the gRPC Go plugin. The command to generate the Go code is:


    _1
    protoc --go_out=. --go-grpc_out=. path/to/your_service.proto

    This command generates two files: your_service.pb.go (contains the generated code for the message types) and your_service_grpc.pb.go (contains the generated code for the gRPC server and client).

  2. Implement the service: Implement the service in Golang by providing definitions for the methods declared in the .proto file. Here is an example implementation:


_33
package main
_33
_33
import (
_33
"context"
_33
"log"
_33
"net"
_33
_33
"google.golang.org/grpc"
_33
pb "path/to/helloworld"
_33
)
_33
_33
const (
_33
port = ":50051"
_33
)
_33
_33
type server struct{}
_33
_33
func (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
_33
func 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
}

  1. Build and run the server: Build the server using the Go build command, and then run the server executable.

_2
build server.go
_2
./server

  1. 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:

_28
package main
_28
_28
import (
_28
"context"
_28
"log"
_28
_28
"google.golang.org/grpc"
_28
pb "path/to/helloworld"
_28
)
_28
_28
const (
_28
address = "localhost:50051"
_28
)
_28
_28
func 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
}

  1. 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.

_2
go build client.go
_2
./client

Output:


_1
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.