欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)Thrift服務(wù)端的方法

 更新時(shí)間:2021年04月19日 17:02:21   作者:酌三巡  
這篇文章主要介紹了python實(shí)現(xiàn)Thrift服務(wù)端的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下

近期在項(xiàng)目中存在跨編程語(yǔ)言協(xié)作的需求,使用到了Thrift。本文將記錄用python實(shí)現(xiàn)Thrift服務(wù)端的方法。

環(huán)境準(zhǔn)備

  • 根據(jù)自身實(shí)際情況下載對(duì)應(yīng)的Thrift編譯器,比如我在Windows系統(tǒng)上使用的是thrift-0.9.3.exe 。下載地址:http://archive.apache.org/dist/thrift/
  • python安裝thrift庫(kù):pip install thrift

編寫(xiě).thrift文件

.thrift文件定義了Thrift服務(wù)端和Thrift客戶端的通信接口,在該文件中定義的接口需由服務(wù)端實(shí)現(xiàn),并可被客戶端調(diào)用。Thrift編譯器會(huì)調(diào)用.thrift文件生成不同語(yǔ)言的thrift代碼,用于之后實(shí)現(xiàn)thrift服務(wù)端或thrift客戶端。

.thrift文件的編寫(xiě)規(guī)則可參考Thrift白皮書(shū)。下面將以demo.thrift文件舉例

service DemoService{
    string ping(1:string param)
    map<i32,string> get_int_string_mapping_result(1:i32 key, 2:string value)
    bool get_bool_result()
}

生成python對(duì)應(yīng)的thrift代碼

使用以下命令可以生成不同語(yǔ)言的thrift代碼:

thrift --gen <language> <Thrift filename>

 通過(guò)thrift-0.9.3.exe --gen py demo.thrift 命令生成python版本的thrift文件,文件夾為gen-py,如下所示:

編寫(xiě)服務(wù)端

編寫(xiě)服務(wù)端server.py,用于實(shí)現(xiàn)在demo.thrift文件中定義的接口功能。

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import sys

sys.path.append("./gen-py/")
from demo import DemoService
import random


class DemoServer:
    def __init__(self):
        self.log = {}

    def ping(self, param):
        return "echo:" + param

    def get_int_string_mapping_result(self, key, value):
        return {key: value}

    def get_bool_result(self):
        return random.choice([True, False])


if __name__ == '__main__':
    # 創(chuàng)建處理器
    handler = DemoServer()
    processor = DemoService.Processor(handler)

    # 監(jiān)聽(tīng)端口
    transport = TSocket.TServerSocket(host="0.0.0.0", port=9999)

    # 選擇傳輸層
    tfactory = TTransport.TBufferedTransportFactory()

    # 選擇傳輸協(xié)議
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    # 創(chuàng)建服務(wù)端
    server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

    # 設(shè)置連接線程池?cái)?shù)量
    server.setNumThreads(5)

    # 啟動(dòng)服務(wù)
    server.serve()

編寫(xiě)客戶端用于測(cè)試

編寫(xiě)客戶端client.py,用于測(cè)試服務(wù)端功能是否可用。

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
import sys
sys.path.append("./gen-py/")

from demo import DemoService


if __name__ == '__main__':
    transport = TSocket.TSocket('127.0.0.1', 9999)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = DemoService.Client(protocol)

    # 連接服務(wù)端
    transport.open()

    recv = client.ping("test")
    print(recv)

    recv = client.get_int_string_mapping_result(10, "MyThrift")
    print(recv)

    recv = client.get_bool_result()
    print(recv)

    # 斷連服務(wù)端
    transport.close()

編寫(xiě)完成后,整個(gè)項(xiàng)目結(jié)構(gòu)如下圖所示:

測(cè)試服務(wù)端

運(yùn)行服務(wù)端server.py后,運(yùn)行客戶端client.py,打印的內(nèi)容如下:

echo:test
{10: 'MyThrift'}
True

此時(shí)客戶端能夠正常調(diào)用服務(wù)端所提供的接口。(PS:在調(diào)試過(guò)程中,也許需要修改gen-py文件夾中Thrift編譯器生成的python代碼)

以上就是python實(shí)現(xiàn)Thrift服務(wù)端的方法的詳細(xì)內(nèi)容,更多關(guān)于python實(shí)現(xiàn)Thrift服務(wù)端的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論