gRPC is a high-performance, general-purpose open-source RPC framework designed based on the HTTP2 protocol standard, using Protocol Buffers as the default data serialization protocol, and supports multiple programming languages.
What is gRPC Framework
Common application scenarios for gRPC mainly focus on calls between backend services, but it can also be used in mobile applications.
Advantages of gRPC:
-
High compatibility, high performance, easy to use
-
Uses HTTP2 as the network transport layer
-
Uses Protobuf, a high-performance data packet serialization protocol
-
Generates easy-to-use SDKs through the protoc gRPC plugin
-
Client makes a single request, server responds once
-
Client makes a single request, server responds multiple times (streaming)
-
Client makes multiple requests (streaming), server responds once
-
Client makes multiple requests (streaming), server responds multiple times (streaming)
👉 Click to Get Go Backend Development Resources
Using HTTP2 Protocol
Why choose HTTP2 as the transport protocol for gRPC?
Beyond speed, the biggest reason is maximum service compatibility. Because gRPC is based on the HTTP2 protocol, and most mainstream proxy tools on the market support HTTP2, gRPC is naturally supported.
We all know the important versions of the HTTP protocol, which have been updated and iterated to solve different problems. For example, in HTTP1.0, the biggest drawback was short connections. With the advent of HTTP1.1, this performance issue was resolved, and rich header semantics were added.
In recent years, websites primarily used the HTTP1.1 protocol. However, this protocol also has many issues, such as head-of-line blocking, where a connection can only process one request at a time. Until that request returns data, other requests cannot use that connection. Generally, the Chrome browser defaults to initiating six concurrent connections, and if there are too many requests, it must wait. Although HTTP1.1 supports pipelining and some solutions exist, they are still not excellent, which led to the development of the HTTP2 protocol.
Of course, there will be better protocols in the future, such as HTTP3.
Core Concepts of gRPC
An introduction to gRPC and protocol buffers.
Writing proto files
Write the helloworld.proto file:
// Specify the protocol syntax syntax = "proto3"; // Define a package package greeterpb; // The keyword server defines a service // gRPC services specify methods that can be called remotely through their parameters and return types service Greeter { rpc SayHello(HelloRequest) returns (HelloReply) {} rpc SayHelloAgain(HelloRequest) returns (HelloReply) {} } // Define request messages // The keyword message defines the message format used for requests or responses message HelloRequest { string name = 1; } // Define response messages // The keyword message defines the message format used for requests or responses message HelloReply { string message = 1; }
-
–python_out: Path to generate code that processes protobuf
-
–grpc_python_out: Path to generate code that processes gRPC
-
-I: Specify the path of the proto file
# Compile proto file $ python -m grpc_tools.protoc \ --python_out=. --grpc_python_out=. -I. \ helloworld.proto # Generated code helloworld_pb2.py helloworld_pb2_grpc.py
Write the gRPC implementation for helloworld:
# Server side # helloworld_grpc_server.py from concurrent import futures import time import grpc import helloworld_pb2 import helloworld_pb2_grpc # Implement the GreeterServicer defined in the proto file class Greeter(helloworld_pb2_grpc.GreeterServicer): # Implement the RPC calls defined in the proto file def SayHello(self, request, context): return helloworld_pb2.HelloReply(message = 'hello {msg}'.format(msg = request.name)) def SayHelloAgain(self, request, context): return helloworld_pb2.HelloReply(message='hello {msg}'.format(msg = request.name)) def serve(): # Start RPC service server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(60*60*24) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': serve()
# Client side # helloworld_grpc_client.py import grpc import helloworld_pb2 import helloworld_pb2_grpc def run(): # Connect to RPC server channel = grpc.insecure_channel('localhost:50051') # Call RPC service stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='czl')) print("Greeter client received: " + response.message) response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='daydaygo')) print("Greeter client received: " + response.message) if __name__ == '__main__': run()
# Run to start the server $ python3 helloworld_grpc_server.py # Run to start the client $ python3 helloworld_grpc_client.py
Quick Start with gRPC Framework
This guide gets you started with gRPC in Python with a simple working example.
Build Basic Environment
Create a virtual environment:
# need python3.5+ $ python -m pip install virtualenv $ virtualenv venv $ source venv/bin/activate $ python -m pip install --upgrade pip
grpcio-tools includes the code generation tools. # install gRPC $ python -m pip install grpcio $ python -m pip install grpcio-tools # install it system-wide $ sudo python -m pip install grpcio
The repository is quite large and may require a strong VPN to download.
# clone the repository $ git clone -b v1.37.1 https://github.com/grpc/grpc # say "hello, world" $ cd grpc/examples/python/helloworld
Run a client-server application.
# run the server $ python greeter_server.py # run the client $ python greeter_client.py
Updating existing services to provide different features.
# examples/protos/helloworld.proto // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} // Sends another greeting rpc SayHelloAgain (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; }
# Enter the corresponding directory cd examples/python/helloworld # Regenerate code $ python -m grpc_tools.protoc -I../../protos \ --python_out=. --grpc_python_out=. \ ../../protos/helloworld.proto # Generated files - for data interaction with protobuf helloworld_pb2.py # Generated files - for data interaction with gRPC helloworld_pb2_grpc.py
# Adjust client code - greeter_client.py def run(): channel = grpc.insecure_channel('localhost:50051') 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) # Adjust server code - greeter_server.py class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) def SayHelloAgain(self, request, context): return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)...
# run the server $ python greeter_server.py # run the client $ python greeter_client.py
Application Scenarios of gRPC
RPC Usage Scenarios: Distributed Systems
With the continuous development of microservices, building microservices based on language neutrality has gradually become a mainstream design pattern. For example, for microservices that require high concurrency processing on the backend, it is more suitable to use Go language, while for frontend web interfaces, JavaScript is more appropriate. Therefore, using the multi-language gRPC framework to build microservices is a good technical choice.
gRPC Microservice
gRPC Kubernetes


Popular Recommendations
-
This monitoring system construction idea will help you thoroughly identify performance bottlenecks -
Leader at Goose Factory: Looking glamorous, anxious to the point of needing medication -
XX Video Creation Platform Collapse! Boss suspected of running away with 160 billion~