C++怎么使用Thrift进行RPC通信_C++跨语言服务框架实践
发布时间:2025-11-21 19:36
发布者:网络
浏览次数:Thrift通过IDL定义服务接口,生成C++代码实现RPC通信。先定义PersonService接口并生成代码,再在服务端继承接口类实现方法,使用TSimpleServer启动服务;客户端通过TBinaryProtocol连接服务端并调用远程方法。编译时链接libthrift库,先运行服务端再启动客户端完成测试。

Thrift 是 Apache 开发的跨语言服务框架,支持多种语言(如 C++、J*a、Python 等)之间的高效 RPC 通信。在 C++ 中使用 Thrift,可以实现高性能的服务端与客户端交互。下面介绍如何在 C++ 项目中使用 Thrift 实现 RPC 通信。
定义接口 IDL 文件
Thrift 的核心是通过 .thrift 文件定义数据结构和服务接口。以下是一个简单的例子:
namespace cpp example
struct Person {
1: i32 id,
2: string name,
3: i16 age
}
service PersonService {
Person getPerson(1: i32 id),
bool s*ePerson(1: Person person)
}
这个文件定义了一个 Person 结构和一个名为 PersonService 的服务接口。使用 Thrift 编译器生成 C++ 代码:
thrift --gen cpp person.thrift
生成的代码位于 gen-cpp 目录下,包括:Person_types.h/cpp、PersonService.h/cpp。
实现服务端逻辑
创建服务端时,需继承自生成的 Service 接口类,并实现对应方法:
#include "PersonService.h"
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
class PersonServiceHandler : public PersonServiceIf {
public:
void getPerson(Person& _return, const int32_t id) override {
_return.id = id;
_return.name = "Alice";
_return.age = 25;
}
bool s*ePerson(const Person& person) override {
printf("S*e person: %s (id=%d)\n", person.name.c_str(), person.id);
return true;
}
};
启动服务端:
CA.LA
第一款时尚产品在线设计平台,服装设计系统
94
查看详情
int main() {
auto handler = std::make_shared<PersonServiceHandler>();
auto processor = std::make_shared<PersonServiceProcessor>(handler);
auto serverTransport = std::make_shared<TServerSocket>(9090);
auto transportFactory = std::make_shared<TBufferedTransp
ortFactory>();
auto protocolFactory = std::make_shared<TBinaryProtocolFactory>();
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
printf("Starting server on port 9090...\n");
server.serve();
return 0;
}
编写客户端调用代码
客户端通过连接服务端并使用代理对象发起远程调用:
#include "PersonService.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
int main() {
std::shared_ptr<TTransport> socket = std::make_shared<TSocket>("localhost", 9090);
std::shared_ptr<TTransport> transport = std::make_shared<TBufferedTransport>(socket);
std::shared_ptr<TProtocol> protocol = std::make_shared<TBinaryProtocol>(transport);
PersonServiceClient client(protocol);
try {
transport->open();
Person person;
client.getPerson(person, 1);
printf("Got person: %s, age %d\n", person.name.c_str(), person.age);
person.id = 2;
person.name = "Bob";
person.age = 30;
bool s*ed = client.s*ePerson(person);
printf("S*e result: %s\n", s*ed ? "true" : "false");
transport->close();
} catch (TException& tx) {
printf("ERROR: %s\n", tx.what());
}
return 0;
}
编译与依赖配置
确保系统已安装 Thrift 开发库。Ubuntu 上可通过:
sudo apt-get install libthrift-dev
编译服务端或客户端时链接 Thrift 库:
g++ -o server server.cpp gen-cpp/PersonService.cpp gen-cpp/Person_types.cpp -lthrift g++ -o client client.cpp gen-cpp/PersonService.cpp gen-cpp/Person_types.cpp -lthrift
运行顺序:先启动 server,再运行 client 测试通信。
基本上就这些。C++ 使用 Thrift 实现 RPC 不复杂,关键是掌握 IDL 定义、代码生成和服务/客户端模板结构。适合构建跨语言微服务或内部高性能通信模块。
以上就是C++怎么使用Thrift进行RPC通信_C++跨语言服务框架实践的详细内容,更多请关注其它相关文章!
# python
# java
# go
# apache
# ubuntu
# ai
# c++
# red
# 服务端
# 客户端
# 数据结构
# 高性能
# 是一个
# 和服务
# 相关文章
# 中文网
# 解决问题
# 可以实现
# 兖州市seo培训
# 哈尔滨怎样优化网站
# 网站推广微信公众号
# 许昌精准营销推广招商
# 恩施网站seo推广
# seo优化如何快速提高网站权重
# 网站建设兼职北京
# 数码推广网站哪个好做些
# 广告seo推广哪个好
# 西夏区智能网站优化排名





ortFactory>();
auto protocolFactory = std::make_shared<TBinaryProtocolFactory>();
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
printf("Starting server on port 9090...\n");
server.serve();
return 0;
}