Use Java to do thrift Server, PHP to do thrift client Simple example

Source: Internet
Author: User
Tags server port

Note :

1). Need to be aware of the package and path problems should be changed to their local

2) See http://pan.baidu.com/s/1qW8xI0k for detailed information

Here take Hello.thrift as an example

namespace Java Hellodemo namespace php  hellodemoservice hello{      string hellostring (1:string  para)      i32 helloint (1: i32 para)       bool Helloboolean (1:bool  para)      void  hellovoid ()       string hellonull ()  

1. thirft-r--gen php hello.thrift by command to generate the corresponding PHP file, through the command thirft-r--gen java hello.thrift Health into the corresponding Java file

2. Write the service side (with Java two files, run Helloserviceserver

Importorg.apache.thrift.TException;ImportHellodemo. Hello; Public classHelloserviceimplImplementshello.iface{ Public BooleanHelloboolean (BooleanParathrowstexception{returnpara; }     Public intHelloint (intParathrowstexception{Try{Thread.Sleep (1); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnpara; }         PublicString Hellonull ()throwstexception{return NULL; }         PublicString hellostring (string para)throwstexception{returnpara; }         Public voidHellovoid ()throwstexception{System.out.println ("Hello world!"); }}
ImportHellodemo. Hello;ImportOrg.apache.thrift.TProcessor;ImportOrg.apache.thrift.protocol.TBinaryProtocol;Importorg.apache.thrift.protocol.TBinaryProtocol.Factory;ImportOrg.apache.thrift.server.TServer;ImportOrg.apache.thrift.server.TSimpleServer;ImportOrg.apache.thrift.server.TThreadPoolServer;ImportOrg.apache.thrift.server.TThreadPoolServer.Args;ImportOrg.apache.thrift.transport.TServerSocket;ImportOrg.apache.thrift.transport.TServerTransport;Importorg.apache.thrift.transport.TTransportException; Public classHelloserviceserver {/*** Start Thrift Server *@paramargs*/     Public Static voidMain (string[] args) {Try{        //set the server port to 7911Tserversocket Servertransport =NewTserversocket (7911); //set the Protocol factory to Tbinaryprotocol.factoryFactory profactory =Newtbinaryprotocol.factory (); //implementation of correlation processor and Hello serviceTprocessor processor =NewHello.processorNewHelloserviceimpl ()); Tserver.args Targs=NewTserver.args (Servertransport);        Targs.processor (processor);        Targs.protocolfactory (profactory); //using TsimpleserverTserver Server =NewTsimpleserver (Targs); System.out.println ("Start server on port 7911 ....");        Server.serve (); //server.stop ();}Catch(ttransportexception e) {e.printstacktrace (); }                /*try{//Set server port to 7911 tserversocket servertransport = new Tserversocket (7911);            Set the Protocol factory to tbinaryprotocol.factory Factory profactory = new Tbinaryprotocol.factory ();            The implementation of the associated processor and the Hello service Tprocessor processor = new Hello.processor*/    }}

3. Writing the client (in PHP)

<?PHP/** * Modify to the corresponding directory*/error_reporting(E_all);Set_time_limit(0 );$root= ' E:\www\www\test\lib ';//# Specify the library directory, which can be either an absolute path or a relative pathrequire_once $root.‘ \thrift\classloader\thriftclassloader.php ';require_once' E:\www\www\test\gen-php\hellodemo\Hello.php ';require_once' E:\www\www\test\gen-php\hellodemo\Types.php ';  UseThrift\classloader\thriftclassloader;  UseThrift\protocol\tbinaryprotocol;  UseThrift\transport\tsocket; Usehellodemo\helloclient; Usethrift\factory\ttransportfactory; UseThrift\transport\tbufferedtransport;$GEN _dir=$root.‘ \gen-php '; $loader=NewThriftclassloader ();$loader->registernamespace (' Thrift ',$root);//# Load Thrift$loader->registerdefinition (' Idata\factory\meta_node ',$GEN _dir);//# Load your own write-thrift file and compile the data definition of the class file$loader-Register (); Try {       $thrif _server_url= ' localhost '; //$tt =new ttransportfactory ();    $transport=NewTsocket ($thrif _server_url, 7911); //$transport = new Tbufferedtransport ($socket, 1024x768, 1024x768);    $protocol=NewTbinaryprotocol ($transport); $client=NewHelloclient ($protocol) ; //$protocol = new tbinaryprotocolaccelerated ($transport);     $transport-open (); //$client = new Helloclient ($protocol),//var_dump ($client);d ie ();     Helloclient ($protocol);  Var_dump ($client); $x = $client->hellovoid (); $x = $client->helloint (1); $x=$client->hellostring ("Hello World, China"); Var_dump($x); $transport-Close (); } Catch(Exception $e) {      Echo $e-GetMessage (); }  

4. Running the service end and customer service observation results

http://pan.baidu.com/s/1qW8xI0k

Use Java to do thrift Server, PHP to do thrift client Simple example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.