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为生成的接口文件

相关推荐
代码游侠21 分钟前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
Gary Studio22 分钟前
rk芯片驱动编写
linux·学习
mango_mangojuice23 分钟前
Linux学习笔记(make/Makefile)1.23
java·linux·前端·笔记·学习
Harvey90333 分钟前
通过 Helm 部署 Nginx 应用的完整标准化步骤
linux·运维·nginx·k8s
A星空1232 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
释怀不想释怀2 小时前
Linux环境变量
linux·运维·服务器
zzzsde2 小时前
【Linux】进程(4):进程优先级&&调度队列
linux·运维·服务器
凡人叶枫2 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
yuanmenghao3 小时前
Linux 性能实战 | 第 7 篇 CPU 核心负载与调度器概念
linux·网络·性能优化·unix
qq_297574673 小时前
Linux 服务器 Java 开发环境搭建保姆级教程
java·linux·服务器