Thrift Win7安装与实例 vsWord格式.docx

上传人:b****6 文档编号:17030126 上传时间:2022-11-28 格式:DOCX 页数:15 大小:576.75KB
下载 相关 举报
Thrift Win7安装与实例 vsWord格式.docx_第1页
第1页 / 共15页
Thrift Win7安装与实例 vsWord格式.docx_第2页
第2页 / 共15页
Thrift Win7安装与实例 vsWord格式.docx_第3页
第3页 / 共15页
Thrift Win7安装与实例 vsWord格式.docx_第4页
第4页 / 共15页
Thrift Win7安装与实例 vsWord格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Thrift Win7安装与实例 vsWord格式.docx

《Thrift Win7安装与实例 vsWord格式.docx》由会员分享,可在线阅读,更多相关《Thrift Win7安装与实例 vsWord格式.docx(15页珍藏版)》请在冰豆网上搜索。

Thrift Win7安装与实例 vsWord格式.docx

●两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。

 

2)libthrift工程配置:

●libthrift>

属性->

C/C++->

常规->

附加包含目录:

\boost\boost_1_51

库管理器->

附加库目录:

\boost\boost_1_51\lib

●若使用openssl库,添加相关配置 

3)libthriftnb工程配置:

libthriftnb>

●\boost\boost_1_51

●\libevent-2.0.21-stable

●\libevent-2.0.21-stable\include

●\libevent-2.0.21-stable\WIN32-Code

●\boost\boost_1_51\lib 

4)编译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文件。

若编译存在问题,可能是工程添加路径文件不对或者某些库文件不存在。

至此,安装完成。

开发步骤

安装好thrift后,就可以开始开发了。

开发过程分这么几步:

第1步:

写.thrift文件,也就是接口描述文件(Interface 

Description 

File);

第2步:

用ThriftcompilerforWindows(thrift-0.9.1.exe) 

,生成目标语言代码;

第3步:

服务器端程序引入thrift生成的代码,实现RPC业务代码。

第4步:

客户端引入代码,调用远程服务。

图中蓝色Thrift.exe就是从官网下载的第二个文件——“IDL翻译工具”,帮助你把.thrift文件“翻译”成目标语言的RPC代码。

例子

这个例子,远程Server提供一个函数。

客户端调用这个函数。

远程函数的功能很简单,就是输出“Hello 

Thrift”。

1)写.thrift文件

新建文本文件hello.txt,保存下面的内容后修改扩展名hello.thrift

servicehello

{

voidfunc1()

}

hello.thrift

2)生成目标语言代码

●把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个目录(hello)下。

●打开cmd命令行窗口,进入到这个目录,

●执行命令:

(thrift--gen 

cpp 

hello.thrift)C:

\Users\admin\Desktop\Hello>

thrift-0.9.1.exe 

--gen 

●执行成功,在hello目录下,生成一个gen-cpp文件夹。

3)创建工程

●Visual 

Studio 

2010新建win32控制台应用程序。

●项目名称 

server

●解决方案名称 

hello 

●注意:

附加选项中选择 

勾选 

空项目。

●类似的,在hello解决方案下,再新建一个空项目client。

4)为项目添加文件

●向Server项目添加文件。

●复制gen-cpp文件夹中文件到Server工程,添加到Server工程中。

●向Client项目添加文件。

●复制gen-cpp文件夹中文件到Client工程,删除hello_server.skeleton.cpp,并额外添加client.cpp文件(注意添加库文件#pragmacomment(lib,"

libthrift.lib"

))。

●也可创建一个公共源代码文件(在工程中分别添加进去)

●最终解决方案的文件结构是这样的:

5)配置项目属性

●Sever工程 

Server>

\thrift-0.9.1\lib\cpp\src

\thrift-0.9.1\lib\cpp\src\thrift

●若使用OpenSSL添加相关路径依赖及引入库文件 

连接器->

\thrift-0.9.1\lib\cpp\Debug

附加库目录指向的是刚刚编译出的Debug目录(此处可将相关库文件放置在你的工程里面,修改附加库目录即可)

类似的,Client工程也做这样的配置。

●Client工程 

Client>

6)Client代码

client.cpp文件是空的,添加代码:

#include<

transport/TSocket.h>

#include"

hello.h"

protocol/TBinaryProtocol.h>

server/TSimpleServer.h>

transport/TServerSocket.h>

transport/TBufferTransports.h>

string>

#pragmacomment(lib,"

)//链接库文件

#pragmacomment(lib,"

ssleay32.lib"

#pragmacomment(lib,"

libeay32.lib"

usingnamespace:

:

apache:

thrift;

thrift:

protocol;

transport;

server;

usingboost:

shared_ptr;

intmain(intargc,char**argv){

intport=9090;

shared_ptr<

TTransport>

socket(newTSocket("

127.0.0.1"

9090));

transport(newTBufferedTransport(socket));

TProtocol>

protocol(newTBinaryProtocol(transport));

helloClientclient(protocol);

try{

transport->

open();

client.func1();

close();

}catch(TException&

tx){

printf("

ERROR:

%s\n"

tx.what());

}

getchar();

return0;

client.cpp

7)Server代码

hello_server.skeleton.cpp 

文件已经有thrift生成的代码,稍作修改,最终如下:

//Thisautogeneratedskeletonfileillustrateshowtobuildaserver.

//Youshouldcopyittoanotherfilenametoavoidoverwritingit.

thrift/protocol/TBinaryProtocol.h>

thrift/server/TSimpleServer.h>

thrift/transport/TServerSocket.h>

thrift/transport/TBufferTransports.h>

classhelloHandler:

virtualpublichelloIf{

public:

helloHandler(){

//Yourinitializationgoeshere

voidfunc1(){

//Yourimplementationgoeshere

printf("

func1\n"

);

};

intmain(intargc,char**argv){

intport=9090;

helloHandler>

handler(newhelloHandler());

TProcessor>

processor(newhelloProcessor(handler));

TServerTransport>

serverTransport(newTServerSocket(port));

TTransportFactory>

transportFactory(newTBufferedTransportFactory());

TProtocolFactory>

protocolFactory(newTBinaryProtocolFactory());

TSimpleServerserver(processor,serverTransport,transportFactory,protocolFactory);

server.serve();

return0;

hello_server.skeleton.cpp

8)调试运行

先启动Server工程,再启动Client工程。

运行结果:

总结

到这里Thrift的安装和开发基本操作步骤就介绍完了。

Thrift的官方文档不是很完善,本篇介绍的安装方法不在网上众多教程之列,主要区别是没有使用Cygwin或者MinGW。

编译问题

● 

若在libthrift库中不使用openssl去除libthrift库工程中文件

TSSLSocket.cpp

TSSLSocket.h

若使用openssl应用程序工程文件中加载Openssl库

否则链接库出错

●Server工程编译问题

最重要的一步,需要在libthrift库工程项目中引入TServerFramework和TConnectedClient

否则你可能会出现这样的错误:

I'

mtestagaintobuildthelib,findthatmustbeaddTConnectedClientandTServerFramework.

路径:

thrift-0.10.0\lib\cpp\src\thrift\server

更多编译问题可以到其管网论坛搜索解决方案https:

//issues.apache.org/jira/browse/THRIFT-3810

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1