由于更换了数据库,因此要在新写接口函数DLL,封装thrift的客户端。thrift官网资料不够详细,在网上查阅各种资料后,终于成功,在此总结一下。
1准备
下载
thrift-0.9.2.tar.gz
Thrift compiler for Windows (thrift-0.9.2.exe)
0.9.3版本下缺少文件,编译时报错,无法成功,在这上面花费了太多时间。
boost库1.52版本,编译时的命令
bjam stage --toolset=msvc-10.0 link=shared runtime-link=shared threading=multi debug release
bjam stage --toolset=msvc-10.0 link=static runtime-link=static threading=multi debug release
bjam stage --toolset=msvc-10.0 threading=multi debug release
需要哪种boost编译出来的静态文件,可根据后面工程报错重新编译,如果不确定的话
libevent库,libevent-2.0.21-stable
2 编译libthrift和libthriftnb静态文件
进入\thrift-0.9.2\lib\cpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。
两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。
libthrift工程配置:
libthrift>属性->C/C++->常规->附加包含目录->\boost\boost_1_52
libthrift>属性->库管理器->常规->附加库目录->\boost\boost_1_52\stage\lib
此外,还需要openssl,下载对应编译器的库文件,libthrift>属性->C/C++->常规->附加包含目录->openssl-1.0.1q-vs2010\include
libthriftnb工程配置:
libthriftnb>属性->C/C++->常规->附加包含目录->\boost\boost_1_52
\libevent-2.0.21-stable
\libevent-2.0.21-stable\include
\libevent-2.0.21-stable\WIN32-Code
libthriftnb>属性->库管理器->常规->附加库目录->
\boost\boost_1_52\stage\lib
完成配置后,就会成功生成libthrift.lib文件和libthriftnb.lib文件。
3生成.h 和.cpp文件
在命令行窗口,用Thrift compiler for Windows (thrift-0.9.2.exe) 生成对应文件:thrift-0.9.2.exe --gen cpp hello.thrift
4创建DLL工程
在DLL项目中,添加文件
配置工程:
属性->C/C++->常规->附加包含目录->\boost\boost_1_52
属性->C/C++->常规->附加包含目录->\thrift-0.9.2\lib\cpp\src
属性->C/C++->常规->附加包含目录->\thrift-0.9.2\lib\cpp\src\thrift
属性->连接器->附加库目录->\boost\boost_1_52\stage\lib
属性->连接器->附加库目录->\thrift-0.9.2\lib\cpp\Debug
附加依赖项 libthrift.lib
对应的client文件是自己写的,部分代码是:
boost::shared_ptr<TSocket> socket(new TSocket(strParameters[0], atoi(strParameters[1])));
boost::shared_ptr<TTransport> transportTemp(new TBufferedTransport(socket));
transportRTDB = transportTemp;
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transportRTDB));
client = new ::com::gdtianren::thrift::RTDBClient(protocol);
transportRTDB->open();
配置完成后,就可生成DLL,并在server调用对应方法。