gRPC is a modern open source RPC framework that can run in any environment. It uses HTTP/2 for transport and Protocol Buffers for message serialization. gRPC is available in many languages, including C#, Go, Java, Node.js, PHP, Python, and Ruby.
In this article, we will show how to use gRPC in your backend. We will cover the following topics:
The first thing you need to do is install the gRPC server. You can do this using the following command:
$ go get -u github.com/grpc/grpc-go/cmd/grpc_server
Once the server is installed, you need to start it. You can do this using the following command:
$ grpc_server
The server will start and listen on port 8080 by default.
Next, you need to define a gRPC service. A gRPC service is defined using a .proto file. The file contains the following:
A message definition is used to define the data that will be exchanged between the client and server. The following is an example message definition:
message Request {
string text = 1;
}
message Response {
string text = 1;
}
A service definition is used to define the RPC methods that can be called by the client. The following is an example service definition:
service Service {
rpc Process(Request) returns (Response);
}
Once you have defined your gRPC service, you need to implement it. The following is an example implementation in Go:
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/grpc/grpc-go/examples/helloworld/helloworld"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer.
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, 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)
}
}
Once you have implemented your gRPC service, you can call it from your client. The following is an example client in Go:
package main
import (
"context"
"log"
"time"
"google.golang.org/grpc"
pb "github.com/grpc/grpc-go/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
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.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
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 article, we have shown how to use gRPC in your backend. We have covered the following topics:
If you want to learn more about gRPC, we recommend the following resources: