1. What is GRPC?

First, understand Remote Procedure Calls (RPC), or Remote Procedure calls. Allows us to call methods on remote machines as if they were local methods. That is, machine A invokes A command to invoke A method, but the service is on machine B, so it is called A remote procedure call. It is designed to solve the problem of invocation between different services.

GRPC is a concrete implementation of RPC. High-performance, open source, and generic RPC framework for mobile and HTTP/2 design.

A complete RPC implementation usually includes a transport protocol and a serialization protocol. GRPC corresponds to the HTTP2 and Protocol Buffers data serialization protocols used. This is high performance because they are binary protocols.

GRPC in-depth, it involves many aspects, such as Protocol Buffers, HTTP2 principle what. I’ll go into details later when I have time. This article will not expand.

2. Use in Node (static)

This note is a supplement to the official example of static usage, assuming you are familiar with the definition of proto files and know the basics of client-server interaction. If not, go to the official address and run the code. Github.com/grpc/grpc/t…

The official part is the use of base types and sets and gets for nested objects.

What it lacks is the use of maps, when array elements are objects, to convert json structures. Of course, there may be more than these missing, but I need these in the project, fortunately, with the help of the great God, these problems were solved. Maybe it’s because I’m not familiar with the principle.

Node provides two modes: dynamic (dynamic_codegen) and static (static_codegen). Personally, I think there is a difference in implementation between these two methods. I asked the official and he answered me in this way

“In this context. “static” code generation refers to generating files ahead of time using grpc-tools and using them with the google-protobuf library. “Dynamic” code generation refers to loading .proto files and generating objects at runtime Using the @grPC /proto-loader library. Those two libraries provide different interfaces for the same services.”

Google translate into Chinese

In this case, “static” code generation means using GRPC-Tools in conjunction with the Google-Protobuf library to generate files ahead of time. “Dynamic” code generation means loading.proto files and generating objects at run time using the @grPC/proto-Loader library. The two libraries provide different interfaces for the same service.

It depends on the project. But it’s best to be consistent. If the person you are interfacing with is using static, stick to static.

Dynamic mode is not mentioned, the official example is written very clearly. Mainly static mode, note: the code is simplified version, only the core part, so be sure to learn the official example, have a basic line.

1. The use of the map

Suppose we have a proto file like this.

syntax = "proto3";

package request; 

service Demo {
    rpc GetDemo(GetDemoRequest) returns (GetDemoResponse) {}
}

message GetDemoRequest{	
}

message GetDemoResponse{    map<string, string> map = 1;
}
Copy the code

After generating two static files, we can use them like this

const res = new messages.GetDemoResponse();

res.getMapMap().set('1', 'a').set('2', 'b');
Copy the code

Map only provides the get method (getMapMap), so I don’t know how to use it.

Why does the map type only provide get methods that let you get back and then set? My leadership and I explained, some memory is described: if is oneself write directly set, create memory allocation system when memory is random, is not conducive to the management, but through the GRPC, first to get back to work the GRPC will help you to do, it will set up a large block of memory, for the memory management.

2. Array elements are objects

Change the proto file above

syntax = "proto3"; package request; service Demo { rpc GetDemo(GetDemoRequest) returns (GetDemoResponse) {} } message GetDemoRequest{ } message GetDemoResponse{ repeated Arr obj= 1; } message Arr { string name = 1; }Copy the code

Arrays provide three methods, set list, getObjList, addObj, that’s the old way, we usually just use set and get, it’s not too easy. But the print was a mess. Why is an element not an object? Because of the above definition, elements should come from Arr objects.

Finally, I asked for help from a big god, and after some trouble, I found that it should be addObj. Why I didn’t use this method at the beginning may be because I was misled by the official. The official is a basic type, not an object. And then IT just kept getting fixed in my head…

It’s also easy to use:

const res = new messages.GetDemoResponse(); const arr = new messages.Arr(); Arr.setname (' ABC ') res.addobj (arr) // res.add(new messages.arr ().setName(' ABC ')Copy the code

3. Convert it to a JSON structure

Use the toObject method provided with the file.

3. Static file generation

The way of use is not very simple, however, I groped for a long time this dish chicken, in addition to see the official, there is no information on the Internet, or are the official handling. I hope the beginners can get started quickly.

There was another question that I had to explore for a long time. It’s about how static files are generated.

  1. NPM download the grPC-tools tool (there was an optional bug in this tool before, I thought it was my problem for a long time, but finally I asked the official, and the official has solved it)

  2. To use the command, I write the command as a BAT file.

    protoc --js_out=import_style=commonjs,binary:./ --plugin=protoc-gen-grpc=./grpc_node_plugin.exe --grpc_out=./ helloworld.proto
    Copy the code

–js_out=import_style=commonjs,binary: Js_out =import_style=commonjs,binary The example is to output to the current directory

–plugin=protoc-gen-grpc=./grpc_node_plugin.exe –plugin=protoc-gen-grpc=./grpc_node_plugin.exe –plugin=protoc-gen-grpc=./grpc_node_plugin.exe –plugin=protoc-gen-grpc=./grpc_node_plugin.exe –plugin=protoc-gen-grpc=./grpc_node_plugin.exe

–grpc_out indicates the output location of grpc_out files, for example, to the current directory

Helloworld. Proto Specifies the proto, which can be preceded by a path, as in the current path.

4. Why use GRPC

Vaguely remember my leader said to me: commonly used protocols are HTTP, TCP and UDP, there are certain problems, UDP transmission is fast, but does not guarantee that the instruction must arrive, and lost packets will not be notified. TCP can be guaranteed, but TCP also has its own problems, packet loss, packet error, security issues, timing disorders and so on. These are problems that need to be solved by themselves, but if you use GRPC, it already does this part of the work, and the workload is reduced. HTTP sends a lot of redundant data, but GRPC uses the Protobuffer protocol, which compresses and serializes the data.

I also just contact GRPC, will point to simple use, in the principle of this, but also need to supplement.