标签:pytho esc question 服务 www 序列 types.h clu types
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协议层和传输层的实现。
过程:
何谓RPC远程过程调用?
RPC(Remote Procedure Call)远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
namespace cpp ld
struct 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 client
server : 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 server
client: 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
标签:pytho esc question 服务 www 序列 types.h clu types
原文地址:https://www.cnblogs.com/walkinginthesun/p/9559466.html