Thriftpy
Thriftpy
Thriftpy是饿了么根据Apache Thrift(RPC)协议标准,使用纯Python实现的用于构建内部SOA框架的基础组件,Thriftpy目前已开源,
Thriftpy代码在github上托管,项目地址为https://github.com/eleme/thriftpy
数据类型
bool:布尔类型(true or value),占一个字节
byte:有符号字节
i16:16位有符号整型
i32:32位有符号整型
i64:64位有符号整型
double:64位浮点数
string:未知编码或者二进制的字符串
代码示例:
dingdong.thrift
service DingService {
string ding(),
}
pingpong.thrift
service PingService {
string ping(),
}
service AargsPingService {
string ping(1:string host, 2:i32 port);
}
service Sleep {
oneway void sleep(1:i32 sencond);
}
thriftServer.py
#-*-coding:utf-8-*-
#@author :vict
#@Time :2020-11-03 10:52
#@File :thriftServer
#@software :PyCharm
import thriftpy
from thriftpy.rpc import make_server
from thriftpy.thrift import TProcessor, TMultiplexedProcessor
from thriftpy.transport import TBufferedTransportFactory, TServerSocket
from thriftpy.protocol import TBinaryProtocolFactory
from thriftpy.server import TThreadedServer
import time
class Dispatcher(object):
def ping(self):
return "ping successful"
def ping(self, host, port):
print("{}:{}".format(host, port))
return "ping {}:{}".format(host, port)
def sleep(self, seconds):
print("I'm going to sleep %d seconds" % seconds)
time.sleep(seconds)
print('Sleep over!')
class DingDispatch(object):
def ding(self):
return 'dong'
DD_SERVICE_NAME = "dd_thrift"
PP_SERVICE_NAME = "pp_thrift"
def test_1():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
server = make_server(pingpong_thrift.PingService, Dispatcher(), '127.0.0.1', 6000)
server.serve()
# 提供多个服务, 这个时候需要建立多个.thrift文件
def test_2():
dd_thrift = thriftpy.load("dingdong.thrift", module_name="dd_thrift")
pp_thrift = thriftpy.load("pingpong.thrift", module_name="pp_thrift")
dd_proc = TProcessor(dd_thrift.DingService, DingDispatch())
pp_proc = TProcessor(pp_thrift.PingService, Dispatcher())
mux_proc = TMultiplexedProcessor()
mux_proc.register_processor(DD_SERVICE_NAME, dd_proc)
mux_proc.register_processor(PP_SERVICE_NAME, pp_proc)
server = TThreadedServer(mux_proc, TServerSocket('127.0.0.1', 6000),
iprot_factory=TBinaryProtocolFactory(),
itrans_factory=TBufferedTransportFactory())
server.serve()
# 传参数
def test_3():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
server = make_server(pingpong_thrift.AargsPingService, Dispatcher(), '127.0.0.1', 6000)
server.serve()
# 采用异步调用的方式,需要在.thrift文件中进行加了关键词oneway定义
# oneway表示该方法没有任何返回值,返回值类型必须为void
# 客户端调用后不等待服务器响应,直接返回
# 服务器端收到请求,执行相关业务逻辑后,不返回任何返回值
def test_4():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
# 创建一个服务,在127.0.0.1的6000端口进行监听
# 将class Dispatcher和module pingpong_thrift中名为Sleep的Object绑定
server = make_server(pingpong_thrift.Sleep, Dispatcher(), '127.0.0.1', 6000)
server.serve()
test_4()
thriftClient.py
#-*-coding:utf-8-*-
#@author :vict
#@Time :2020-11-03 10:53
#@File :thriftClient
#@software :PyCharm
import thriftpy
from thriftpy.rpc import make_client
from thriftpy.rpc import client_context
from thriftpy.protocol import (
TBinaryProtocolFactory,
TMultiplexedProtocolFactory
)
DD_SERVICE_NAME = "dd_thrift"
PP_SERVICE_NAME = "pp_thrift"
def test_1():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
client = make_client(pingpong_thrift.PingService, '127.0.0.1', 6000)
print(client.ping())
def test_2():
dd_thrift = thriftpy.load("dingdong.thrift", module_name="dd_thrift")
pp_thrift = thriftpy.load("pingpong.thrift", module_name="pp_thrift")
binary_factory = TBinaryProtocolFactory()
dd_factory = TMultiplexedProtocolFactory(binary_factory, DD_SERVICE_NAME)
with client_context(dd_thrift.DingService, '127.0.0.1', 6000,
proto_factory=dd_factory) as c:
print(c.ding())
pp_factory = TMultiplexedProtocolFactory(binary_factory, PP_SERVICE_NAME)
with client_context(pp_thrift.PingService, '127.0.0.1', 6000,
proto_factory=pp_factory) as c:
print(c.ping())
def test_3():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
client = make_client(pingpong_thrift.AargsPingService, '127.0.0.1', 6000)
print(client.ping("127.0.0.1", 6000))
def test_4():
pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift")
client = make_client(pingpong_thrift.Sleep, '127.0.0.1', 6000)
client.sleep(2)
test_4()