当前位置: 移动技术网 > IT编程>脚本编程>Python > python thrift 实现 单端口多服务的过程

python thrift 实现 单端口多服务的过程

2020年06月14日  | 移动技术网IT编程  | 我要评论

贤妻电视剧全集剧情,究极美味料理,2013春晚节目表

thrift 是一种接口描述语言和二进制通信协议。以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 thrift 进行通信的,然后写自动化脚本之前研究了一下。

  需要定义一个xxx.thrift的文件, 来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用api的存根,直接调用。

  和 http 相比,同属于应用层,走 tcp 协议。thrift 优势在于发送同样的数据,request包 和 response包 要比 http 小很多,在整体性能上要优于 http 。

前言

学习了两天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 中的一些概念

到此这篇关于python thrift 实现 单端口多服务的过程的文章就介绍到这了,更多相关python thrift单端口多服务内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网