Linux下配置RPC框架Apache Thrift
来源:互联网 发布:程序员必备的手机app 编辑:程序博客网 时间:2024/05/28 17:05
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.gztar -xzvf boost_1_53_0.tar.gzcd 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.gztar -xzvf thrift-0.10.0.tar.gzcd thrift-0.10.0./configure --with-boost=/usr #指出boost库的安装位置make -j8 && make installldconfig
接下来我们实现一个简单的功能:调用远程主机来计算两个的加减乘除,并把结果返回给本机。
首先自己新建一个文件,叫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.thrift1 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.cppgedit 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
接下来就应该没问题了。
- Linux下配置RPC框架Apache Thrift
- thrift rpc框架入门
- Thrift RPC框架介绍
- 回顾:Thrift RPC框架介绍
- RPC简介与Thrift框架
- RPC框架-Thrift的使用
- RPC框架Thrift架构描述
- RPC框架 之 Apach thrift
- 使用RPC框架Apache Thrift在远程主机运算并返回
- Apache Thrift简介,与其它RPC对比
- Apache Thrift简介,与其它RPC对比
- Apache Thrift 配置环境
- Apache Thrift 配置环境
- RPC 工具--Thrift 框架 (一)
- 跨语言RPC框架Thrift详解
- Linux下软件开发RPC框架
- thrift系列 - harpc 基于thrift的轻量级rpc框架
- Linux下配置apache
- microsoft sql server management studio 弹出 字体“Aharoni” 不支持“Regular”
- java.net.SocketException: Software caused connection abort: socket write error
- 将字符串内的特定的字符或字符串替换成另一个字符或字符串(纯C)
- Java I/O操作
- 微信OAuth2网页授权登陆接口
- Linux下配置RPC框架Apache Thrift
- Android 随笔集
- 自定义信号槽
- repo,git相关命令使用
- CORS 跨域 实现思路及相关解决方案
- 【南阳理工】 44 子串和
- android 修改源码framework后如何编译
- 在lua环境中使用protobuf ,编译protobuf文件
- 虚函数