@fmuyassarov
A communication system for handling request/response interactions and temporary streaming.
A client application can call a method on a server application running on another machine just like it would a local function.
Server implements the interface and runs a gRPC server to handle client requests.
Client (a.k.a.) stub, provides the same methods as the server.
Relies on Protocol Buffer for message serialization (can be replaced by JSON)
Relies on HTTP/2 as transport layer
Pluggable auth, tracing, load balancing and health checking
11 supported languages (C++, Go, Python, Java etc)
Open sourced by google in 2015 as the replacement for Stubby (micro services communication framework)
000110101011011101
gRPC
client
HTTP/2
network
gRPC
server
var opts []grpc.DialOption
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(*serverAddr, opts...)
gRPC
client
gRPC
server
request
response
rpc SayHello(HelloRequest) returns (HelloResponse);
gRPC
client
gRPC
server
request
response
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
gRPC
client
gRPC
server
request
response
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
gRPC
client
gRPC
server
request
response
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
Strongly typed – enforces data types (int32, float, string)
Backward compatibility – allows to add/remove fields without breaking older clients
Unlike JSON (gzip), it doesn't require extra tooling for reducing the payload size
Since it is in binary format, it is NOT human readable nor a self describing/human readable language
000110101011011101
gRPC
client
HTTP/2
gRPC
server
Scheme
.proto
network
.proto
files and generates code in different languages with the help of plugins.protoc
that generates Go structures and serialization methods for Protocol Buffers (but without gRPC support).
.proto
files and generates code in different languages with the help of plugins.protoc
that generates Go structures and serialization methods for Protocol Buffers (but without gRPC support).
message User {
string name = 1;
int32 age = 2;
}
type User struct {
Name string
Age int32
}
.proto
files and generates code in different languages with the help of plugins.protoc
that generates Go structures and serialization methods for Protocol Buffers (but without gRPC support).protoc
that generates Go code specifically for gRPC server and client stubs from .proto
files.
message User {
string name = 1;
int32 age = 2;
}
type User struct {
Name string
Age int32
}
.proto
files and generates code in different languages with the help of plugins.protoc
that generates Go structures and serialization methods for Protocol Buffers (but without gRPC support).protoc
that generates Go code specifically for gRPC server and client stubs from .proto
files.
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
}
type UserServiceServer interface {
GetUser(context.Context, *UserRequest) (*UserResponse, error)
}
At this point, we have a Go interface, but we still need to implement it.
protoc hello/hello.proto \
--go_out=. \
--go-grpc_out=. \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative
protoc hello/hello.proto \
--go_out=. \
--go-grpc_out=. \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative
ls -l hello
total 40
-rw-r--r--@ 1 est staff 6127 Feb 24 15:57 hello.pb.go
-rw-r--r--@ 1 est staff 277 Feb 24 15:43 hello.proto
-rw-r--r--@ 1 est staff 4688 Feb 24 15:57 hello_grpc.pb.go
hello.pb.go - interface types for servers to implement
hello_grpc.pb.go - protocol buffer code (for populating, serializing and retrieve messages.