python thrift 实现 单端口多服务的过程
Thrift是一种接口描述语言和二进制通信协议。以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过Thrift进行通信的,然后写自动化脚本之前研究了一下。
需要定义一个xxx.thrift的文件,来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用API的存根,直接调用。
和http相比,同属于应用层,走tcp协议。Thrift优势在于发送同样的数据,request包和response包要比http小很多,在整体性能上要优于http。
前言
学习了两天thrift一直想实现单端口多服务但是苦于网上的thrift实在太少而且大部分都是java实现的最后改了一个java的实现了单端口多服务
实现过程
1创建thrift文件添加两个服务TransmitHello_test
serviceTransmit{
stringinvoke(1:i32cmd2:stringtoken3:stringdata)
}
serviceHello_test{
stringhello(1:stringname)
}
2运行thrift.exe-outgen-py--genpytest.thrift
生成对应接口因为我的服务端和用户端都是用python写的所以只需要生成python接口即可
3编写server.py
#服务类1TransmitHandler
classTransmitHandler:
def__init__(self):
self.log={}
definvoke(self,cmd,token,data):
cmd=cmd
token=token
data=data
ifcmd==1:
returndata+'and'+token
else:
return'cmd不匹配'
#服务类2HelloHandler classHelloHandler: defhello(self,name): return'hello'+name
4编写服务端运行代码开启服务端
fromtestimportTransmit
fromtestimportHello_test
fromthrift.transportimportTSocket
fromthrift.transportimportTTransport
fromthrift.protocolimportTBinaryProtocol
fromthrift.serverimportTServer
#导入
fromthrift.TMultiplexedProcessorimportTMultiplexedProcessor
fromTransmitHandler_serverimportTransmitHandler
fromHello_serverimportHelloHandler
#openserver
if__name__=="__main__":
#实现单端口多服务的方法
transmit_handler=TransmitHandler()
transmit_processor=Transmit.Processor(transmit_handler)
hello_handler=HelloHandler()
hello_processor=Hello_test.Processor(hello_handler)
transport=TSocket.TServerSocket('127.0.0.1',8000)
tfactory=TTransport.TBufferedTransportFactory()
pfactory=TBinaryProtocol.TBinaryProtocolFactory()
#多processor
processor=TMultiplexedProcessor()
processor.registerProcessor('transmit',transmit_processor)
processor.registerProcessor('hello',hello_processor)
server=TServer.TSimpleServer(processor,transport,tfactory,pfactory)
print("Startingpythonserver...")
server.serve()
值得注意的是要想实现单端口多服务就必须得
引入processor=TMultiplexedProcessor()
用来注册两个服务类
processor.registerProcessor(‘name',procress对象)
name属性将会在client时用到
5运行runserver.py
如果出现Startingpythonserver…则运行成功
6编写client.py
fromtestimportTransmit
fromtestimportHello_test
fromthrift.transportimportTSocket
fromthrift.transportimportTTransport
fromthrift.protocolimportTBinaryProtocol
fromthrift.protocol.TMultiplexedProtocolimportTMultiplexedProtocol
if__name__=='__main__':
#启动服务
transport=TSocket.TSocket('127.0.0.1',8000)
transport=TTransport.TBufferedTransport(transport)
protocol=TBinaryProtocol.TBinaryProtocol(transport)
#注册两个protocol如果想要实现单端口多服务就必须使用TMultiplexedProtocol
transmit_protocol=TMultiplexedProtocol(protocol,'transmit')
hello_protocol=TMultiplexedProtocol(protocol,'hello')
#注册两个客户端
transmit_client=Transmit.Client(transmit_protocol)
hello_client=Hello_test.Client(hello_protocol)
transport.open()#打开链接
#测试服务1
cmd=1
token='1111-2222-3333-4444'
data="kong_ge"
msg=transmit_client.invoke(cmd,token,data)
print(msg)
#测试服务2
name='孔格'
msg2=hello_client.hello(name)
print(msg2)
#关闭
transport.close()
7运行client
观察结果实现单端口多服务
总结
核心就是TMultiplexedProcessor类和TMultiplexedProtocol
但是网上关于thriftpython的实例太少了导致浪费了很长时间
通过这篇文章的学习很快的明白thrift中的一些概念
到此这篇关于pythonthrift实现单端口多服务的过程的文章就介绍到这了,更多相关pythonthrift单端口多服务内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!