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

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 中的一些概念
初学 thrift 博客中若有错误 希望批评指正

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

(0)

相关推荐

  • python thrift搭建服务端和客户端测试程序

    本文生动简洁介绍了如何通过python搭建一个服务端和客户端的简单测试程序. 一.简介 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的.高效的服务. 二.安装 1.下载地址 http://www

  • python利用thrift服务读取hbase数据的方法

    因工作需要用python通过hbase的thrift服务读取Hbase表数据,发现公司的测试环境还不支持,于是自己动手准备环境,在此我将在安装步骤尽可能描述清楚,旨在给第一次动手安装的朋友,此过程亲测成功! 安装过程如下: 1.首先确保hbase安装测试成功,再者确认下hbase的thrift服务是否启动,注意目前的Hbase(本文基于版本0.98.17)有两套thrift接口thrift和thrift2,本文使用thrift,启动命令:hbase thrift -p 9090 start,确保

  • python实现超简单端口转发的方法

    本文实例讲述了python实现超简单端口转发的方法.分享给大家供大家参考.具体如下: 代码非常简单,实现了简单的端口数据转发功能,用于真实环境还需要再修改一下. 复制代码 代码如下: #tcp server import socket host = '127.0.0.1'          #Local Server IP host2 = '127.0.0.1'   #Real Server IP port = 6001 #Local Server Port port2 = 7001 #Real

  • python使用thrift教程的方法示例

    一.前言: Thrift 是一种接口描述语言和二进制通信协议.以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下. 需要定义一个xxx.thrift的文件, 来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用API的存根,直接调用. 和 http 相比,同属于应用层,走 tcp 协议.Thrift 优势在于发送同样的数据,request包 和 respons

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

    Thrift 是一种接口描述语言和二进制通信协议.以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下. 需要定义一个xxx.thrift的文件, 来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用API的存根,直接调用. 和 http 相比,同属于应用层,走 tcp 协议.Thrift 优势在于发送同样的数据,request包 和 response包 要比

  • mysql5.7单实例自启动服务配置过程

    1.mysql版本 [root@clq system]# mysql -v Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 49 Server version: 5.7.33 MySQL Community Server (GPL) 2.配置mysqld.service文件 [Unit] Description=mysql server daemon After=networ

  • Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】

    本文实例讲述了Python 网络编程之TCP客户端/服务端功能.分享给大家供大家参考,具体如下: demo.py(TCP客户端): import socket def main(): # 1. 创建tcp的套接字 tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 2. 链接服务器 # tcp_socket.connect(("192.168.33.11", 7890)) server_ip = input(

  • Python 通过监听端口实现唯一脚本运行方式

    我就废话不多说了,大家还是直接看代码吧 try: s = socket.socket() s.bind(('127.0.0.1', port)) except: log.info(' * already has an instance, so exit.') exit(0) 补充知识:Python:一个简单的tornado程序:监听服务器端口,访问时给浏览器返回一个字符串 一个简单的tornado程序 tornado是python里的一个模块,它是一种web服务器软件的开源版本 它作为web服务

  • python进程结束后端口占用问题解析

    目录 socket分配 例子 解决方案 其他 socket分配 一个服务端进程向操作系统申请一个 scoket 来监听,但是当进程退出后,还未关闭的连接不会立即消失,而是会留给操作系统处理.操作系统会尝试关闭这个连接.但是如果关闭时出现问题,这个连接就会一直处于 TIME_WAIT 或其他非正常状态,而这是相应的端口还处于占用状态,如果这个时候再重新启动这个服务端程序,就会出现地址被占用的情况 例子 测试代码: import socket s = socket.socket() s.bind((

  • python利用socket实现客户端和服务端之间进行通信

    目录 一.socket socket通信的条件:IP和端口 形象比喻 二.客户端实现过程 三.服务器实现过程 四.演示 五.实现持续通信过程 前言: 今天教大家通过Python进行Socket网络编程(做一个聊天程序),可以实现在不同的主机(电脑)之间进行通话. 具体效果如何,接着往下看: 可以看到客户端(上方)向服务器端(下方)发送了内容,服务器端进行了回复 [备注:客户端是我的本机,服务器是另一条主机(阿里云服务器)] 两台主机的目的:验证两台主机可以相互通信 一.socket 先简单给大家

  • python检测远程udp端口是否打开的方法

    本文实例讲述了python检测远程udp端口是否打开的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: import socket import threading import time import struct import Queue queue = Queue.Queue() def udp_sender(ip,port):     try:         ADDR = (ip,port)         sock_udp = socket.socket(sock

  • python实现的多线程端口扫描功能示例

    本文实例讲述了python实现的多线程端口扫描功能.分享给大家供大家参考,具体如下: 下面的程序给出了对给定的ip主机进行多线程扫描的Python代码 #!/usr/bin/env python #encoding: utf-8 import socket, sys, thread, time openPortNum = 0 socket.setdefaulttimeout(3) def usage(): print '''''Usage: Scan the port of one IP: py

  • Python 自动化表单提交实例代码

    今天以一个表单的自动提交,来进一步学习selenium的用法 练习目标 0)运用selenium启动firefox并载入指定页面(这部分可查看本人文章 http://www.cnblogs.com/liu2008hz/p/6958126.html) 1)页面元素查找(多种查找方式:find_element_*) 2)内容填充(send_keys) 3)iframe与父页面切换(switch_to_frame是切换到iframe,switch_to_default_content是切换到主页面)

随机推荐