色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過程

瀏覽:12日期:2022-07-22 10:36:57

Thrift 是一種接口描述語(yǔ)言和二進(jìn)制通信協(xié)議。以前也沒接觸過,最近有個(gè)項(xiàng)目需要建立自動(dòng)化測(cè)試,這個(gè)項(xiàng)目之間的微服務(wù)都是通過 Thrift 進(jìn)行通信的,然后寫自動(dòng)化腳本之前研究了一下。

需要定義一個(gè)xxx.thrift的文件, 來生成各種語(yǔ)言的代碼,生成之后我們的服務(wù)提供者和消費(fèi)者,都需要把代碼引入,服務(wù)端把代碼實(shí)現(xiàn),消費(fèi)者直接使用API的存根,直接調(diào)用。

和 http 相比,同屬于應(yīng)用層,走 tcp 協(xié)議。Thrift 優(yōu)勢(shì)在于發(fā)送同樣的數(shù)據(jù),request包 和 response包 要比 http 小很多,在整體性能上要優(yōu)于 http 。

前言

學(xué)習(xí)了兩天thrift 一直想實(shí)現(xiàn)單端口多服務(wù) 但是苦于網(wǎng)上的 thrift 實(shí)在太少 而且大部分都是java實(shí)現(xiàn)的 最后 改了一個(gè)java的 實(shí)現(xiàn)了 單端口多服務(wù)

實(shí)現(xiàn)過程

1 創(chuàng)建 thrift 文件 添加兩個(gè)服務(wù) 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 運(yùn)行 thrift.exe -out gen-py --gen py test.thrift

生成對(duì)應(yīng)接口 因?yàn)槲业?服務(wù)端和 用戶端 都是用 python寫的 所以 只需要 生成python 接口即可

3 編寫 server.py

# 服務(wù)類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不匹配’

# 服務(wù)類2 HelloHandlerclass HelloHandler:def hello(self, name):return ’hello’+name

4 編寫服務(wù)端運(yùn)行代碼 開啟服務(wù)端

from test import Transmitfrom test import Hello_testfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServer# 導(dǎo)入from thrift.TMultiplexedProcessor import TMultiplexedProcessorfrom TransmitHandler_server import TransmitHandlerfrom Hello_server import HelloHandler# open serverif __name__ == '__main__': # 實(shí)現(xiàn) 單端口 多服務(wù) 的方法 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()

值得注意的是 要想實(shí)現(xiàn)單端口 多服務(wù) 就必須得引入processor = TMultiplexedProcessor()用來注冊(cè)兩個(gè)服務(wù)類processor.registerProcessor(‘name’, procress對(duì)象)name 屬性將會(huì)在client 時(shí)用到

5運(yùn)行 runserver.py

如果出現(xiàn)Starting python server… 則運(yùn)行成功

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__’:# 啟動(dòng) 服務(wù)transport = TSocket.TSocket(’127.0.0.1’, 8000)transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)# 注冊(cè)兩個(gè)protocol 如果想要實(shí)現(xiàn)單端口 多服務(wù) 就必須使用 TMultiplexedProtocoltransmit_protocol = TMultiplexedProtocol(protocol, ’transmit’)hello_protocol = TMultiplexedProtocol(protocol, ’hello’)# 注冊(cè)兩個(gè)客戶端transmit_client = Transmit.Client(transmit_protocol)hello_client = Hello_test.Client(hello_protocol)transport.open() # 打開鏈接# 測(cè)試服務(wù)1cmd = 1token = ’1111-2222-3333-4444’data = 'kong_ge'msg = transmit_client.invoke(cmd, token, data)print(msg)# 測(cè)試服務(wù)2name = ’孔格’msg2 = hello_client.hello(name)print(msg2)# 關(guān)閉transport.close()

7運(yùn)行client

觀察結(jié)果 實(shí)現(xiàn)單端口多服務(wù)

總結(jié)

核心就是 TMultiplexedProcessor 類 和 TMultiplexedProtocol但是網(wǎng)上關(guān)于 thrift python的實(shí)例 太少了 導(dǎo)致浪費(fèi)了很長(zhǎng)時(shí)間通過這篇文章的學(xué)習(xí)很快的明白thrift 中的一些概念

到此這篇關(guān)于python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過程的文章就介紹到這了,更多相關(guān)python thrift單端口多服務(wù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品一区二区三区www | 亚洲综合天堂网 | 午夜私人影院免费体验区 | 中文字幕高清在线天堂网 | 福利姬在线精品观看 | 色偷偷亚洲女人天堂观看欧 | 日韩久久一区二区三区 | 日韩国产三级 | 日本三级香港三级人妇r | 在线观看va | 日本aaa视频 | 亚洲美女黄色片 | 精品一区二区三区波多野结衣 | 免费观看欧美一区二区三区 | 美国亚洲成年毛片 | 欧美精品亚洲精品日韩 | 精品国产一区二区三区久久影院 | 爆操巨乳美女 | 亚洲第一网站免费视频 | 日韩专区亚洲精品欧美专区 | 久久福利青草精品资源 | 国产成视频 | 99久热在线精品视频观看 | 美女又爽又黄视频 | 国产亚洲精品美女一区二区 | 又黄又爽视频好爽视频 | 国产亚洲精品午夜高清影院 | 国产乱淫a∨片免费视频 | 国产网友自拍 | 国产专区第一页 | 男女午夜24式免费视频 | 91精品国产手机在线版 | 亚洲人成网7777777国产 | 久久国产精品成人免费 | 国产在线精品一区二区 | 真实的国产乱xxxx | 99re66热这里只有精品免费观看 | 成人在线免费视频播放 | 一区二区不卡久久精品 | 国产精品高清一区二区 | 一区二区三区欧美日韩国产 |