python thrift 實現 單端口多服務

前言

學習了兩天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  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__":
    # 實現 單端口 多服務 的方法

    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 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 如果想要實現單端口 多服務 就必須使用 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
但是網上關於 thrift python的實例 太少了 導致浪費了很長時間
今天最大的收穫就是明白了thrift 中的一些概念
初學 thrift 博客中若有錯誤 希望批評指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章