Abstract: GRPC is a high-performance, universal open source RPC framework, which is developed by Google for mobile applications and based on HTTP/2 protocol standard and design, based on Protobuf serialization protocol development, and support a number of development languages.

This article is shared from the Huawei Cloud Community “Building GRPC Service with Python”, the original author: Jinggangshan _ Yangchun.

GRPC is a high-performance, universal open source RPC framework, which is designed by Google for mobile application development and based on HTTP/2 protocol standard, Protobuf serialization protocol development, and support a number of development languages. The general structure of a GRPC service is as follows:

Figure 1 shows that GRPC’s services are cross-language, but need to follow the same protocol (PROTO). One obvious advantage of GPRC over REST services is that it uses binary encoding, so it is faster than JSON/HTTP, has a clear interface specification, and supports streaming. However, its implementation is slightly more complex than REST services. Here are the steps for setting up a GRPC service.

1. Install the libraries required for Python

pip install grpcio
pip install grpcio-tools  
pip install protobuf

2. Define GRPC interface

The first step to create a GRPC service is to define the interface in the. Proto file, which is a protocol file. The communication interface between the client and the server is through the Proto file protocol. This agreement document is mainly defined services (service) interface, and request parameters and data structure of the corresponding results, see the following links for specific proto grammar (https://www.jianshu.com/p/da7)… On two-dimensional arrays and dictionaries, is commonly used in python data types, such as the expression of proto grammar see link (https://blog.csdn.net/xiaoxia)… Here is a simple example.

syntax = "proto3"; option cc_generic_services = true; RPC Hello (HelloRequest) Returns (HelloResponse) {// RPC Hello (HelloRequest) Returns (HelloResponse) {// RPC Hello (HelloRequest) Returns (HelloResponse) {// RPC Hello (HelloRequest) Returns (HelloResponse) { } // Message HelloRequest {String data = 1; Skill Skill = 2; // Skill = 2; // Support custom data format, very flexible}; Message HelloResponse {String result = 1; map<string, int32> map_result = 2; // Support map data format, similar to dict}; message Skill { string name = 1; };

3. Use Protoc and the corresponding plug-in to compile and generate the code of the corresponding language

python -m grpc_tools.protoc -I ./ --python_out=./ --grpc_python_out=. ./hello.proto

Use the compiler to convert the proto file into a py file, and directly run the above code in the current file directory.

  1. -i Specifies the directory where proto resides
  2. -m specifies that the py file is generated using protoc
  3. –python_out specifies the output path of the generated py file
  4. Hello. proto input proto file

After executing the above command, the two files hello_pb2. Py and hello_pb2_grpc.py are generated.

4. Wrote the server code of GRPC

#! /usr/bin/env python # coding=utf8 import time from concurrent import futures import grpc from gRPC_example import hello_pb2_grpc, hello_pb2 _ONE_DAY_IN_SECONDS = 60 * 60 * 24 class TestService(hello_pb2_grpc.GrpcServiceServicer): Def __init__(self): pass def hello(self, request, context): "" Implementsthe method of hello and constructs HelloResponse according to PB return object return: Param request: : Param context: :return: ''' result = request.data + request.skill.name + " this is gprc test service" list_result = {"12": } return hello_pb2.HelloResponse(result= STR (result), map_result=list_result) def run(): ''' server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) hello_pb2_grpc.add_GrpcServiceServicer_to_server(TestService(),server) server.add_insecure_port('[::]:50052') server.start() print("start service..." ) try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': run()

On the server side, we need to implement the hello method to meet the interface requirements of GRPCService in the proto file. The incoming parameter of the hello method is the HelloRequest defined in the proto file. The context is the reserved field. The return parameter is the HelloResponse defined in Proto. The code for starting the service is standard, and the IP address and port number of the service can be changed according to the demand.

5. Wrote the GRPC client code

#! /usr/bin/env python # coding=utf8 import grpc from gRPC_example import #! /usr/bin/env python # coding=utf8 import grpc from gRPC_example import hello_pb2_grpc, hello_pb2 def run(): "" Simulation request service method information :return: ''' conn=grpc.insecure_channel('localhost:50052') client = hello_pb2_grpc.GrpcServiceStub(channel=conn) skill = hello_pb2.Skill(name="engineer") request = hello_pb2.HelloRequest(data="xiao gang", skill=skill) respnse = client.hello(request) print("received:",respnse.result) if __name__ == '__main__': Run () def run():" ''' conn=grpc.insecure_channel('localhost:50052') client = hello_pb2_grpc.GrpcServiceStub(channel=conn) skill = hello_pb2.Skill(name="engineer") request = hello_pb2.HelloRequest(data="xiao gang", skill=skill) response = client.hello(request) print("received:",response.result) if __name__ == '__main__': run()

The implementation of the client-side code is relatively simple. First, the access IP and port number are defined, and then the HelloRequest data structure is defined, and hello can be called remotely. It needs to be emphasized that the client and the server must import the Hello_pb2_GRPC and Hello_PB2 modules compiled from the same proto file. Even if the server and the client use different languages, this is also a manifestation of the consistent GRPC interface specification.

6. Invoke tests

Start the code to run the server first, then start the code to run the client.

7. Summary of GRPC usage

  1. Define the interface document
  2. The tool generates server/client code
  3. The server side adds the business code
  4. After the client establishes a GRPC connection, the function is called using the automatically generated code
  5. Compiling and running

Click on the attention, the first time to understand Huawei cloud fresh technology ~