RPC远程过程调用–Thrift

简介

  • Thrift是一个由Facebook开发的轻量级、跨语言的远程服务调用框架,后进入Apache开源项目。支持通过自身接口定义语言IDL定义RPC接口和数据类型,然后通过编译器生成不同语言代码,用于构建抽象易用、可互操作的RPC客户端和服务器。
  • Thrift软件栈分层从下向上分别为:传输层(Transport Layer)、协议层(Protocol Layer)、处理层(Processor Layer)和服务层(Server Layer)。
  • 具有开发速度快、易维护、高效、跨语言(C++、 Java、Python、PHP、Ruby、C#、、JavaScript、Node.js、等)优点
  • 应用广泛:hadFacebook和

安装(源码编译安装)

sudo apt install build-essential automake bison flex libtool pkg-config
  • 解压后配置和编译
./bootstrap.sh
./configure
make
  • 安装
sudo make instal
  • 配置环境变量和映射库
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
  • 更新库缓存
    sudo ldconfig

  • 查看安装结果

thrift --version
Thrift version 0.21.0

远程过程调用服务器客户端Demo

  • 编写Thrift IDL 文件<calculator.thrift>定义服务或数据类型
namespace cpp tutorial

service Calculator {
    i32 add(1:i32 num1, 2:i32 num2)
}
  • 使用thrift编译器生成C++代码
thrift --gen cpp test.thrift

在这里插入图片描述- 编写服务器和客户端应用代码

/* Server.cpp */
#include <iostream>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TBufferTransports.h>
#include "gen-cpp/Calculator.h" // 根据实际生成的目录结构包含正确的头文件

using namespace apache::thrift;
using namespace apache::thrift::server;
using namespace apache::thrift::transport;
using namespace apache::thrift::protocol;
using namespace tutorial;

class CalculatorHandler : public CalculatorIf
{
public:
    CalculatorHandler() {}

    int32_t add(const int32_t num1, const int32_t num2) override
    {
        std::cout << "Adding " << num1 << " and " << num2 << std::endl;
        return num1 + num2;
    }
};

int main()
{
    int port = 9090;

    std::shared_ptr<CalculatorHandler> handler = std::make_shared<CalculatorHandler>();
    std::shared_ptr<TProcessor> processor = std::make_shared<CalculatorProcessor>(handler);
    std::shared_ptr<TServerTransport> serverTransport = std::make_shared<TServerSocket>(port);
    std::shared_ptr<TTransportFactory> transportFactory = std::make_shared<TBufferedTransportFactory>();
    std::shared_ptr<TProtocolFactory> protocolFactory = std::make_shared<TBinaryProtocolFactory>();

    TSimpleServer server(
        processor,
        serverTransport,
        transportFactory,
        protocolFactory);

    std::cout << "Starting the server..." << std::endl;
    server.serve();
    std::cout << "Server stopped" << std::endl;

    return 0;
}

/* Client.cpp */
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include "gen-cpp/Calculator.h" // 根据实际生成的目录结构包含正确的头文件

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace tutorial;

int main()
{
    // 连接到 Thrift 服务器
    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);

    CalculatorClient client(protocol);

    try
    {
        // 打开连接
        transport->open();

        // 调用远程方法
        int num1 = 10;
        int num2 = 5;
        int result = client.add(num1, num2);

        std::cout << "Result of adding " << num1 << " and " << num2 << " is: " << result << std::endl;

        // 关闭连接
        transport->close();
    }
    catch (const TException &tx)
    {
        std::cerr << "Thrift exception: " << tx.what() << std::endl;
    }

    return 0;
}

  • 编译
g++ -o Server Server.cpp gen-cpp/Calculator.cpp -I/usr/local/include/thrift -lthrift
g++ -o Client Client.cpp gen-cpp/Calculator.cpp -I/usr/local/include/thrift -lthrift
  • 执行
./Server 
Starting the server...

Adding 10 and 5

./Client 
Result of adding 10 and 5 is: 15

附件

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部