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

相关推荐
broad-sky2 小时前
Ubuntu上查看USB相机连接的是哪个口,如何查看
linux·数码相机·ubuntu
秋深枫叶红2 小时前
嵌入式第三十七篇——linux系统编程——线程控制
linux·学习·线程·系统编程
shaohui9733 小时前
ARMv7 linux中断路由以及处理
linux·gic·cpsr·armv7
三小尛3 小时前
linux的开发工具vim
linux·运维·vim
陈陈爱java3 小时前
Conda 常用命令行
linux·windows·conda
twdnote3 小时前
dokcer 环境中集成LibreOffice
linux
ChristXlx3 小时前
Linux安装redis(虚拟机适用)
linux·运维·redis
源文雨3 小时前
PVE实现USB硬盘盒在备份前自动上电/结束后自动断电脚本
linux·运维·服务器·备份·perl·pve·usb硬盘盒
ascarl20103 小时前
准确--CentOS 7 配置用户资源限制(nofile / nproc)
linux·运维·centos
秋深枫叶红3 小时前
嵌入式第三十六篇——linux系统编程——线程
linux·运维·服务器·学习