ubuntu thrift 0.9.3编译安装


Table of Contents


1 下载thrift源代码

git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
cd thrift/
git checkout 0.9.3



或者下载版本解压到指定文件(/work/thrift/)目录


2 编译并安装

安装依赖

sudo apt-get install libssl-dev
sudo apt-get install byacc
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install libevent-dev

生成编译文件


cd /work/thrift
./bootstrap.sh
说明:如果提示:
apt-get install libtool 

则:sudo apt-get install libtool 
(略过)



编译前配置,禁用go , 安装目录设为/usr/lib,而不是默认的/usr/local/lib

./configure --libdir=/usr/lib --without-go --without-java
or ./configure --with-cpp  --with-boost --without-python --without-csharp --without-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell --without-go
说明:安装thrift 0.9 时 configure: error: "Error: libcrypto required."
  
  

On Ubuntu installing libssl-dev package should fix it.

sudo apt-get install libssl-dev

http://stackoverflow.com/questions/9123457/configure-thrift-libcrypto-required



编译

make


3 运行测试程序

测试

make check

(略过)

4 安装

sudo make install

注意:

  • $thrift-git-project 是thrift gi项目所在目录
  • 库文件安装在/usr/lib目录下
  • 头文件安装在/usr/local/include/thrift目录下

///
//


Thrift的简单示例   


首先创建Thrift的语法规则文件,命名为server.thrift,内容如下:

struct message
{
	1:i32 seqId,
	2:string content
}

service serDemo
{
	void put(1:message msg)//<span style="font-family:Arial, Helvetica, sans-serif;">说明:可以有返回值的</span>

}

在shell下面执行执行:

thrift -gen cpp server.thrift 
thrift -gen java server.thrift (java 生成的)

该语句用于创建c++服务框架,创建成功后会在该目录下生成gen-cpp文件夹,然后修改该目录下的serDemo_server.skeleton.cpp,在put函数中添加如下代码:
class serDemoHandler : virtual public serDemoIf {  
 public:  
  serDemoHandler() {  
    // Your initialization goes here  
  }  
  
  void put(const message& msg) {  
    // Your implementation goes here  
    printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str());  
  } 

然后进行编译就可以了 : g++ -o server *.cpp -lthrift

上面是server框架的代码,对于client的框架其实已经创建,但是现在需要添加client执行代码,可以在该目录下创建client.cpp,然后输入以下内容,下面的内容可以作为SimpleServer的client的模板,只需修改注释的部分。


// -------------------------替换成对应service名字的.h  文件------------------------
#include "SerDemo.h"  
//------------------------------------------------------------------------------
#include <thrift/transport/TSocket.h>  
#include <thrift/transport/TBufferTransports.h>  
#include <thrift/protocol/TBinaryProtocol.h>  
  
using namespace apache::thrift;  
using namespace apache::thrift::protocol;  
using namespace apache::thrift::transport;  
  
using boost::shared_ptr;  
  
int main(int argc, char **argv) {  
    boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));  
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  
  
    transport->open();  
  
    // ----------------------------我们的代码写在这里------------------------------
    message msg;
    msg.seqId = 1;
    msg.content = "client message";

    serDemoClient client(protocol);
    client.put(msg);
    //--------------------------------------------------------------------------

    transport->close();  
  
    return 0;  
}

然后进行编译: g++ -o client *[^n].cpp - lthrift ,也可以将serDemo_server.skeleton.cpp移动到其它目录,使用 g++ -o client *.cpp - lthrift 命令。

然后就可以执行了,启动server后,启动client,server执行如下:


anonymalias@anonymalias-Rev-1-0:~/code/thriftSerDemo/gen-cpp$ ./server 
receive message: id: 1, content: client message


thrift 异步:

客户端异步

thrift也提供了异步客户端的实现,但生成代码时需要添加cob_style属性,即运行以下命令:

[cpp]  view plain  copy
  1. thrift --gen cpp:cob_style a.thrift   

生成的代码中包含一个AsynClient的类以供实现异步调用,初步看到是使用回调函数进行的。
此种方法正在研究中,随后会将研究结果补充上来

服务端异步

Thrift服务端异步通过使用TNonblockingServer实现,TNonblockingServer依赖libevent,即编译Thrift时系统必须已经安装libevent,否则编译出的Thrift不包含TNonblockingServer的实现,ubuntu安装libevent使用如下命令:

[cpp]  view plain  copy
  1. sudo apt-get install libevent-dev  

同时使用TNonblockingServer时,应用程序编译命令也需要添加 -lthriftnb -levent。使用TNonblockingServer的代码如下:

[cpp]  view plain  copy
  1. int main(int argc, char **argv)   
  2. {  
  3.     int port = 9090;       shared_ptr<serDemoHandler> handler(new serDemoHandler());
         shared_ptr<TProcessor> processor(new serDemoProcessor(handler));
         shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
         shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
         shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
         shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);//指定10个线程数
  4. shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
  5. threadManager->threadFactory(threadFactory);
  6. threadManager->start();
  7. //TThreadPoolServer server(processor,serverTransport,
  8. transportFactory,protocolFactory,threadManager);//线程池
  9. TNonblockingServer server(processor,protocolFactory,port,threadManager);//使用线程池
  10. //TNonblockingServer server(processor,protocolFactory,port);//不使用线程池
  11. printf("Starting the server...\n");
  12. server.serve();
  13. printf("done.\n");
  14. return 0;  
  15. }  

TNonblockingServer也可以不使用线程池,仅仅使用单线程处理,此时只需在构造TNonblockingServer时不添加threadManager即可,如以下代码:

[cpp]  view plain  copy
  1. TNonblockingServer server(processor,  
  2.         protocolFactory,  
  3.         port);  

TNonblockingServer区别于其他server(例如TThreadPoolServer)在于:TNonblockingServer使用epoll与udp协议(TFramedTransport传输方式)实现,这样既可使用很少的线程实现大并发,而不会像TThreadPoolServer那样并发受线程池线程数限制。
所以使用TNonblockingServer的异步也仅仅是server内部实现思想上的异步,将线程池的阻塞线程处理请求改为了非阻塞串行处理,TNonblockingServer调用serve方法时本身还是会阻塞调用线程。

调用serve方法不阻塞方法应该也很多,并且还有服务端callback方式,有时间找到再补上来吧


需包含头文件:
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/server/TNonblockingServer.h>

using namespace ::apache::thrift::concurrency;



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值