gRPC is a high-performance, open-source, remote procedure call (RPC) framework
originally developed by Google. It uses the Protocol Buffers (protobuf) language
to define the structure of the data being transmitted between the client and the server,
and supports multiple programming languages.
In gRPC, a client can call a method on a remote server, which executes the method
and returns a response to the client. This communication can happen over HTTP/2,
which allows for more efficient communication and faster response times.
Here's an example of how to use gRPC in Golang and Docker:
First, let's create a simple gRPC service and client.
We'll define a service that simply concatenates two strings and returns the result:
package main
import (
"context"
"log"
"net"
pb "github.com/myuser/myapp/helloworld"
"google.golang.org/grpc"
)
type server struct {
pb.UnimplementedGreeterServer
}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, 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{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}