thrift 轻松实现多语言跨服务器通信
来源:互联网 发布:天龙八部for mac 编辑:程序博客网 时间:2024/06/05 03:45
thrift是由facebook开发的轻量级跨语言的服务框架,现在已经移交到apache基金会下。和他类似的是google出的protocol buffer和ice。 thrift的一大优势就是支持的语言很丰富,它使用自己的IDL语言来描述服务接口和数据交换的格式。
官方网站:http://incubator.apache.org/thrift/
一、安装:
yum -y install gcc-c++ autoconf automake sysconftool boost / boost-devel libtool perl-ExtUtils-MakeMaker gettext-base / gettext gettext-devel liblocale-gettext-perl zlib-devel / byacc bison flex pkgconfig python-devel wget http://apache.freelamp.com/incubator/thrift/0.2.0-incubating/thrift-0.2.0-incubating.tar.gz ./bootstrap.sh ./configure --prefix=/usr/local/thrift --with-ruby=no --with-erlang=no --with-java=no --with-csharp=no --enable-gen-java=no --enable-gen-csharp=no --enable-gen-rb=no --enable-gen-erl=nomakemake install
、IDL描述:
# 1.支持的变量类型 类型描述 bool#true, false byte#8位的有符号整数 i16#16位的有符号整数 i32#32位的有符号整数 i64#64位的有符号整数 double#64位的浮点数 string#UTF-8编码的字符串 binary#字符数组 struct#结构体 list<type>#有序的元素列表,类似于STL的vector set<type>#无序的不重复元素集,类似于STL的set map<type1,type2>#key-value型的映射,类似于STL的map exception#是一个继承于本地语言的exception基类 service#服务包含多个函数接口(纯虚函数) # 2.摘一段例子上来,让瞧瞧这是啥东东。(本例子文件名为:tutorial.thrift,是本身带的教程。)include "shared.thrift" namespace cpp tutorialnamespace java tutorialnamespace php tutorialnamespace perl tutorialnamespace smalltalk.category Thrift.Tutorial typedef i32 MyInteger const i32 INT32CONSTANT = 9853const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'} enum Operation { ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4} struct Work { 1: i32 num1 = 0, 2: i32 num2, 3: Operation op, 4: optional string comment,} exception InvalidOperation { 1: i32 what, 2: string why} service Calculator extends shared.SharedService { void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), oneway void zip() } # 3. 我们来写个 helloworld.thrift service HelloWorld{ string ping(1: string name), string getpng(),}三、编译 helloworld:
/usr/local/thrift/bin/thrift -r --gen py helloworld.thrift/usr/local/thrift/bin/thrift -r --gen php helloworld.thrift#会在当前目录下生成 gen-* 目录。四、编写服务器端:
import syssys.path.append('./gen-py') from helloworld import HelloWorldfrom helloworld.ttypes import * from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServer class HellowordHandler:def __init__ (self):pass def ping (self, name):print name + ' from server.'return "%s from server." % namedef getpng (self):f = open("./logo.png", "rb")c = f.read()f.close()return chandler = HellowordHandler()processor = HelloWorld.Processor(handler)transport = TSocket.TServerSocket(9090)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) # You could do one of these for a multithreaded server#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) print 'Starting the server...'server.serve()print 'done.'五、编写客户端:
<?phptry{ $GLOBALS['THRIFT_ROOT'] = './php/src'; require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';error_reporting(E_NONE);$GEN_DIR = './gen-php';require_once $GEN_DIR.'/helloworld/HelloWorld.php';error_reporting(E_ALL); $socket = new TSocket('*.*.*.*', 9090);$transport = new TBufferedTransport($socket, 1024, 1024);$protocol = new TBinaryProtocol($transport);$client = new HelloWorldClient($protocol); $transport->open(); $a = $client->ping('xyq ');echo $a; $transport->close(); } catch (TException $tx) {print 'TException: '.$tx->getMessage()."/n";} ?>按上面的流程就可以写自己的thrift了,我使用py做服务端,用php做客户端,当然也可以使用c++来做服务端。
- [转]thrift 轻松实现多语言跨服务器通信
- thrift 轻松实现多语言跨服务器通信
- thrift 轻松实现多语言跨服务器通信
- thrift 轻松实现多语言跨服务器通信
- [转]thrift 轻松实现多语言跨…
- 使用thrift实现了Java服务器和nodejs客户端之间的跨平台通信
- 基于Thrift实现跨语言服务
- 用thrift实现多语言相互调用
- 有了Thrift,跨语言通信不用再发愁
- 跨语言通信框架Apache Thrift在PHP中的使用
- Thrift框架实现Socket通信
- 【通信框架】跨语言通信框架的比较——Protobuf、Thrift和Avro
- Thrift白皮书《Thrift:可扩展的跨语言服务实现》 中文完整翻译
- Thrift:可扩展的跨语言服务实现
- Java跨语言调用实现方案 protobuf thrift Hadoop avro
- Golang通过Thrift框架完美实现跨语言调用
- Golang通过Thrift框架完美实现跨语言调用
- Golang通过Thrift框架完美实现跨语言调用
- 给力的android手机照片处理软件(PicClipper)诞生记
- myeclipse高效率使用
- MyEclipse编码改写
- Java与面向对象概述
- (ZZ)三大WEB服务器对比分析(apache ,lighttpd,nginx)
- thrift 轻松实现多语言跨服务器通信
- 在建立与服务器的连接时出错。在连接到 sql server 2005 时 在默认的设置下 sql server 不允许进行远程连
- 关于Myeclipse本身
- Internet地址概述
- c语言调用汇编模块
- (ZZ)设计高性能网站架构-LLMP
- DNA-批量域名转IP程序使用方法
- 深入学习Flex组件生命周期
- 使用winavr2010+avrStudio时要注意的地方