分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Apache Thrift 的使用

发布时间:2023-09-06 02:12责任编辑:赖小花关键词:暂无标签

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved