Abstract: gRPC is a high-performance, universal open source RPC framework, which is mainly developed for mobile applications by Google and designed based on HTTP/2 protocol standards, based on the ProtoBuf serialization protocol development, and support many development languages.

This article is shared by Huawei Cloud community “Building gRPC Services with Python”, originally written by Jinggangshan _ Yangchun.

GRPC is a high-performance, universal open source RPC framework, which is mainly developed for mobile applications by Google and designed based on THE HTTP/2 protocol standard. It is developed based on the ProtoBuf serialization protocol and supports many development languages. The general structure of a gRPC service is as follows:

Figure 1 shows that GRPC’s services are cross-lingual, but need to follow the same protocol (PROTO). One of the obvious advantages 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, but its implementation is a bit more complex than REST services. Here are the steps to set up gRPC services.

1. Install the required Python libraries

pip install grpcio
pip install grpcio-tools  
pip install protobuf
Copy the code

2. Define the gRPC interface

The first step to create gRPC service is to define the interface in the. Proto file. Proto is a protocol file, and the communication interface between the client and the server is agreed by proTO file. 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 (www.jianshu.com/p/da7ed5914… That is commonly used in python data types, expression of proto grammar see links (blog.csdn.net/xiaoxiaojie…

syntax = "proto3"; option cc_generic_services = true; Service GrpcService {RPC hello (HelloRequest) returns (HelloResponse) {} Message HelloRequest {string data = 1; // the number 1,2 is the position order of the parameters and is not assigned to the parameters. // 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; };Copy the code

3. Use the protoc and corresponding plug-ins to compile and generate the corresponding language code

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

Convert the proto file to py file using a compilation tool and run the above code directly in the current file directory.

  • -i Specifies the directory where proto resides
  • -m Specifies that py files are generated using protoc
  • –python_out specifies the output path to generate the py file
  • Hello. proto Input proto file

After the preceding command is executed, two files hello_pb2.py and hello_pb2_grpc.py are generated.

4. Write 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): HelloResponse returns :param Request: :param Context: :return: ''' result = request.data + request.skill.name + " this is gprc test service" list_result = {"12": 1232} return list_response (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()Copy the code

On the server side, we need to implement the Hello method to meet the interface requirements of GrpcService in the proto file. The parameter passed to the Hello method is the HelloRequest defined in the proto file. Context is a reserved field. The return parameter is HelloResponse defined in proto. The code for starting the service is standard, and the IP address and port number of the service can be modified according to the requirements.

5. Write the code of gRPC client

#! /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(): Mock 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()Copy the code

Client-side code implementation is relatively simple, first define the access IP and port number, and then define the HelloRequest data structure, remote call Hello. It should be emphasized that the client and server must import the same proto file generated hello_PB2_GRPC, hello_PB2 module, even if the server and client use different languages, this is also the reflection of the same GRPC interface specification.

6. Invoke tests

Start the code to run the server and then start the code to run the client.

7. Use summary of gRPC

  • Define the interface documentation

  • The tool generates server/client code

  • The server side supplements the business code

  • After the client establishes the gRPC connection, it invokes the function using the automatically generated code

  • Compile, run

Click to follow, the first time to learn about Huawei cloud fresh technology ~