溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中thrift如何實現(xiàn)單端口多服務(wù)

發(fā)布時間:2020-07-17 16:33:18 來源:億速云 閱讀:167 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了python中thrift如何實現(xiàn)單端口多服務(wù),內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

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

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

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

前言

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

實現(xiàn)過程

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

生成對應(yīng)接口 因為我的 服務(wù)端和 用戶端 都是用 python寫的 所以 只需要 生成python 接口即可

3 編寫 server.py

# 服務(wù)類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不匹配'
# 服務(wù)類2 HelloHandler
class HelloHandler:
	def hello(self, name):
		return 'hello'+name

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

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
# 導(dǎo)入
from thrift.TMultiplexedProcessor import TMultiplexedProcessor
from TransmitHandler_server import TransmitHandler
from Hello_server import HelloHandler


# open server
if __name__ == "__main__":
 # 實現(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()

值得注意的是 要想實現(xiàn)單端口 多服務(wù) 就必須得
引入processor = TMultiplexedProcessor()
用來注冊兩個服務(wù)類
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__':
	# 啟動 服務(wù)
	transport = TSocket.TSocket('127.0.0.1', 8000)
	transport = TTransport.TBufferedTransport(transport)
	protocol = TBinaryProtocol.TBinaryProtocol(transport)

	# 注冊兩個protocol 如果想要實現(xiàn)單端口 多服務(wù) 就必須使用 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() # 打開鏈接
	
	# 測試服務(wù)1
	cmd = 1
	token = '1111-2222-3333-4444'
	data = "kong_ge"
	msg = transmit_client.invoke(cmd, token, data)
	print(msg)
	
	# 測試服務(wù)2
	name = '孔格'
	msg2 = hello_client.hello(name)
	print(msg2)
	
	# 關(guān)閉
	transport.close()

7運行client

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

以上就是關(guān)于python中thrift如何實現(xiàn)單端口多服務(wù)的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI