当前位置: 首页 > 后端技术 > PHP

Thrift-简单实用

时间:2023-03-29 19:53:46 PHP

简介ApacheThrift软件框架,用于可扩展的跨语言服务开发,将软件堆栈与代码生成引擎相结合,构建可在C++、Java、Python、PHP、Ruby之间高效无缝工作的服务、Erlang、Perl、Haskell、C#、Cocoa、JavaScript、Node.js、Smalltalk、OCaml和Delphi等语言。ApacheThrift是一个可扩展的跨语言服务开发的软件框架,结合了软件堆栈和代码生成引擎,用于构建C++、Java、Python等语言,使它们能够无缝结合并高效服务.安装brewinstallthrift进行跨语言调用,打破不同语言之间的壁垒。跨项目调用,微服务。示例前提Thrift版本:Go版本、Php版本、Python版本:本示例包含三种语言:python、php、go。(java暂不可用)新建HelloThrift.thrift,进入目录cd/Users/birjemin/Developer/Php/study-php写入HelloThrift.thriftvimHelloThrift.thrift内容如下:namespacephpHelloThrift{stringSayHello(1:stringusername)}php测试加载thrift-php库composer需要apache/thrift生成php版本的thrift相关文件cd/Users/birjemin/Developer/Php/study-phpthrift-r--genphp:serverHelloThrift.thriftAt这次,一个名为gen-php的目录。建立Server.php文件registerDefinition('HelloThrift',$GEN_DIR);$loader->register();classHelloHandlerimplements\HelloThrift\HelloServiceIf{publicfunctionSayHello($username){return"Php-Server:".$用户名;}}$handler=newHelloHandler();$processor=new\HelloThrift\HelloServiceProcessor($handler);$transport=newTBufferedTransport(newTPhpStream(TPhpStream::MODE_R|TPhpStream::MODE_W));$protocol=newTBinaryProtocol($transport,true,true);$transport->open();$processor->process($protocol,$protocol);$transport->close();建立Client.php文件registerDefinition('HelloThrift',$GEN_DIR);$loader->register();if(array_search('--http',$argv)){$socket=newTHttpClient('local.study-php.com',80,'/Server.php');}else{$host=explode(":",$argv[1]);$socket=newTSocket($host[0],$host[1]);}$transport=newTBufferedTransport($socket,1024,1024);$protocol=newTBinaryProtocol($transport);$client=new\HelloThrift\HelloServiceClient($protocol);$transport->open();echo$client->SayHello("Php-Client");$tran运动->关闭();testphpClient.php--httpPython测试加载thrift-python3模块(只测试python3,python2不会测试)pip3installthrift生成python3版本的thrift相关文件thrift-r--genpyHelloThrift.thrift此时,一个目录下生成名为gen-py的目录建立Server.py文件#!/usr/bin/python3#-*-coding:UTF-8-*-importsyssys.path.append('./gen-py')fromHelloThriftimportHelloServicefromHelloThrift.ttypesimport*fromthrift.transport导入TSocketfromthrift.transport导入TTransportfromthrift.protocol导入TBinaryProtocolfromthrift.server导入TServerclassHelloWorldHandler:def__init__(self):self.log={}defSayHello(self,user_name=""):return"Python-Server:"+user_namehandler=HelloWorldHandler()processor=HelloService.Processor(handler)transport=TSocket.TServerSocket('localhost',9091)tfactory=TTransport.TBufferedTransportFactory()pfactory=TBinaryProtocol.TBinaryProtocolFactory()server=TServer.TSimpleServer(处理器,transport,tfactory,pfactory)server.serve()建立Client.py文件#!/usr/bin/python3#-*-coding:UTF-8-*-importsyssys.path.append('./gen-py')fromHelloThriftimportHelloServicefromHelloThrift.ttypesimport*fromHelloThrift.constantsimport*fromthrift.transport导入TSocketfromthrift.transport导入TTransportfromthrift.protocol导入TBinaryProtocolhost=sys.argv[1].split(':')transport=TSocket.TSocket(host[0],host[1])transport=TTransport.TBufferedTransport(transport)protocol=TBinaryProtocol.TBinaryProtocol(transport)client=HelloService.Client(protocol)transport.open()msg=client.SayHello('Python-Client')print(msg)transport.close()测试打开新开一个窗口,运行以下命令:python3Server.phpGotestloadgo'sthriftmodulegogetgit.apache.org/thrift.git/lib/go/thrift生成go版本的thrift相关文件thrift-r--gengoHelloThrift.thrift生成Server.go文件包mainimport("./gen-go/hellothrift""git.apache.org/thrift.git/lib/go/thrift""context")const(NET_WORK_ADDR="localhost:9092")typeHelloServiceTmplstruct{}func(this*HelloServiceTmpl)SayHello(ctxcontext.Context,strstring)(sstring,errerror){return"Go-Server:"+str,nil}funcmain(){transportFactory:=thrift.NewTTransportFactory()protocolFactory:=thrift.NewTBinaryProtocolFactoryDe??fault()transportFactory=thrift.NewTBufferedTransportFactory(8192)transport,_:=thrift.NewTServerSocket(NET_WORK_ADDR)handler:=&HelloServiceTmpl{}processor:=hellothrift.NewHelloServiceProcessor(handler)服务器:=thrift.NewTSimpleServer4(processor,transport,transportFactory,protocolFactory)server.Serve()}生成Client.go文件packagemainimport("./gen-go/hellothrift""git.apache.org/thrift.git/lib/go/thrift""fmt""context""os")funcmain(){vartransportthrift.TTransportargs:=os.ArgsprotocolFactory:=thrift.NewTBinaryProtocolFactoryDe??fault()transport,_=thrift.NewTSocket(args[1])transportFactory:=thrift。NewTBufferedTransportFactory(8192)transport,_=transportFactory.GetTransport(transport)//延迟transport.Close()transport.Open()client:=hellothrift.NewHelloServiceClientFactory(transport,protocolFactor)str,_:=client.SayHello(context.Background(),"Go-Client")fmt.Println(str)}测试打开一个新窗口,运行如下命令:gorunServer.gocomprehensivetestto打开两个窗口,确保服务器是打开的gorunServer.go#localhost:9092python3Server.py#localhost:9091phpcallsgo-server,py-serverphpClient.phplocalhost:9091phpClient.phplocalhost:9092python3callsgo-server,py-serverpython3Client.pylocalhost:9091python3Client.pylocalhost:9092go调用go-server,py-servergorunClient.golocalhost:9091gorunClient.golocalhost:9092备注没测试phpsocket,go,python3http,可以花时间去做下面的代码纯拼装,没有深入理解原理,我打算写一篇理论文章和封装类库的文章供参考https://studygolang.com/articles/1120https://www.cnblogs.com/qufo/p/5607653.htmlhttps://www.cnblogs.com/lovemdx/archive/2012/11/22/2782180.htmlhttps://github.com/yuxel/thrift-例子http://thrift.apache.org/

最新推荐
猜你喜欢