RPC远程过程调用--Thrift

news/2024/7/7 19:08:17 标签: rpc, 网络协议, 网络, c++

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和

安装(源码编译安装)

  • 下载源码
    https://github.com/apache/thrift

  • 安装编译工具和依赖项

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

附件

  • 文档
    https://thrift.apache.org/tutorial/
  • 源码
    https://github.com/apache/thrift

http://www.niftyadmin.cn/n/5534891.html

相关文章

Python 学习之常用第三方库(五)

Python 常用第三方库 Python 是一门功能强大的编程语言&#xff0c;其生态系统中包含了许多优秀的第三方库&#xff0c;这些库极大地扩展了 Python 的功能。以下是一些常用的 Python 第三方库&#xff1a; 1. NumPy&#xff1a; a. 用于数值计算的库&#xff0c;提供了大量的…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 英文单词联想(100分) - 三语言AC题解(Python/Java/Cpp)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 💻 ACM银牌🥈| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 📎在线评测链接 https://app5938.acapp.acwing.com.cn/contest/2/problem/OD…

二进制方式部署consul单机版

1.consul的下载 mkdir -p /root/consul/data && cd /root/consul wget https://releases.hashicorp.com/consul/1.18.0/consul_1.18.0_linux_amd64.zip unzip consul_1.18.0_linux_amd64.zip mv consul /usr/local/bin/ 2.配置文件 // 配置文件路径&#xff1a; /roo…

Elasticsearch 复合聚合:bucket_by_keys、date_histogram 等

在Elasticsearch中&#xff0c;聚合&#xff08;Aggregation&#xff09;是一种强大的功能&#xff0c;允许我们对数据进行复杂的分析和总结。其中&#xff0c;复合聚合&#xff08;Composite Aggregations&#xff09;是一种特别灵活的聚合方式&#xff0c;它可以将多个聚合类…

MYSQL安装及环境配置

1.数据库下载 1.1 浏览器下载相应版本&#xff0c;如果相应版本不在此页&#xff0c;可点击Archives &#xff0c;然后选择相应版本 https://dev.mysql.com/downloads/mysql/ 1.2 放置指定目录&#xff0c;并将其解压 2.配置数据库环境变量 2.1 使用电脑win键 Q &#xff0c;…

湘潭大学概率论总结

文章目录 前言试卷结构反思第一个填空题某个大题矩估计和最大似然估计算方差最后 前言 自己其实寒假的时候就想学这个了&#xff0c;但是那个时候在acm训练&#xff0c;就没有学&#xff0c;但是3月份退队了&#xff0c;还是没有学&#xff0c;完全就是自己太拖延的原因&#…

红酒与时尚秀场:品味潮流新风尚

在时尚与品味的交汇点上&#xff0c;红酒总是以其不同的方式&#xff0c;为每一次的时尚盛宴增添一抹诱人的色彩。当红酒遇上时尚秀场&#xff0c;不仅是一场视觉的盛宴&#xff0c;更是一次心灵的触动。今天&#xff0c;就让我们一起走进红酒与时尚秀场的世界&#xff0c;感受…