Apache Thrift
Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目。Thrift通过IDL(Interface Definition Language,接口定义语言)来定义RPC(Remote Procedure Call,远程过程调用)的接口和数据类型,然后通过thrift编译器生成不同语言的代码(目前支持C++,Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk和OCaml),并由生成的代码负责RPC协议层和传输层的实现。
- Apache Thrift
- Apache Thrift Tutorial
- Apache Thrift interface description language
- Apache Thrift Whitepaper
- Thrift入门教程
- thrift 的原理和使用
- 谁能用通俗的语言解释一下什么是 RPC 框架?
过程:
- Call ID 映射
- 序列化和反序列化
- 网络传输
何谓RPC远程过程调用?
RPC(Remote Procedure Call)远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
示例
WhatTime.thrift文件
namespace cpp ldstruct Example{ ???1:i32 ????number=10, ???2:i64 ????bigNumber, ???3:double ?decimals, ???4:string ?name="thrifty"}service TimeInterface{ ???i32 ?GetTime(), ???void SetTime()}
generate the source from a thrift file
thrift [-r] --gen <language> <Thrift filename># -r 可选
client.cpp
#include "TimeInterface.h"#include <thrift/transport/TSocket.h>#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/transport/TBufferTransports.h>using namespace ::apache::thrift;using namespace ::apache::thrift::protocol;using namespace ::apache::thrift::transport;using boost::shared_ptr;using namespace ?::ld;int main(int argc, char **argv) { ???int port = 9090; ???boost::shared_ptr<TSocket> socket(new TSocket("localhost", port)); //注意此处的ip和端口 ???boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); ???boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); ???transport->open(); ???// 我们的代码写在这里 ???TimeInterfaceClient client(protocol); ?// 客户端接口,声明于TimeInterface.h中。 ???client.SetTime(); ???client.GetTime(); ???????transport->close(); ???return 0;}
makefile
all : server clientserver : TimeInterface.cpp ?TimeInterface.h ?TimeInterface_server.skeleton.cpp ?WhatTime_constants.cpp ?WhatTime_constants.h ?WhatTime_types.cpp ?WhatTime_types.h ???????g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp ?TimeInterface.h ?TimeInterface_server.skeleton.cpp ?WhatTime_constants.cpp ?WhatTime_constants.h ?WhatTime_types.cpp ?WhatTime_types.h -o serverclient: TimeInterface.cpp ?TimeInterface.h ?WhatTime_constants.cpp ?WhatTime_constants.h ?WhatTime_types.cpp ?WhatTime_types.h client.cpp ???????g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp ?TimeInterface.h ?WhatTime_constants.cpp ?WhatTime_constants.h ?WhatTime_types.cpp ?WhatTime_types.h client.cpp -o client
Apache Thrift 的使用
原文地址:https://www.cnblogs.com/walkinginthesun/p/9559466.html