Linux下配置RPC框架Apache Thrift

104 篇文章 4 订阅
47 篇文章 2 订阅

Thrift最初生于Facebook,并茁壮成长,在2007年由Facebook正式开源出来,2008年由Apache软件基金会开始负责项目孵化直至今日。Thrift的全名叫做Apache Thrift,是一款由 Facebook 开发的远程服务调用框架框架,它可以很高效地实现跨语言的RPC服务。
github地址:https://github.com/apache/thrift
最新的版本是0.10.0:http://archive.apache.org/dist/thrift/0.10.0/

Thrift采用接口描述语言定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码生成引擎可以在多种语言中,如 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk 等创建高效的、无缝的服务,其传输数据采用二进制格式,相对 XML 和 JSON 体积更小,对于高并发、大数据量和多语言的环境更有优势。本文将介绍 Apache Thrift的配置和简单使用,并加以解释说明,帮助读者快速用Thrift构建服务。

Apache Thrift安装依赖于Boost1.53及以上,本文采用Boost1.53来安装Apache Thrift。
1.下载Boost1.53源代码包并编译安装:


wget https://nchc.dl.sourceforge.net/project/boost/boost/1.53.0/boost_1_53_0.tar.gz
tar -xzvf boost_1_53_0.tar.gz
cd boost_1_53_0
./bootstrap.sh
./b2 install --prefix=/usr    #指定boost库的安装位置
ldconfig  

2.下载thrift-0.10.0源代码包并编译安装:

wget http://archive.apache.org/dist/thrift/0.10.0/thrift-0.10.0.tar.gz
tar -xzvf thrift-0.10.0.tar.gz
cd thrift-0.10.0
./configure --with-boost=/usr   #指出boost库的安装位置
make -j8 && make install
ldconfig

接下来我们实现一个简单的功能:调用远程主机来计算两个的加减乘除,并把结果返回给本机。
首先自己新建一个文件,叫testping.thrift,如下:

# testping.thrift


/**
 * Thrift files can namespace, package, or prefix their output in various
 * target languages.
 */
namespace cpp freebird

/**
 * Defining a removed class named WorkerManager
 */
service WorkerManager {

  /**
   * client calls ping method to make sure service process is active or dead
   */
   void ping()

}

然后在testping.thrift文件所在目录执行:

thrift -r --gen cpp testping.thrift

这会在当前目录新建一个文件夹gen-cpp,cd进入到gen-cpp目录,执行tree ..命令,可以看到如下打印信息:

..
├── gen-cpp
│   ├── testping_constants.cpp
│   ├── testping_constants.h
│   ├── testping_types.cpp
│   ├── testping_types.h
│   ├── WorkerManager.cpp
│   ├── WorkerManager.h
│   └── WorkerManager_server.skeleton.cpp
└── testping.thrift

1 directory, 8 files

可见上面脚本执行后一口气生成了八个文件。
其中WorkerManager_server.skeleton.cpp文件如下:

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

#include "WorkerManager.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

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

using boost::shared_ptr;

using namespace  ::freebird;

class WorkerManagerHandler : virtual public WorkerManagerIf {
 public:
  WorkerManagerHandler() {
    // Your initialization goes here
  }

  /**
   * client calls ping method to make sure service process is active or dead
   */
  void ping() {
    // Your implementation goes here
    printf("counting....\n");
  }
  /*
  void show_result(int a,int b)
  {
      printf("+ result is: %d\n - result is :%d\n * result is :%d\n / result is :%d\n",a+b,a-b,a*b,a/b);
  }
  */

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<WorkerManagerHandler> handler(new WorkerManagerHandler());
  shared_ptr<TProcessor> processor(new WorkerManagerProcessor(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;
}

这里我们可以看到它绑定了9090端口并监听来自客户端的请求。
其实上面服务端的代码已经完成了,接下来编译生成server执行文件:

g++ -g -I/usr/local/include/thrif -L /usr/local/lib -lthrift testping_constants.cpp WorkerManager_server.skeleton.cpp WorkerManager.cpp testping_types.cpp -o server
#-I 链接include目录,-L链接lib文件

接下来提前开启服务端的监听:

./server

至此,服务端配置完成,监听也已经开启。
然后在另一台主机装好Apache Thrift,并用上面同样的方式生成客户端代码。
进入到客户端机器的gen-cpp目录,执行:

touch Client.cpp
gedit Client.cpp

键入以下代码:

#include "WorkerManager.h"  // Your .h File 
#include <iostream> 
#include <thrift/transport/TSocket.h>  
#include <thrift/transport/TBufferTransports.h>  
#include <thrift/protocol/TBinaryProtocol.h>  
using namespace std;
using namespace apache::thrift;  
using namespace apache::thrift::protocol;  
using namespace apache::thrift::transport;  
using boost::shared_ptr;

using namespace  ::freebird;


int main(int argc, char **argv) {  
    boost::shared_ptr<TSocket> socket(new TSocket("server_host_ip", 9090));  //替换IP
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  
    WorkerManagerClient client(protocol);
    int a =6;
     int b=3;
    transport->open();  

    // Your Codes 
    client.ping();
    //client.show_result(a,b);
    transport->close();
    return 0;  
} 

然后编译并执行代码并开启客户端请求:

g++ -g -I/usr/local/include/thrif -L /usr/local/lib -lthrift testping_constants.cpp Client.cpp WorkerManager.cpp testping_types.cpp -o client
#-I 链接include目录,-L链接lib文件
./client

如果提示:

error while loading shared libraries: libthriftc.so.0: cannot open shared object file: No such file or directory

可以编辑~/.bashrc文件:

vim ~/.bashrc

加入以下内容:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH  #thrift lib所在目录

然后保存生效:

source ~/.bashrc

接下来就应该没问题了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值