当前位置: 首页 > news >正文

Thrift作为客户端流程(多路复用)

以下是一个 使用多路复用(TMultiplexedProtocol) 的 Thrift 客户端完整流程和关键函数(以 Java 为例),适用于当服务端使用 TMultiplexedProcessor 注册了多个服务时,客户端可以区分并调用不同的服务。

✅ 客户端整体流程(使用 TMultiplexedProtocol)

1. 定义多个服务(以两个为例)

// example1.thrift
namespace java exampleservice ExampleService1 {string sayHello(1:string name)
}
// example2.thrift
namespace java exampleservice ExampleService2 {i32 add(1:i32 a, 2:i32 b)
}

使用 Thrift 编译器生成 Java 代码:

thrift --gen java example1.thrift
thrift --gen java example2.thrift

2. 服务端使用多路复用(服务端部分简略)

TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();multiplexedProcessor.registerProcessor("ExampleService1", new ExampleService1.Processor<>(new ExampleService1Impl()));
multiplexedProcessor.registerProcessor("ExampleService2", new ExampleService2.Processor<>(new ExampleService2Impl()));TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(multiplexedProcessor));
server.serve();

3. 客户端实现(核心)

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;import example.ExampleService1;
import example.ExampleService2;public class ThriftClient {public static void main(String[] args) {TTransport transport = null;try {// 1. 打开传输层连接transport = new TSocket("localhost", 9090);transport.open();// 2. 创建基础协议TProtocol baseProtocol = new TBinaryProtocol(transport);// 3. 使用多路复用协议(针对不同服务)TMultiplexedProtocol protocol1 = new TMultiplexedProtocol(baseProtocol, "ExampleService1");TMultiplexedProtocol protocol2 = new TMultiplexedProtocol(baseProtocol, "ExampleService2");// 4. 创建客户端 Stub(代理)ExampleService1.Client client1 = new ExampleService1.Client(protocol1);ExampleService2.Client client2 = new ExampleService2.Client(protocol2);// 5. 调用服务方法String greeting = client1.sayHello("Alice");int result = client2.add(5, 7);System.out.println("Service1 response: " + greeting);System.out.println("Service2 response: " + result);} catch (Exception e) {e.printStackTrace();} finally {// 6. 关闭连接if (transport != null) {transport.close();}}}
}

4. C++ 版本:客户端实现

客户端代码(C++)
#include 
#include 
#include 
#include #include "ExampleService1.h"
#include "ExampleService2.h"using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;int main() {try {// 客户端连接std::shared_ptr socket(new TSocket("localhost", 9090));std::shared_ptr transport(new TBufferedTransport(socket));std::shared_ptr protocol(new TBinaryProtocol(transport));transport->open();// 使用多路复用协议TMultiplexedProtocol protocol1(protocol, "ExampleService1");TMultiplexedProtocol protocol2(protocol, "ExampleService2");// 创建客户端ExampleService1Client client1(protocol1);ExampleService2Client client2(protocol2);// 调用服务std::string greeting;client1.sayHello(greeting, "Alice");std::cout << "Response from ExampleService1: " << greeting << std::endl;int result;client2.add(result, 5, 7);std::cout << "Response from ExampleService2: " << result << std::endl;transport->close();} catch (const std::exception& e) {std::cerr << "Error: " << e.what() << std::endl;}return 0;
}

🔍 客户端关键类和函数说明

类 / 函数
作用说明
TSocket(host, port)
创建客户端传输连接(基于 TCP)
transport.open()
打开连接
TBinaryProtocol
使用二进制协议进行编码
TMultiplexedProtocol
多路复用协议,用于标识服务名称
ExampleService1.Client
客户端代理,调用服务方法
client1.sayHello("Alice")
实际调用远程服务端方法
transport.close()
关闭连接

✅ 小提示

  • 多路复用的关键是:
    • 服务端使用 TMultiplexedProcessor 注册服务(带名字);
    • 客户端用 TMultiplexedProtocol 创建不同服务的协议实例(带同样的名字)。

相关文章:

  • 设计模式在上位机项目的实战
  • 基于SpringBoot+Uniapp的活动中心预约小程序(协同过滤算法、腾讯地图、二维码识别)
  • 微服务拆分——nacos/Feign
  • python中学物理实验模拟:凸透镜成像和凹透镜成像
  • 力扣1477. 找两个和为目标值且不重叠的子数组
  • IEEE5节点系统潮流仿真模型(simulink+matlab全功能模型)
  • MySQL误删数据急救指南:基于Binlog日志的实战恢复详解
  • Vue3 + TypeScript + xlsx 导入excel文件追踪数据流转详细记录(从原文件到目标数据)
  • 编程基础:调用访问
  • pyqt事件过滤器eventFilter
  • 计算机网络学习笔记:应用层概述、动态主机配置协议DHCP
  • ProtoBuf:proto3 语法详解
  • 软件工程期末试卷填空题版带答案(共40道)
  • 华为云 Flexus+DeepSeek 实战:华为云Dify 平台 CCE 高可用集群部署与大模型知识库构建指南
  • C++链表的虚拟头节点
  • 【unity游戏开发——热更新】YooAsset简化资源加载、打包、更新等流程
  • 【设计模式】6.原型模式
  • 获取 Git 仓库
  • 第三章 线性回归与感知机
  • 面试题-ts中的typeof
  • 安徽网站建设方案优化/附子seo
  • 娄底网站建设79ld/搜索量查询百度指数
  • 做设计在哪个网站上找高清图片/重庆seo技术教程博客
  • b/s网站开发/nba最新排名东西部
  • 做批发上哪个网站好/seo外链专员
  • 河北网络公司网站建设/河北seo技术