帮助模块轻松构建grpc服务
easy-grpc的Python项目详细描述
简易GRPC
python(>;=3.7)grpc服务帮助程序库。
安装
我们建议创建一个虚拟环境。
python3.7 -m venv ./venv
source venv/bin/activate
python -m pip install --upgrade pip
安装模块:
pip install easy-grpc
用法示例
Easy GRPC帮助您通过三个步骤创建GRPC服务:
- 定义并编译原型文件
- 执行操作
- 配置并运行GRPC服务
一。定义并编译proto文件
在示例文件夹中,有一些文件是从grpclib存储库中提取的,该存储库当前用于运行此服务。您可以找到helloworld.proto和相对编译的python文件(helloworld[u pb2.py和helloworld[u grpc.py)。
syntax="proto3";packageexample.helloworld;messageHelloRequest{stringname=1;}messageHelloReply{stringmessage=1;}serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply){}}
要定义服务,必须按照官方文档grpc.io中的说明创建proto文件。
要自己编译proto文件,只需从根目录执行此命令。
python -m grpc_tools.protoc \ --proto_path=. \ --python_out=. \ --python_grpc_out=. \ ./example/helloworld.proto
Note: pay attention to the python_grpc_out param. This is not standard in the grpc_tools.protoc library, it is grpclib specific.
2.执行操作
在helloworld.proto文件中声明了一个rpc函数(您可以声明更多)。您应该为每个rpc创建一个操作,处理定义的输入消息(hellorequest)并返回相对的返回消息(helloreply)。
在本例中,将创建helloworld_action.py:
fromeasygrpcimportActionfrom.helloworld_pb2importHelloReplyclassHello(Action):asyncdefexecute(self,hello_request=None):returnHelloReply(message=f'Hello {hello_request.name}!')
action是扩展easygrpc.action.action抽象基类的类,必须覆盖execute方法。
三。配置并运行grpc服务
最后,要运行服务,您应该创建一个配置文件,用于配置和运行服务。
[SERVER]server=example.helloworld_grpc.GreeterBaseclient=example.client.SendRequesthost=127.0.0.1port=50051[ACTIONS]SayHello=example.helloworld_action.Hello
在actions部分,您应该为每个rcp函数声明一个操作。在corse中,您可以声明比已定义的可使用和公开的rpc更多的内容。
要运行服务,只需打开一个终端,然后:
source venv/bin/activate
python -m easygrpc.start
要执行客户端,请打开另一个终端,然后:
source venv/bin/activate
python -m easygrpc.start -c
您应该看到服务的响应:
Hello Mr. Easy!
数据库交互
在每个实现的操作中,您都可以访问postgresql数据库客户端库(asyncpg)。在配置文件中,定义postgresql连接参数。在操作实现中,您可以执行sql命令来与数据库交互。
创建一个名为“easy”的PostgreSQL(9.2到10版)数据库。
sudo -u postgres createdb -E UTF8 easy
然后创建一个新表:
CREATETABLEpublic.messages(idserial,textcharactervarying,PRIMARYKEY(id));
并插入一行:
INSERTINTOpublic.messages(text)VALUES('Hello PostgreSQL!');
添加以下配置部分:
[POSTGRESQL]user=postgrespassword=postgresdatabase=easyhost=localhostport=5432
修改hello操作(example/helloword_action.py):
fromeasygrpcimportActionfrom.helloworld_pb2importHelloReplyclassHello(Action):asyncdefexecute(self,hello_request=None):rec=awaitself.conn.fetchval(""" SELECT row_to_json(t) FROM ( SELECT text as message FROM public.messages WHERE id = $1 ) as t """,1)ifrecisnotNone:returnself.encode(rec,HelloReply)returnNone
要运行服务,只需打开一个终端,然后:
source venv/bin/activate
python -m easygrpc.start
要执行客户端,请打开另一个终端,然后:
source venv/bin/activate
python -m easygrpc.start -c
多服务示例
当然,您可以在proto文件中声明多个服务和客户端:
syntax="proto3";packageexample.helloworld;messageHelloRequest{stringname=1;}messageHelloReply{stringmessage=1;}serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply){}}serviceGreeterDB{rpcSayHello(HelloRequest)returns(HelloReply){}}
在这种情况下,config.ini文件将稍有不同:
[POSTGRESQL]user=postgrespassword=postgresdatabase=easyhost=localhostport=5432[SERVER]server=example.helloworld_grpc.Greeter, example.helloworld_grpc.GreeterDBclient=example.client.SendRequest, example.client.SendRequestDbhost=127.0.0.1port=50051[ACTIONS]Greeter.SayHello=example.helloworld_action.HelloGreeterDB.SayHello=example.helloworld_action.HelloDb
注意actions部分。这些键是使用点符号定义的,以标识哪个服务器用于给定的操作。
客户端也可以是多个(请参见./example/client.py):
importasynciofromgrpclib.clientimportChannel# generated by protocfrom.helloworld_pb2importHelloRequest,HelloReplyfrom.helloworld_grpcimportGreeterStub,GreeterDBStubasyncdefSendRequest(channel):greeter=GreeterStub(channel)reply=awaitgreeter.SayHello(HelloRequest(name='Mr. Easy'))print(reply.message)channel.close()asyncdefSendRequestDb(channel):greeter=GreeterDBStub(channel)reply=awaitgreeter.SayHello(HelloRequest(name='Mr. PostgreSQL'))print(reply.message)channel.close()
运行服务与启动单个服务实例一样:
source venv/bin/activate
python -m easygrpc.start
要执行客户机,您可以选择运行哪个客户机:
source venv/bin/activate
python -m easygrpc.start -c example.client.SendRequestDb
Note:If the client is not give as a the parameter than the first one is executed