Python GRPC Overview:
GRPC is a Google open source RPC (remote program call) framework that can be easily cross-lingual, cross-platform programming with GRPC protocol (based on HTTP2).
- Rpc:
Remote procedure call, which is translated is a long-range program invocation. Specifically, the client C1 needs to call a method (function) on the server S1 to get the corresponding return value and pass it to C1.
- GRPC protocol
To say that the GRPC agreement needs to understand HTTP2 first, although the http1.x agreement is still the mainstream agreement, but with our higher performance requirements, and the increasing size of the Web, HTTP2 came into being.
Here, we just need to know that its performance is higher than the existing HTTP1, interested friends can learn more about HTTP2.
Use reason
In the home company, because the previous project has a few pain points, so decided to use the RPC framework:
- Project apps, websites, m stations, small programs, etc. are independent projects, one change, everywhere change, inefficient
- All modules in one project, maintenance needs to be familiar with all business processes, maintenance more difficult
- Scalability is not strong, to cope with the high point of concurrency need to copy the project everything to the new server, run all modules, including low-volume modules, resulting in wasted resources
So we took a little time to deal with these problems, the order, users and other modules apart, easy to deploy independently, independent upgrade, independent maintenance, which can greatly improve maintenance efficiency and project scalability.
How to use
syntax = "proto3"; package order; message OrderRequest {// 定义请求数据 string phone = 1; string price = 2; map<string, string> request_arg = 3;//便于字段扩展 } message JSONResponse{// 定义返回格式 string rst_string = 1; //统一返回json字符串作处理 } service OrderHandler { // format a list of events. rpc create_order (OrderRequest) returns (JSONResponse) {} }// 其中:// message: 定义数据结构<br>// service: 定义接口的名字,参数,
- Generate Required files (required by both server and client)
python -m grpc_tools.protoc -I./ --python_out=./ --grpc_python_out=./ ./*.proto
Two files are generated after running (test_pb2.py, test_pb2_grpc.py)
import Timeimport test_pb2import test_pb2_grpcimport grpc def Test (Request): # actual call to function J Son_response = Test_pb2. Jsonresponse () json_response.rst_string = Json.dumps ({"ret": "Hi Grpc"}) # constructs the return value format defined in the proto file return Json_response Class Orderhandler (Test_pb2_grpc. Orderhandlerservicer): "Grpc request will go into this class to distribute, according to the client request method to find the corresponding processing method interested in can break point view request, context content, they contain all the information requested "Def create_order (self, request, context): Return test (Request, context) def serve (): # Turn on the GRPC service, listen to a specific port , Server = Grpc.server (futures. Threadpoolexecutor (max_workers=10)) Test_pb2_grpc.add_orderhandlerservicer_to_server ( Orderhandler (), server) Server.add_insecure_port (' [::]:{} '. Format (12006)) Server.start () Try: While True:time.sleep (186400) except KeyboardInterrupt:server.stop (0) Serve ()
- Write the client code to test client.py
import grpc import test_pb2_grpc import test_pb2 channel = grpc.insecure_channel("127.0.0.1:12006") stub = test_pb2_grpc.OrderHandlerStub(channel) # 要完成请求需要先构造出proto文件中定义的请求格式 ret = stub.create_order(test_pb2.OrderRequest(phone="12990", price="50")) print(ret.rst_string)
Python uses GRPC