Thrift对多接口服务的支持

2024-05-05 00:18
文章标签 服务 接口 支持 thrift

本文主要是介绍Thrift对多接口服务的支持,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

my_thrift.thrift

struct Message {1: string msg
}service MessageService {Message getMessage(1:Message msg)
}struct User {1: string name
}service UserService {User getUser(1:User user)
}
thrift --gen py test.thrift├── gen-py
│   ├── __init__.py
│   └── my_thrift
│       ├── constants.py
│       ├── __init__.py
│       ├── MessageService.py
│       ├── MessageService-remote
│       ├── ttypes.py
│       ├── UserService.py
│       └── UserService-remote
├── __init__.py
├── my_thrift.thrift

服务端

server.py

#!/usr/bin/env python
# # -*- coding: utf-8 -*-import sys
from thrift.TMultiplexedProcessor import TMultiplexedProcessor
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TTransport, TSocketsys.path.append('./gen-py')from my_thrift import MessageService
from my_thrift import UserService
from my_thrift.ttypes import *class MessageHandler:def getMessage(self, msg):return msgclass UserHandler:def getUser(self, user):return usermsg_processor = MessageService.Processor(MessageHandler()) #定义msg处理器
use_processor = UserService.Processor(UserHandler()) #定义user处理器tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()processor = TMultiplexedProcessor() #使用TMultiplexedProcessor接收多个处理
processor.registerProcessor("msg", msg_processor) #注册msg服务
processor.registerProcessor("user", use_processor) #注册user服务transport = TSocket.TServerSocket()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)server.serve() #开始监听请求

客户端

client.py

#!/usr/bin/env python
# # -*- coding: utf-8 -*-import sys
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocolsys.path.append('./gen-py')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from my_thrift import MessageService
from my_thrift import UserService
from my_thrift.ttypes import *transport = TSocket.TSocket()transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)msg_protocol = TMultiplexedProtocol(protocol, "msg") #如果服务端使用TMultiplexedProcessor接收处理,客户端必须用TMultiplexedProtocol并且指定serviceName和服务端的一致
user_protocol = TMultiplexedProtocol(protocol, "user")msg_client = MessageService.Client(msg_protocol)#msg客户端
user_client = UserService.Client(user_protocol)#user客户端
transport.open()#打开链接print msg_client.getMessage(Message(msg="111"))
print user_client.getUser(User(name="111"))transport.close()

这篇关于Thrift对多接口服务的支持的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/960414

相关文章

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

MySQL中C接口的实现

《MySQL中C接口的实现》本节内容介绍使用C/C++访问数据库,包括对数据库的增删查改操作,主要是学习一些接口的调用,具有一定的参考价值,感兴趣的可以了解一下... 目录准备mysql库使用mysql库编译文件官方API文档对象的创建和关闭链接数据库下达sql指令select语句前言:本节内容介绍使用C/

Java服务实现开启Debug远程调试

《Java服务实现开启Debug远程调试》文章介绍如何通过JVM参数开启Java服务远程调试,便于在线上排查问题,在IDEA中配置客户端连接,实现无需频繁部署的调试,提升效率... 目录一、背景二、相关图示说明三、具体操作步骤1、服务端配置2、客户端配置总结一、背景日常项目中,通常我们的代码都是部署到远程

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

sysmain服务可以禁用吗? 电脑sysmain服务关闭后的影响与操作指南

《sysmain服务可以禁用吗?电脑sysmain服务关闭后的影响与操作指南》在Windows系统中,SysMain服务(原名Superfetch)作为一个旨在提升系统性能的关键组件,一直备受用户关... 在使用 Windows 系统时,有时候真有点像在「开盲盒」。全新安装系统后的「默认设置」,往往并不尽编

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁

解决若依微服务框架启动报错的问题

《解决若依微服务框架启动报错的问题》Invalidboundstatement错误通常由MyBatis映射文件未正确加载或Nacos配置未读取导致,需检查XML的namespace与方法ID是否匹配,... 目录ruoyi-system模块报错报错详情nacos文件目录总结ruoyi-systnGLNYpe