雷声是沙发螺栓协议库。
anthunder的Python项目详细描述
安敦达(又名沙发螺栓Python)
anthunder(ant thunder)是一个用python编写的沙发螺栓库。 它支持通过“sofa bolt+protobuf”协议调用rpc。
要求
- python3>;=3.4(aio类需要异步支持)
- python2.7(支持有限,需要额外的第三方库)
- mosn>;=1.3(用于版本>;=0.6)
- mosn<;1.3(用于<;0.6版)
路线图
- [X]螺栓客户端(protobuf序列化)
- [X]通过Mosn发现服务(沙发服务网侧车)
- [X]螺栓服务器(protobuf序列化)
- []Hessian2序列化支持
教程
作为客户(呼叫者)
- 获取
.proto
文件 - 执行
protoc --python_out=. *.proto
编译protobuf文件,并获取_pb2.py
文件。 - 导入protobuf类(用
_pb2
后缀)
fromSampleServicePbResult_pb2importSampleServicePbResultfromSampleServicePbRequest_pb2importSampleServicePbRequestfromanthunderimportAioClientspanctx=SpanContext()# generate a new context, an object of mytracer.SpanContext, stores rpc_trace_context.# spanctx = ctx # or transfered from upstream rpcclient=AioClient(my_app_name)# my_app_name will be send to sidecar as caller name.# will create a thread, and send heartbeat to mesh every 30sinterface='com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0'# Subscribe interfaceclient.subscribe(interface)# Call synchronouslycontent=client.invoke_sync(interface,"hello",SampleServicePbRequest(name=some_name).SerializeToString(),timeout_ms=500,spanctx=spanctx)result=SampleServicePbResult()result.ParseFromString(content)# Call asynchronouslydefclient_callback(resp):# callback function, accepts bytes as the only argument,# then do deserialize and further processesresult=SampleServicePbResult()result.ParseFromString(content)# do somethingfuture=client.invoke_async(interface,"hello",SampleServicePbRequest(name=some_name).SerializeToString(),spanctx=spanctx,callback=client_callback))
有关可运行的演示,请参见项目的unittest
作为服务器
fromanthunder.listenerimportaio_listenerclassSampleService(object):def__init__(self,ctx):# service must accept one param as spanctx for rpc tracing supportself.ctx=ctxdefhello(self,bs:bytes):obj=SampleServicePbRequest()obj.ParseFromString(bs)print("Processing Request",obj)returnSampleServicePbResult(result=obj.name).SerializeToString()listener=aio_listener.AioListener(('127.0.0.1',12200),"test_app")# register interface and its function, plus its protobuf definition classlistener.handler.register_interface("com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0",SampleService)# start server in a standalone threadlistener.run_threading()# or start in current threadlistener.run_forever()# publish interfaces to service meshlistener.publish()# shutdown the serverlistener.shutdown()
许可证
版权所有(c)2018年至今,蚂蚁金融服务集团
apache许可证2.0
请参阅许可证文件。
第三方
mysockpool包的一部分使用来自urllib3项目的代码 根据麻省理工学院的许可证。请参阅mysockpool包下的origin-license.txt。
发布历史记录
0.5.6(2019-03-15)
错误修复
- 修复解析协议时出现的无限循环错误
0.5.4(2018-11-09)
错误修复
- 修复python2.7下的服务器错误
0.5.3(2018-08-27)
功能
- 支持AntShareCloud参数。
0.5.2(2018-09-03)
错误修复
- 修复python2.7下的各种错误
0.5.1(2018-08-31)
错误修复
- sofa trace rpc id可能包含str.