python thrift 實現(xiàn) 單端口多服務的過程
Thrift 是一種接口描述語言和二進制通信協(xié)議。以前也沒接觸過,最近有個項目需要建立自動化測試,這個項目之間的微服務都是通過 Thrift 進行通信的,然后寫自動化腳本之前研究了一下。
需要定義一個xxx.thrift的文件, 來生成各種語言的代碼,生成之后我們的服務提供者和消費者,都需要把代碼引入,服務端把代碼實現(xiàn),消費者直接使用API的存根,直接調(diào)用。
和 http 相比,同屬于應用層,走 tcp 協(xié)議。Thrift 優(yōu)勢在于發(fā)送同樣的數(shù)據(jù),request包 和 response包 要比 http 小很多,在整體性能上要優(yōu)于 http 。
前言
學習了兩天thrift 一直想實現(xiàn)單端口多服務 但是苦于網(wǎng)上的 thrift 實在太少 而且大部分都是java實現(xiàn)的 最后 改了一個java的 實現(xiàn)了 單端口多服務
實現(xiàn)過程
1 創(chuàng)建 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 TransmitHandler
class 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 HelloHandler class HelloHandler: def hello(self, name): return 'hello'+name
4 編寫服務端運行代碼 開啟服務端
from test import Transmit
from test import Hello_test
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
# 導入
from thrift.TMultiplexedProcessor import TMultiplexedProcessor
from TransmitHandler_server import TransmitHandler
from Hello_server import HelloHandler
# open server
if __name__ == "__main__":
# 實現(xiàn) 單端口 多服務 的方法
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()
值得注意的是 要想實現(xiàn)單端口 多服務 就必須得
引入processor = TMultiplexedProcessor()
用來注冊兩個服務類
processor.registerProcessor(‘name', procress對象)
name 屬性將會在client 時用到
5運行 runserver.py
如果出現(xiàn)Starting python server… 則運行成功
6 編寫client.py
from test import Transmit
from test import Hello_test
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
if __name__ == '__main__':
# 啟動 服務
transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# 注冊兩個protocol 如果想要實現(xiàn)單端口 多服務 就必須使用 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)
# 關(guān)閉
transport.close()
7運行client
觀察結(jié)果 實現(xiàn)單端口多服務
總結(jié)
核心就是 TMultiplexedProcessor 類 和 TMultiplexedProtocol
但是網(wǎng)上關(guān)于 thrift python的實例 太少了 導致浪費了很長時間
通過這篇文章的學習很快的明白thrift 中的一些概念
到此這篇關(guān)于python thrift 實現(xiàn) 單端口多服務的過程的文章就介紹到這了,更多相關(guān)python thrift單端口多服務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python thrift搭建服務端和客戶端測試程序
- python3.7通過thrift操作hbase的示例代碼
- python使用thrift教程的方法示例
- python利用thrift服務讀取hbase數(shù)據(jù)的方法
- python 如何用urllib與服務端交互(發(fā)送和接收數(shù)據(jù))
- Python連接Java Socket服務端的實現(xiàn)方法
- python 實現(xiàn)客戶端與服務端的通信
- python網(wǎng)絡編程socket實現(xiàn)服務端、客戶端操作詳解
- Python Websocket服務端通信的使用示例
- python實現(xiàn)Thrift服務端的方法
相關(guān)文章
python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法詳解
這篇文章主要介紹了python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
Python處理不同接口間參數(shù)依賴的方法總結(jié)
這篇文章主要為大家詳細介紹了如何使用Python編寫接口自動化測試,以有效地處理不同接口之間的參數(shù)依賴,并提供豐富的示例代碼,希望對大家有所幫助2024-01-01
python networkx 根據(jù)圖的權(quán)重畫圖實現(xiàn)
這篇文章主要介紹了python networkx 根據(jù)圖的權(quán)重畫圖實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Python的Tornado框架實現(xiàn)圖片上傳及圖片大小修改功能
Tornado是一個異步的Python Web開發(fā)框架,同時也是一個優(yōu)秀的異步服務器開發(fā)庫,這里我們將來講解一下Python的Tornado框架實現(xiàn)圖片上傳及圖片大小修改功能方面的一些重點:2016-06-06
Python簡單實現(xiàn)查找一個字符串中最長不重復子串的方法
這篇文章主要介紹了Python簡單實現(xiàn)查找一個字符串中最長不重復子串的方法,涉及Python針對字符串的簡單遍歷、運算等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Python使用Selenium模塊實現(xiàn)模擬瀏覽器抓取淘寶商品美食信息功能示例
這篇文章主要介紹了Python使用Selenium模塊實現(xiàn)模擬瀏覽器抓取淘寶商品美食信息功能,涉及Python基于re模塊的正則匹配及selenium模塊的頁面抓取等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

