What is gRPC
- Open Source RPC System
- developed by Google in 2015
- Use HTTP/2 for Transport and Protobuf as interface description language.
- Provides features such as authentication, bidirectional streaming and flow control, blocking or nonblocking bindings, and cancellation and timeouts.
- generates cross-platform client and server bindings for many languages
Where we use gRPC
- connecting services in microservices style architecture
- Connecting mobile devices and browser client to Backend Service
- gRPC user protobuf, contrary to JSON. Hence follows strict specification
The client and server can talk to each other in variety of environment
Working with protbuf
Protobuf is data language which is used to serialize structured data(Object, Array). It is faster than JSON and XML.
- Create .proto file
- Define the structure for the data you want to serialize
message Person { string name = "Akanksha"; int32 id = 1; bool is_mcu_fan = false; }
- Use the protocol buffer compiler
protoc
to generate data access classes in preferred language(s) from your proto definition.These provide simple accessors for each field, likename()
andset_name()
, as well as methods to serialize/parse the whole structure to/from raw bytes.
Core concepts, architecture and lifecycle
gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
gRPC lets you define four kinds of service method:
- Single Request, Single Response
Unary RPC
- Single Request, Stream Response
Serve Streaming RPC
- Stream Request, Single Response
Client Streaming RPC
- Stream Request, Stream Response
Bidirectional streaming RPC
The gRPC programming API in most languages comes in both synchronous and asynchronous flavors.
gRPC supports termination and cancellation of RPC request and deadline for waits.