Windows下Thrift环境搭建与开发教程

一、准备工作

1.安装vs
2.下载可执行文件(用于在Windows下生成目标语言的桩代码);http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.1/thrift-0.9.1.exe
3.下载源代码包(注意版本与可执行文件相匹配);
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.1/thrift-0.9.1.tar.gz
4.下载boost库(我这里用的1.51,经测试可用);
https://sourceforge.net/projects/boost/files/boost/1.51.0/
5.下载libevent库(我这里用的是2.0.21,经测试可用);
http://libevent.org/

二、安装步骤

1.执行\boost_1_51_0下的bootstrap.bat批处理文件,在同级目录以生成b2.exe可执行文件;
2.执行刚刚生成的b2.exe,以生成stage\lib中的连接库文件,以备开发时使用;
3.解压缩thrift-0.9.1.tar.gz文件;
4.进入\thrift-0.9.1\lib\cpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库;
5.Libthrift工程配置
(1)libthrift>属性->C/C+±>常规->附加包含目录->\boost\boost_1_51
(2)libthrift>属性->库管理器->常规->附加库目录->\boost\boost_1_51\lib
6.Libthriftnb工程配置
(1)libthriftnb>属性->C/C+±>常规->附加包含目录->\boost\boost_1_51
(2)libthriftnb>属性->C/C+±>常规->附加包含目录->\libevent-2.0.21-stable
(3)libthriftnb>属性->C/C+±>常规->附加包含目录->\libevent-2.0.21-stable\include
(4)libthriftnb>属性->C/C+±>常规->附加包含目录->\libevent-2.0.21-stable\WIN32-Code
(5)libthriftnb>属性->库管理器->常规->附加库目录->\boost\boost_1_51\lib
7.编译libthrift和libthriftnb工程;
编译完成后,在\thrift-0.9.1\lib\cpp\Debug下生成libthrift.lib文件,和libthriftnb.lib文件。选择release模式,则在\thrift-0.9.1\lib\cpp\Release下生成libthrift.lib文件和libthriftnb.lib文件
注:为方便后续的调试与开发,这里建议release和debug都同时配置并生成lib文件。

三、开发步骤

1.写.thrift文件,也就是接口描述文件(Interface Description File);
2.用Thrift compiler for Windows (thrift-0.9.1.exe) ,生成目标语言代码;
3.服务器端程序引入thrift生成的代码,实现RPC业务代码。
4.客户端引入代码,调用远程服务。

四、测试例子

1.写.thrift文件
新建文本文件hello.txt,保存下面的内容后修改扩展名hello.thrift
在这里插入图片描述
2.生成目标语言代码
把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个目录(hello)下。
打开cmd命令行窗口,进入到这个目录,执行命令:
C:\Users\admin\Desktop\Hello>thrift-0.9.1.exe --gen cpp hello.thrift
执行成功,在hello目录下,生成一个gen-cpp文件夹。
在这里插入图片描述
3.创建工程
在vs中创建一个解决方案,名称为hello;
在hello的解决方案中创建两个空项目,一个为server,另一个为client;
4.为项目添加文件
(1)向Server项目添加文件。
复制gen-cpp文件夹中文件到Server工程,添加到Server工程中。
(2)向Client项目添加文件。
复制gen-cpp文件夹中文件到Client工程,删除hello_server.skeleton.cpp,并额外添加client.cpp文件。
最终解决方案的文件结构是这样的:
在这里插入图片描述
1.配置项目属性
Sever工程 Server>属性->C/C+±>常规->附加包含目录->\boost\boost_1_51
Sever工程 Server>属性->C/C+±>常规->附加包含目录->\thrift-0.9.1\lib\cpp\src
Sever工程 Server>属性->C/C+±>常规->附加包含目录->\thrift-0.9.1\lib\cpp\src\thrift
Sever工程 Server>属性->连接器->附加库目录->\boost\boost_1_51\lib
Sever工程 Server>属性->连接器->附加库目录->\boost\boost_1_51\stage\lib
注:这里的附加库目录指向的是第二步骤,安装时执行批处理文件生成的链接库
Sever工程 Server>属性->连接器->附加库目录->\thrift-0.9.1\lib\cpp\Debug
注:这里的附加库目录指向的是刚刚编译出的Debug目录,若此时编译的为release版本则在此处添加release目录

类似的,Client工程也做这样的配置。
Client工程 Client>属性->C/C+±>常规->附加包含目录->\boost\boost_1_51
Client工程 Client>属性->C/C+±>常规->附加包含目录->\thrift-0.9.1\lib\cpp\src
Client工程 Client>属性->C/C+±>常规->附加包含目录->\thrift-0.9.1\lib\cpp\src\thrift
Client工程 Client>属性->连接器->附加库目录->\boost\boost_1_51\lib
Client工程 Client>属性->连接器->附加库目录->\boost\boost_1_51\stage\lib
Client工程 Client>属性->连接器->附加库目录->\thrift-0.9.1\lib\cpp\Debug
2.Client代码
client.cpp文件是空的,添加代码:
#include “hello.h”
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>

#pragma comment(lib, “libthrift.lib”)
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

#pragma comment(lib,“libthrift.lib”)//链接库文件

int main(int argc, char** argv) {
int port = 9090;
boost::shared_ptr socket(new TSocket(“127.0.0.1”, 9090));
boost::shared_ptr transport(new TBufferedTransport(socket));
boost::shared_ptr protocol(new TBinaryProtocol(transport));

helloClient client(protocol);
try {
	transport->open();

	client.func1();

	transport->close();
}
catch (TException& tx) {
	printf("ERROR:%s\n", tx.what());
}
getchar();
return 0;

}
3.Server代码
hello_server.skeleton.cpp 文件已经有thrift生成的代码,稍作修改,最终如下:

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

#include “hello.h”
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

#pragma comment(lib, “libthrift.lib”)

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

class helloHandler : virtual public helloIf {

public:

helloHandler() {

	// Your initialization goes here
}

void func1() {

	// Your implementation goes here

	printf("Hello Thrift\n");

}

};

int main(int argc, char **argv) {

//-----------------------------//
WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
//-------------------------------//
//对上面这段代码做个说明,这是依赖windows的一段代码
//到2014.9.2官网的稳定版0.9.1,仍需要这段代码才可以在windows下编译通过。
//但是如果用git clone最新版,这个错误已经修正
//最新版注释掉这段代码,一样可以在windows下编译通过。
//备注时间:2014.9.2

int port = 9090;
shared_ptr<helloHandler> handler(new helloHandler());
shared_ptr<TProcessor> processor(new helloProcessor(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;

}

4.调试运行
先启动Server工程,再启动Client工程。运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值