最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年06月08日 10:44:56   作者:bug開發(fā)工程師.  
這篇文章主要介紹了python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

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

  和 http 相比,同屬于應(yīng)用層,走 tcp 協(xié)議。Thrift 優(yōu)勢在于發(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

生成對應(yīng)接口 因?yàn)槲业?服務(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ù)端運(yùn)行代碼 開啟服務(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__":
 # 實(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()
用來注冊兩個(gè)服務(wù)類
processor.registerProcessor(‘name', procress對象)
name 屬性將會(huì)在client 時(shí)用到

5運(yùn)行 runserver.py

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

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__':
	# 啟動(dòng) 服務(wù)
	transport = TSocket.TSocket('127.0.0.1', 8000)
	transport = TTransport.TBufferedTransport(transport)
	protocol = TBinaryProtocol.TBinaryProtocol(transport)

	# 注冊兩個(gè)protocol 如果想要實(shí)現(xiàn)單端口 多服務(wù) 就必須使用 TMultiplexedProtocol
	transmit_protocol = TMultiplexedProtocol(protocol, 'transmit')
	hello_protocol = TMultiplexedProtocol(protocol, 'hello')

	# 注冊兩個(gè)客戶端
	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運(yùn)行client

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

總結(jié)

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

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

相關(guān)文章

  • Python使用MD5加密算法對字符串進(jìn)行加密操作示例

    Python使用MD5加密算法對字符串進(jìn)行加密操作示例

    這篇文章主要介紹了Python使用MD5加密算法對字符串進(jìn)行加密操作,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)md5加密相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • 150行Python代碼實(shí)現(xiàn)帶界面的數(shù)獨(dú)游戲

    150行Python代碼實(shí)現(xiàn)帶界面的數(shù)獨(dú)游戲

    這篇文章主要介紹了150行Python代碼實(shí)現(xiàn)帶界面的數(shù)獨(dú)游戲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python操作Sql Server 2008數(shù)據(jù)庫的方法詳解

    Python操作Sql Server 2008數(shù)據(jù)庫的方法詳解

    這篇文章主要介紹了Python操作Sql Server 2008數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了Python使用pyodbc庫操作Sql Server 2008數(shù)據(jù)庫的連接、執(zhí)行sql語句、關(guān)閉連接等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-05-05
  • python3 selenium自動(dòng)化 下拉框定位的例子

    python3 selenium自動(dòng)化 下拉框定位的例子

    今天小編就為大家分享一篇python3 selenium自動(dòng)化 下拉框定位的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 解決python明明pip安裝成功卻找不到包的問題

    解決python明明pip安裝成功卻找不到包的問題

    今天小編就為大家分享一篇解決python明明pip安裝成功卻找不到包的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python練習(xí)之操作SQLite數(shù)據(jù)庫

    Python練習(xí)之操作SQLite數(shù)據(jù)庫

    這篇文章主要介紹了Python練習(xí)之操作SQLite數(shù)據(jù)庫,主要通過三個(gè)問題如何創(chuàng)建SQLite數(shù)據(jù)庫?如何向SQLite表中插入數(shù)據(jù)?如何查詢SQLite表中的數(shù)據(jù)?展開文章主題詳情,需要的朋友可以參考一下
    2022-06-06
  • Python基本數(shù)據(jù)類型及內(nèi)置方法

    Python基本數(shù)據(jù)類型及內(nèi)置方法

    這篇文章主要介紹了Python基本數(shù)據(jù)類型及內(nèi)置方法,??數(shù)據(jù)類型是用來記錄事物狀態(tài)的,而事物的狀態(tài)是不斷變化的,下文圍繞主題展開相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • python寫的一個(gè)文本編輯器

    python寫的一個(gè)文本編輯器

    這篇文章主要介紹了python寫的一個(gè)文本編輯器,大家參考使用吧
    2014-01-01
  • python 讀取目錄下csv文件并繪制曲線v111的方法

    python 讀取目錄下csv文件并繪制曲線v111的方法

    今天小編就為大家分享一篇python 讀取目錄下csv文件并繪制曲線v111的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python判斷無向圖環(huán)是否存在的示例

    python判斷無向圖環(huán)是否存在的示例

    今天小編就為大家分享一篇python判斷無向圖環(huán)是否存在的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11

最新評論

锦州市| 洮南市| 共和县| 彭水| 平山县| 宝应县| 博乐市| 云安县| 隆化县| 元阳县| 金华市| 东方市| 澳门| 宝鸡市| 秦皇岛市| 潼南县| 海城市| 东丰县| 黄梅县| 清丰县| 南溪县| 正安县| 加查县| 丰镇市| 博爱县| 微山县| 义乌市| 离岛区| 玉树县| 浦江县| 禄丰县| 怀来县| 兴文县| 阳高县| 菏泽市| 徐汇区| 改则县| 安吉县| 乃东县| 麻江县| 大渡口区|