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

上一篇:【剑指offr--C/C++】JZ54 二叉搜索树的第k个节点


下一篇:蓝桥杯嵌入式总结