This paper introduces the basic concepts of RPC, gRPC and Protocol Buffers. Suitable for beginners

TL; DR

  • RPC describes how to use remote objects or methods as if they were local. Right
  • GRPC uses Protocol Buffers as its interface definition Language (IDL) and its underlying message exchange format
  • The node instance

GRPC is an RPC framework

Remote Procedure Call (RPC) – Remote Procedure Call. In simple terms, I’m calling a function locally, or a method on an object, and I’m actually calling a function on a remote machine, or a method on a remote object, but this communication is transparent to the programmer, that is, it achieves a kind of location transparency. RPC is a technical idea, not a specification. Protocol only defines the point-to-point invocation process between the Client and Server, including stub, communication protocol, and RPC message resolution. In practical applications, high availability of services and load balancing must be considered

GRPC and protocol buffers

GRPC is a high-performance, open source and generic RPC framework designed for mobile and HTTP/2 with multi-language support.

Designed based on the HTTP/2 standard, gRPC brings features such as bidirectional streaming, flow control, header compression, and multiplexing requests over a single TCP connection. These features allow it to perform better on mobile devices, saving power and space.

GRPC uses ProtoBuf as its IDL.

Like many RPC systems, gRPC is based on the idea of defining a service, specifying methods that can be called remotely with its parameters and return types. On the server side, the server implements this interface and runs the gRPC server to handle client calls. On the client side, the client has a stub (called the client in some languages) that provides the same methods as the server. Protocol buffers are used for data serialization. The structure of person is defined below

meassage Person {
 string name = 1;
 int32 id= 2;
 boo has_ponycopter = 3
}
Copy the code

Use the Protocol Buffers compiler protoc to generate data access classes for the corresponding language and generate accessors such as name(), set_name(), and serialize/parse methods

Define the Greeter service

service Greeter {
 rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloResquest {
 string name = 1;
}

message HelloReply {
 string message = 1;
}
Copy the code

Node implements gRPC Server and client

GRPC supports multiple languages. In Node, there are two ways to use Protocol buffers. One is to generate code dynamically at run time using protobuf.js, and the other is to generate code statically using a Protoc compiler.

Define the service

Protocol Buffer files are named with the extension. Proto

Service specifies service, RPC specifies method name, and message specifies type

message Point { int32 latitude = 1; int32 longitude = 1; } service RouteGuide { rpc GetFeature(Point) returns (Feature) {} rpc ListFeatures(Rectangle) returns (stream Feature) {}}Copy the code
use@grpc/proto-loaderLoad the proto

Dynamic operation

Create a server

Implement RPC methods according to the Service described in the Protocol buffer

function getFeature(args) {
  // implement
}

var server = new grpc.Server();
server.addProtoService(
  routeguide.RouteGuide.service, 
  {
    getFeature: getFeature,
  }
);
server.bind('0.0.0.0:50051')
Copy the code

Create a clinet

You need to load proto first to know the description of the service on the server. Create a stub for the server

/ / specified server0.0.0.0:50051
var client = new routeguide.RouteGuide(
    'localhost:50051', 
    grpc.credentials.createInsecure()
);

client.getFeature()
Copy the code

GRPC Basics – node.js details