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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值