第一个thrift示例

1.我机器上已经安装成功thrift-0.10.0

2.编辑demo.thrift文件

struct UserInfo {
    1: required i32 id
    2: required string name
    3: required string sex 
}
service UserSeivice {
    void storeInfo(1: UserInfo info)
    UserInfo getInfo(1: i32 id) 
}

 运行编译:

thrift -r --gen cpp demo.thrift 

 之后会生成一个 gen-cpp的目录,gen-cpp目录内容如下:

demo_constants.cpp  demo_constants.h  demo_types.cpp  demo_types.h  UserSeivice.cpp  UserSeivice.h  UserSeivice_server.skeleton.cpp

 其中只有UserSeivice_server.skeleton.cpp是和业务相关的,可以修改,将该文件拷贝到和demo.thrift同级目录并重命名为server.cpp,然后修改该文件如下(修改部分均做了标注):

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include "gen-cpp/UserSeivice.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

// added by Lyndon, begin
#include <iostream>
#include <map>
using namespace std;
// added by Lyndon, end
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

class UserSeiviceHandler : virtual public UserSeiviceIf {
    public:
    UserSeiviceHandler() {
        // Your initialization goes here
    }

    void storeInfo(const UserInfo& info) {
        // Your implementation goes here
        // added by Lyndon, begin
        m_user_map.insert(std::pair<int32_t, UserInfo>(info.id, info));
        std::cout << "storeInfo, now map size is:" << m_user_map.size() << std::endl;
        // added by Lyndon, end
    }   

    void getInfo(UserInfo& _return, const int32_t id) {
        // Your implementation goes here
        // added by Lyndon, begin
        _return = m_user_map[id];
        std::cout << "getInfo, id:" << id << ",name: " << _return.name
            << ",sex: " << _return.sex << std::endl;
        // added by Lyndon, end
    }

private:
    // added by Lyndon, begin
    std::map<int32_t, UserInfo>  m_user_map;
    // added by Lyndon, end
};

int main(int argc, char **argv) {
    int port = 9090;
    shared_ptr<UserSeiviceHandler> handler(new UserSeiviceHandler());
    shared_ptr<TProcessor> processor(new UserSeiviceProcessor(handler));
    shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    server.serve();
    return 0;
}

上面是一个简单的服务端代码,之后编译客户端代码,从thrift官方拷贝一份客户端框架代码重命名为client.cpp并做如下修改(修改部分同上做了标注):

 1 #include <iostream>
 2 #include <unistd.h>
 3 #include <sys/time.h>
 4 #include <thrift/protocol/TBinaryProtocol.h>
 5 #include <thrift/transport/TSocket.h>
 6 #include <thrift/transport/TTransportUtils.h>
 7 // added by Lyndon, begin
 8 #include "gen-cpp/UserSeivice.h"
 9 #include "gen-cpp/demo_types.h"
10 // added by Lyndon, end
11 
12 using namespace std;
13 using namespace apache::thrift;
14 using namespace apache::thrift::protocol;
15 using namespace apache::thrift::transport;
16 using namespace boost;
17 int main() {
18     shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
19     shared_ptr<TTransport> transport(new TBufferedTransport(socket));
20     shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
21     UserSeiviceClient client(protocol);  // added by Lyndon
22     try {
23         transport->open();
24         // added by Lyndon, begin
25         UserInfo user;
26         user.id = 0;
27         user.name = "Lyndon";
28         user.sex = "Male";
29         client.storeInfo(user);
30         UserInfo user2;
31         client.getInfo(user2, 0); 
32         std::cout << "user.id = " << user2.id << ",user.name = "
33             << user2.name << ",user.sex = " << user2.sex << std::endl;
34         // added by Lyndon, end
35         transport->close();
36     } catch (TException &tx) {
37         std::cout << "ERROR:" << tx.what();
38     }
39 }

之后编写makefile文件,如下(注意makefile的格式):

BOOST_DIR = /usr/local/include/boost/
THRIFT_DIR = /usr/local/include/thrift/
LIB_DIR = /usr/local/lib
GEN_SRC = ./gen-cpp/UserSeivice.cpp ./gen-cpp/demo_constants.cpp ./gen-cpp/demo_types.cpp
default: server client
server: server.cpp
        g++ -o server -I${THRIFT_DIR} -I${BOOST_DIR}  -I ./gen-cpp -L${LIB_DIR} server.cpp ${GEN_SRC} -lthrift
client: client.cpp
        g++ -o client -I${THRIFT_DIR} -I${BOOST_DIR}  -I ./gen-cpp -L${LIB_DIR} client.cpp ${GEN_SRC} -lthrift
clean:
        $(RM) -r client server

 之后编译执行:

make之后会生成server和client两个可执行文件,
先执行server文件,之后执行client文件,会打印如下结果:
client端:
user.id = 0,user.name = Lyndon,user.sex = Male
server端:
storeInfo, now map size is:1
getInfo, id:0,name: Lyndon,sex: Male

 参考链接:

https://thrift.apache.org/tutorial/cpp

http://blog.163.com/zhangjie_0303/blog/static/9908270620140311022650/

http://blog.51cto.com/speakingbaicai/1160378

转载于:https://www.cnblogs.com/LyndonYoung/articles/7977654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值