ubuntu thrift 编译和使用

安装必要依赖 sudo apt-get install automake libtool flex bison pkg-config g++ libssl-dev byacc

1. libevent-2.1.12
 1. ./configure --prefix=/home/jdtf/Thrift/Libinstall/libevent-2.1.12
 2. make -j8 && make install

2. boost_1_84_0 
cd boost_1_84_0/
sudo ./bootstrap.sh --prefix=/home/jdtf/Thrift/Libinstall/boost_1_84_0
sudo ./b2
sudo ./b2 install

3. thrift-0.20.0
cd thrift-0.20.0
chmod +x configure
apt-get install python3-pip
sudo ./configure  --prefix=/home/jdtf/Thrift/Libinstall/thrift-0.20.0 \
    --with-boost=/home/jdtf/Thrift/Libinstall/boost_1_84_0          \
    -with-libevent=/home/jdtf/Thrift/Libinstall/libevent-2.1.12 \
    -disable-tests --with-lua=no --with-erlang=no
    
sudo make -j8
sudo make install

新建 thrift文件 calculate.thrift

namespace cpp calculate
service Calculator
{
    i32 add(1:i32 add_num1, 2:i32 add_num2),
    i32 subtract(1:i32 sub_num1, 2:i32 sub_num2)
}

生成server框架

thrift  --gen cpp calculate.thrift  ( -out 参数可指定生成的路径)

服务端程序

#include "Calculator.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

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

using namespace  ::calculate;

class CalculatorHandler : virtual public CalculatorIf {
 public:
  CalculatorHandler() {
    // Your initialization goes here
  }

  int32_t add(const int32_t add_num1, const int32_t add_num2) {
    // Your implementation goes here
    printf("add %d %d\n",add_num1,add_num2);
    return add_num1+add_num2;
  }

  int32_t subtract(const int32_t sub_num1, const int32_t sub_num2) {
    // Your implementation goes here
    printf("subtract %d %d\n",sub_num1,sub_num2);
    return sub_num1-sub_num2;
  }

};

int main(int argc, char **argv) {
  int port = 9090;
  ::std::shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
  ::std::shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
  ::std::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  ::std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  ::std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}

服务端编译

g++ -o ServerTest Server.cpp Calculator.cpp \
-I/home/jdtf/Thrift/Libinstall/thrift-0.20.0/include \
-L/home/jdtf/Thrift/Libinstall/thrift-0.20.0/lib \
-I/home/jdtf/Thrift/Libinstall/boost_1_84_0/include \
-L/home/jdtf/Thrift/Libinstall/boost_1_84_0/lib \
-I/home/jdtf/Thrift/Libinstall/libevent-2.1.12/include \
-L/home/jdtf/Thrift/Libinstall/libevent-2.1.12/lib \
-std=c++11 -lthriftnb -lthrift

//客户端

客户端程序Client.cpp

#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include "Calculator.h"

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

using namespace calculate;

int main() {
    std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    CalculatorClient client(protocol);
    int isConnected=0;
    int a=4,b=3;
    while(1){
            if(!isConnected){
                    try{
                        transport->open();
                        isConnected=1;
                    }catch (TException& tx){
                        isConnected=0;
                        printf("Connect ERROR:%s\n", tx.what());
                        sleep(1);
                        continue;
                    }
           }
           try{
                printf("add:%d \n",client.add(a,b));
           }catch (TException& tx){
                isConnected=0;
                printf("add ERROR:%s\n", tx.what());
                transport->close();
                sleep(1);
                continue;
           }
           
           try{
                printf("subtract:%d \n",client.subtract(a,b));
           }catch (TException& tx){
                isConnected=0;
                printf("subtract ERROR:%s\n", tx.what());
                transport->close();
                sleep(1);
                continue;
           }
            sleep(1);
            a++;
            //transport->close();
            //break;
    }
    transport->close();
    return 0;
}

编译客户端程序

g++ -o ClientTest Client.cpp Calculator.cpp \
-I/home/jdtf/Thrift/Libinstall/thrift-0.20.0/include \
-L/home/jdtf/Thrift/Libinstall/thrift-0.20.0/lib \
-I/home/jdtf/Thrift/Libinstall/boost_1_84_0/include \
-L/home/jdtf/Thrift/Libinstall/boost_1_84_0/lib \
-I/home/jdtf/Thrift/Libinstall/libevent-2.1.12/include \
-L/home/jdtf/Thrift/Libinstall/libevent-2.1.12/lib \
-std=c++11 -lthriftnb -lthrift

其中 Calculator.cpp为生成的接口文件

相关推荐
热爱嵌入式的小许17 分钟前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
韩楚风4 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学4 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO4 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu74 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我5 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、5 小时前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程5 小时前
Linux中环境变量
linux
挥剑决浮云 -5 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
小O_好好学6 小时前
CentOS 7文件系统
linux·运维·centos