Thrift通过IDL定义服务接口,生成C++代码实现RPC通信。先定义PersonService接口并生成代码,再在服务端继承接口类实现方法,使用TSimpleServer启动服务;客户端通过TBinaryProtocol连接服务端并调用远程方法。编译时链接libthrift库,先运行服务端再启动客户端完成测试。

Thrift 是 Apache 开发的跨语言服务框架,支持多种语言(如 C++、Java、Python 等)之间的高效 RPC 通信。在 C++ 中使用 Thrift,可以实现高性能的服务端与客户端交互。下面介绍如何在 C++ 项目中使用 Thrift 实现 RPC 通信。
Thrift 的核心是通过 .thrift 文件定义数据结构和服务接口。以下是一个简单的例子:
namespace cpp example
struct Person {
1: i32 id,
2: string name,
3: i16 age
}
service PersonService {
Person getPerson(1: i32 id),
bool savePerson(1: Person person)
}
这个文件定义了一个 Person 结构和一个名为 PersonService 的服务接口。使用 Thrift 编译器生成 C++ 代码:
thrift --gen cpp person.thrift
生成的代码位于 gen-cpp 目录下,包括:Person_types.h/cpp、PersonService.h/cpp。
立即学习“C++免费学习笔记(深入)”;
创建服务端时,需继承自生成的 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 savePerson(const Person& person) override {
printf("Save person: %s (id=%d)\n", person.name.c_str(), person.id);
return true;
}
};
启动服务端:
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<TBufferedTransportFactory>();
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 saved = client.savePerson(person);
printf("Save result: %s\n", saved ? "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++跨语言服务框架实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号