• thrift


    thrift

    官网:

    https://thrift.apache.org

    Linux下下载

    wget https://mirror.bit.edu.cn/apache/thrift/0.13.0/thrift-0.13.0.tar.gz 
    

    安装相关的依赖包

    sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config
    

    编译

    tar -zxvf thrift-0.13.0.tar.gz
    cd thrift-0.13.0/
    ./configure --with-cpp --with-boost --without-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go 
    sudo make
    sudo make install
    

    验证thrift是否装好

    thrift -version
    

    thrift-cpp-example

    参考github示例

    https://github.com/crazyStar00/thrift-cpp-example.git
    

    客户端代码修改

    #include "serv.h"
    #include </usr/local/include/thrift/transport/TSocket.h>
    #include </usr/local/include/thrift/transport/TTransport.h>
    #include </usr/local/include/thrift/transport/TBufferTransports.h>
    #include </usr/local/include/thrift/protocol/TBinaryProtocol.h>
    #include <sstream>
    #include <boost/shared_ptr.hpp>
    
    using namespace std;
    using namespace apache::thrift;
    using namespace apache::thrift::protocol;
    using namespace apache::thrift::transport;
    using boost::shared_ptr;
    
    
    int main (int argc, char *argv[]) {
    
        ::std::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
        ::std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
        ::std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
        servClient client(protocol);
        transport->open();
    
        // 我们的代码写在这里
        student stu;
        stu.no = 1;
        stu.name = "weikai";
        stu.age = 22;
        client.put(stu);
    
        transport->close();
    
        return 0;
    }
    

    编译thrift-cpp-example服务端

    g++ -std=c++11 -DHAVE_NETINET_IN_H -g -Wall -I/usr/local/include/thrift/ -I/usr/include/boost/ -I./ -I./gen-cpp/ -I/usr/include/  gen-cpp/serv.cpp  gen-cpp/serv_server.skeleton.cpp gen-cpp/student_constants.cpp  gen-cpp/student_types.cpp -L/usr/local/lib/*.so -lthrift  -o bin/server
    

    编译thrift-cpp-example客户端

    g++ -std=c++11 -DHAVE_NETINET_IN_H -g -Wall -I/usr/local/include/thrift/ -I/usr/include/boost/ -I./ -I./gen-cpp/ -I/usr/include/  gen-cpp/serv.cpp  gen-cpp/student_constants.cpp  gen-cpp/student_types.cpp client/client.cpp -L/usr/local/lib/*.so -lthrift  -o bin/client
    

    执行程序如果报错

    luni@ubuntu:~/Project/src/thrift-cpp-example/thrift-cpp-example/bin$ ./server
    ./server: error while loading shared libraries: libthrift-0.13.0.so: cannot open shared object file: No such file or directory
    

    在明确已经安装该库的情况下出现上述错误,可能是这个库目录并没有加入到该环境变量中,解决办法如下:

    1、更改配置文件

    一般安装目录在

    /usr/local/lib
    

    将该目录加入到共享库的配置文件中

     echo "/usr/local/lib" >> /etc/ld.so.conf
     ldconfig
    

    2、临时解决方案

    export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH 
    

    命名空间和常量

    Thrift中的命名空间同C++中的namespace类似,它提供了一种组织(隔离)代码的方式。因为每种语言均有自己的命名空间定义方式,thrift允许开发者针对特定语言定义namespace:格式:namespace 语言名 路径

    namespace cpp brkservice
    

    定义接口service

    服务的定义类似于面向对象编程中的接口定义。Service支持继承,一个service可使用extends关键字继承另一个service。

    service HelloWord 
    {
        string  Action(1: string name, 2: i32 age);
    }
    
  • 相关阅读:
    QT QT程序初练
    Linux Shell编程三
    Linux Shell编程二
    git操作
    git push命令
    Zabbix全方位告警接入-电话/微信/短信都支持
    CentOS 7安装TigerVNC Server
    MySQL各版本的区别
    MariaDB Galera Cluster 部署
    MySQL高可用方案MHA的部署和原理
  • 原文地址:https://www.cnblogs.com/huanyinglvtuan/p/13955612.html
Copyright © 2020-2023  润新知