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

相关文章

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

定价129元!支持双频 Wi-Fi 5的华为AX1路由器发布

《定价129元!支持双频Wi-Fi5的华为AX1路由器发布》华为上周推出了其最新的入门级Wi-Fi5路由器——华为路由AX1,建议零售价129元,这款路由器配置如何?详细请看下文介... 华为 Wi-Fi 5 路由 AX1 已正式开售,新品支持双频 1200 兆、配有四个千兆网口、提供可视化智能诊断功能,建

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学