Client
- call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services
- has a local object known as stub (for some languages, the preferred term is client) that implements the same methods as the service. The client can then just call those methods on the local object, wrapping the parameters for the call in the appropriate protocol buffer message type - gRPC looks after sending the request(s) to the server and returning the server’s protocol buffer response(s).
Server
- implements this interface and runs a gRPC server to handle client calls
- implements the methods declared by the service and runs a gRPC server to handle client calls. The gRPC infrastructure decodes incoming requests, executes service methods, and encodes service responses
Protocol Buffers
- .proto extension
- for serializing structured data
- Starting from a service definition in a
.proto
file - is structured as message ( = a series of name-value pairs)
- define gRPC service and method request and responce
Protoc
compiler
- generate data structures to data access classes with your proto definition(proto file) name → name(), set_name()
- generate client- and server-side code
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
-
Create .procto file
helloworld.proto
-
Generate stub files(grpc code) by using tool
$ python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto
helloworld_pb2.py ← generated request and response classes
helloworldpb2grpc.py ← generated client and server classes
-
Implement the new method on the Server
greeter_server.py
-
Implement the new method on the Client
greeter_client.py
Bidirectional streaming RPC
In a bidirectional streaming RPC, the call is initiated by the client invoking the method and the server receiving the client metadata, method name, and deadline. The server can choose to send back its initial metadata or wait for the client to start streaming messages.
Client- and server-side stream processing is application specific. Since the two streams are independent, the client and server can read and write messages in any order. For example, a server can wait until it has received all of a client’s messages before writing its messages, or the server and client can play “ping-pong” – the server gets a request, then sends back a response, then the client sends another request based on the response, and so on.
Load Balancing
Proxy(server side) load balancing
- client issues rpcs to the a LB proxy
- LB distributes the rpc call to one of the available backend servers
- clients do not know about backend server
Client side load balancing
- client is aware of backend servers and choose one to use
- client implements the load balancing algorithms (round-robin)
Reference
- documentation
https://grpc.io/docs/languages/python/quickstart/
https://grpc.io/docs/what-is-grpc/core-concepts/
- chatting server
https://melledijkstra.github.io/science/chatting-with-grpc-in-python
https://levelup.gitconnected.com/grpc-how-to-make-bi-directional-streaming-calls-70b4a0569b5b
https://klize.github.io/beautiful-jekyll/2020-03-16-grpc-3/
- load balancing