How gRPC works

The benefit of gRPC in comparison with JSON-RPC is speed. gRPC can work faster, because it uses a fast serialization format—Protocol Buffers. Both gRPC and Protocol Buffers were originally developed by Google and are proven in high-performance cases.

gRPC uses HTTP/2 for transport. It's a really fast and good transport protocol. First, it's binary: all requests and responses are squeezed into a compact portion of bytes and compressed. It's multiplexed: you can send a lot of requests simultaneously, but HTTP/1 demands respect for the order of requests.

gRPC needs a scheme and uses Protocol Buffers as the Interface Definition Language (IDL). Before you start writing an implementation of a service, you have to write the proto file that contains a declaration of all types and services. After that, you need to compile the declaration to sources (in the Rust programming language in our case) and use them to write the implementation.

The protobuf crate and the common gRPC crates use that crate as a basis. Actually, there are not many crates; just two: the grpcio crate, which is a wrapper over the original gRPC core library, and the grpc crate, which is the pure Rust implementation of the protocol.

Now we can rewrite the previous example from JSON-RPC protocol to gRPC. At first, we have to add all the necessary dependencies and write a declaration of our service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.115