Understanding the essentials of gRPC

The fundamental structure of a gRPC client-server interaction

Figure 1: The fundamental structure of a gRPC client-server interaction

The de-facto standard for exchanging data between client and server under gRPC is Protocol Buffers

Figure 2: The de-facto standard for exchanging data between client and server under gRPC is Protocol Buffers

The .proto file for the SimpleService demonstration API

Listing 1: The .proto file for the SimpleService demonstration API

Defining Messages

A message represents a custom gRPC data type described in Protocol Buffers format. A message is composed of fields. A field's type can be one of the Protocol Buffers scalar types. Also, a field's type can be another message. Table 1 below lists the scalar types defined by the Protocol Buffers specification. A description of complex types is provided in the section, Understanding the Protocol Buffers Message Format, that follows.

Type Example Comment
double 2.5 double has 15 decimal digits of precision
float 11.23 double has 7 decimal digits of precision
int32 -4 Range: -2,147,483,648 to 2,147,483,647
int64 4 Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
uint32 564 0 to 4,294,967,295; Uses variable-length encoding.
uint64 3 0 to 18,446,744,073,709,551,615; Uses variable-length encoding.
sint32 -99 Signed int value. These more efficiently encode negative numbers in the Protocol Buffer binary format than regular int32
sint64 99 Signed int value. These more efficiently encode negative numbers in the Protocol Buffer binary format than regular int64
fixed32 45 Always four bytes. More efficient than uint32 if values are often greater than 228
fixed64 45 Always eight bytes. More efficient than uint64 if values are often greater than 256
sfixed32 -76 Always four bytes
sfixed64 76 Always eight bytes
bool TRUE True or False, 0 or 1
string "Hello World"
bytes < Buffer 0a 0d 49 20 >
Protocol Buffers binary format specification encodes and decodes into binary data according to the message definition in the .proto file

Figure 3: Protocol Buffers binary format specification encodes and decodes into binary data according to the message definition in the .proto file

Katacoda screenshot
+

Esta página está disponible en español

Ver en español