gRPC is a protocol developed by Google to implement RPC.
It uses Protocol Buffers to serialize data, enabling high-speed communication.
API specifications are pre-defined in a .proto file using IDL, from which source code for both the server and client sides is generated.
Differences between REST and gRPC
REST is resource-oriented, whereas RPC focuses on method invocation, with data being a byproduct.
Advantages and Disadvantages
Advantages
High performance with HTTP/2
Data transfer via Protocol Buffers
Development follows a schema-first approach due to the use of IDL
Flexible streaming modes
Disadvantages
Non-compliance with HTTP/2
Limited browser support
Variability in feature implementation depending on the programming language
Serialized binary data is not human-readable
REST is also sufficiently fast
.proto Files
gRPC uses Protocol Buffers as its serialization format.
Schema definitions are made in files with the .proto extension, and code for various languages is generated using the protoc command.
In Protocol Buffers, all values have types, which can be divided into scalar types and message types.
Scalar Types
Numeric, string, boolean, byte array
Message Types
Message types with multiple fields
Multiple message types can be defined in a single .proto file
cd examples/python/helloworld
python -m grpc_tools.protoc -I../../protos --python_out=. --pyi_out=. --grpc_python_out=. ../../protos/helloworld.proto
The following files are regenerated.
1
2
3
4
5
ls -l
-rw-r--r--@ 1 xx xx 182391 18:12 helloworld_pb2.py
-rw-r--r--@ 1 xx xx 57891 18:12 helloworld_pb2.pyi
-rw-r--r--@ 1 xx xx 701891 18:12 helloworld_pb2_grpc.py
The _pd file that is updated contains the auto-generated Protocol Buffers definition classes and is generally not manually modified.
fromconcurrentimportfuturesimportloggingimportgrpcimporthelloworld_pb2importhelloworld_pb2_grpcclassGreeter(helloworld_pb2_grpc.GreeterServicer):defSayHello(self,request,context):returnhelloworld_pb2.HelloReply(message="Hello, %s!"%request.name)# Add the following functiondefSayHelloAgain(self,request,context):returnhelloworld_pb2.HelloReply(message="Hello Again, %s!"%request.name)defserve():port="50051"server=grpc.server(futures.ThreadPoolExecutor(max_workers=10))helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)server.add_insecure_port("[::]:"+port)server.start()print("Server started, listening on "+port)server.wait_for_termination()if__name__=="__main__":logging.basicConfig()serve()
Update the greeter_client.py file.
1
2
3
4
5
6
7
8
9
10
11
defrun():# NOTE(gRPC Python Team): .close() is possible on a channel and should be# used in circumstances in which the with statement does not fit the needs# of the code.print("Will try to greet world ...")withgrpc.insecure_channel("localhost:50051")aschannel:stub=helloworld_pb2_grpc.GreeterStub(channel)response=stub.SayHello(helloworld_pb2.HelloRequest(name="you"))print("Greeter client received: "+response.message)response=stub.SayHelloAgain(helloworld_pb2.HelloRequest(name="you"))print("Greeter client received: "+response.message)
The newly added method is confirmed to be functioning correctly.
Summary
This time, we researched gRPC and followed the official documentation tutorial.
gRPC allows for schema-first development and enables high-performance communication via HTTP/2. Recently, it has been gaining attention as an alternative to REST APIs, making it a technology worth familiarizing yourself with.