python thrift 實現 單端口多服務的過程
Thrift 是一種接口描述語言和二進制通信協議。以前也沒接觸過,最近有個項目需要建立自動化測試,這個項目之間的微服務都是通過 Thrift 進行通信的,然后寫自動化腳本之前研究了一下。
需要定義一個xxx.thrift的文件, 來生成各種語言的代碼,生成之后我們的服務提供者和消費者,都需要把代碼引入,服務端把代碼實現,消費者直接使用API的存根,直接調用。
和 http 相比,同屬于應用層,走 tcp 協議。Thrift 優勢在于發送同樣的數據,request包 和 response包 要比 http 小很多,在整體性能上要優于 http 。
前言
學習了兩天thrift 一直想實現單端口多服務 但是苦于網上的 thrift 實在太少 而且大部分都是java實現的 最后 改了一個java的 實現了 單端口多服務
實現過程
1 創建 thrift 文件 添加兩個服務 Transmit Hello_test
service Transmit {string invoke(1:i32 cmd 2:string token 3:string data)}service Hello_test {string hello(1: string name)}
2 運行 thrift.exe -out gen-py --gen py test.thrift
生成對應接口 因為我的 服務端和 用戶端 都是用 python寫的 所以 只需要 生成python 接口即可
3 編寫 server.py
# 服務類1 TransmitHandlerclass TransmitHandler: def __init__(self): self.log = {} def invoke(self, cmd, token, data): cmd = cmd token = token data = data if cmd == 1: return data + ’and’ + token else: return ’cmd不匹配’
# 服務類2 HelloHandlerclass HelloHandler:def hello(self, name):return ’hello’+name
4 編寫服務端運行代碼 開啟服務端
from test import Transmitfrom test import Hello_testfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServer# 導入from thrift.TMultiplexedProcessor import TMultiplexedProcessorfrom TransmitHandler_server import TransmitHandlerfrom Hello_server import HelloHandler# open serverif __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('Starting python server...') server.serve()
值得注意的是 要想實現單端口 多服務 就必須得引入processor = TMultiplexedProcessor()用來注冊兩個服務類processor.registerProcessor(‘name’, procress對象)name 屬性將會在client 時用到
5運行 runserver.py
如果出現Starting python server… 則運行成功
6 編寫client.py
from test import Transmitfrom test import Hello_testfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocolif __name__ == ’__main__’:# 啟動 服務transport = TSocket.TSocket(’127.0.0.1’, 8000)transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)# 注冊兩個protocol 如果想要實現單端口 多服務 就必須使用 TMultiplexedProtocoltransmit_protocol = TMultiplexedProtocol(protocol, ’transmit’)hello_protocol = TMultiplexedProtocol(protocol, ’hello’)# 注冊兩個客戶端transmit_client = Transmit.Client(transmit_protocol)hello_client = Hello_test.Client(hello_protocol)transport.open() # 打開鏈接# 測試服務1cmd = 1token = ’1111-2222-3333-4444’data = 'kong_ge'msg = transmit_client.invoke(cmd, token, data)print(msg)# 測試服務2name = ’孔格’msg2 = hello_client.hello(name)print(msg2)# 關閉transport.close()
7運行client
觀察結果 實現單端口多服務
總結
核心就是 TMultiplexedProcessor 類 和 TMultiplexedProtocol但是網上關于 thrift python的實例 太少了 導致浪費了很長時間通過這篇文章的學習很快的明白thrift 中的一些概念
到此這篇關于python thrift 實現 單端口多服務的過程的文章就介紹到這了,更多相關python thrift單端口多服務內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: