Thrift Basic Tutorial (i) installation chapter, today to introduce the next thrift encoding process, first generate the framework code from the command line. The command format is as follows: Thrift-gen language Xxx.thrift
1, first introduce the next Xxx.thrift file, we need to create a myserver.thrift file, this interface mainly defines the service interface and data format. Introduce the syntax format of the thrift file to the Myserver.thrift description I wrote.
/*** data type in thrift * bool boolean type * byte in java with byte* i16 short integer, same as int* i32 integer type, Same as int* i64 Long Integer, same long * double 64-bit floating-point number * string with Java string* binary byte array * map<t1,t2> with Java hashmap generics * list<t1> with Java ArrayList, stored data is sorted * set< T1> with Java hashset, storing data that is not repeated *//** the namespace*/namespace java com.lzf.thrift.demo1namespace php demo/** for each language defined here * Define services in the form of service service name {}, which can also inherit service from other thrift files in the following format * Service name extends aaa.aaaservice{}, need to add * include to the current file header Aaa.thrift "* Here defines a simple service *//** can define a struct, for exchanging data such as objects */struct user{ 1:string name, 2:i32 age}service helloserve{ /* * In service you can define method * Format: Return value method name (parameter position: Parameter type argument name) * /i32 adduser (1:i32 age,2:string name), Map<string,i32> getusers ()}
Service Side2. Generate Java Code thrift--gen Java Myserver.thrift generated from server java. To facilitate testing and compilation, I used idea to create a project that normally uses ant or Maven to create the thrift-0.9.0/lib/java/build/libthrift-0.9.0.jar,thrift-0.9.0 /lib/java/build/lib/*.jar Copy to your project and import it.
3, the implementation of the method we defined in the configuration file AddUser and Getusers two methods, Thrift help us generate the Helloserve.java, there is an interface, so we create a new Demohandler class, the implementation of Helloserve.java Iface, the code is as follows:
Package Com.lzf.thrift.demo1;import Org.apache.thrift.texception;import Java.util.hashmap;public class DemoHandler Implements helloserve.iface{public Demohandler () {} private static hashmap<string,integer> users = new Hashmap<> (); public int AddUser (int age, String name) throws texception{ Users.put (name,age); return 1001; } Public hashmap<string,integer> getusers () { return users; }}
Create a new Demoserver class that is used primarily for building services with the following code:
Package Com.lzf.thrift.demo1;import Org.apache.thrift.server.tserver;import Org.apache.thrift.server.TServer.Args ; Import Org.apache.thrift.server.tsimpleserver;import Org.apache.thrift.server.tthreadpoolserver;import Org.apache.thrift.transport.tssltransportfactory;import Org.apache.thrift.transport.tserversocket;import Org.apache.thrift.transport.tservertransport;import Org.apache.thrift.transport.tssltransportfactory.tssltransportparameters;import Java.lang.Exception;import Java.lang.override;import Java.lang.runnable;public class demoserver{public static Demohandler handler; public static helloserve.processor Processor; public static void Main (string[] args) {try{handler = new Demohandler (); processor = new Helloserve.processor (handler); Simple (processor); }catch (Exception x) {x.printstacktrace (); }} public static void simple (Helloserve.processor Processor) {try {TservertransportServertransport = new Tserversocket (9090); Tserver Server = new Tsimpleserver (new Args (Servertransport). Processor (processor)); System.out.println ("Starting the simple server ..."); Server.serve (); } catch (Exception e) {e.printstacktrace (); } }}
The service-side code is written.
Client1, Generate PHP client code thrift--gen PHP myserver.thrift generate PHP Framework code, create a new project to copy gen-php to the project, and thrift-0.9.0/lib/php/lib/ Thrift Copy to your project, this is the base library.
2, create the client in the project root directory new index.php, the code is as follows:
<?php/** * AUTHOR:LZF * CREATETIME:15/8/3 22:13 * Description: */require_once ' Thrift/classloader/thriftclassloader. PHP '; use thrift\classloader\thriftclassloader; $GEN _dir = Realpath (dirname (__file__)). ' /gen-php '; $loader = new Thriftclassloader (); $loader->registernamespace (' Thrift ', __dir__); $loader Registerdefinition (' Demo ', $GEN _dir); $loader->register (); Use Thrift\protocol\tbinaryprotocol;use thrift\ Transport\tsocket;use Thrift\transport\thttpclient;use Thrift\transport\tbufferedtransport;use Thrift\Exception\ texception;try {$socket = new Tsocket (' localhost ', 9090); $transport = new Tbufferedtransport ($socket, 1024, 1024); $protocol = new Tbinaryprotocol ($transport); $client = new \demo\helloserveclient ($protocol); $transport->open (); echo $client->adduser ("Lzf"); echo $client->adduser ("lzf001"); echo $client->adduser ("lzf002"); Echo ' performed the <br/> '; $userlist = $client->getusers (); Print_r ($userlist); $tranSport->close ();} catch (Texception $tx) {print ' texception: '. $tx->getmessage (). " \ n ";}
3. Test run Java server code, run directly from the IDE
Then visit index.php and the results are as follows
At this point, Thrift Basic Tutorial is finished, if there are questions to welcome the criticism of the evidence:)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Thrift Basic Course (ii) coding Chapter