Official Website Example:
Http://thrift.apache.org/tutorial/java
Software Download:
Http://thrift.apache.org/download
Learning Tutorials:
Http://jnb.ociweb.com/jnb/jnbJun2009.html
Comparison of thrift and other transmission modes
XML is much larger than JSON, but the XML tradition is not complicated.
JSON is small, novel, but not perfect.
Thrift volume is very small , the use of more trouble, than the former two light, but for 1. High concurrency, 2. Large data transmission, 3. Multi-lingual environment
The socket is the TCP network layer, and HTTP is the application layer
1. Writing IDL interface definition files
namespace Java org.acooly.thrift.demo.generalcode struct contact{1:i32 ID 2:string name 3:i64 birthday 4:string Phoneno 5:string ipAddress 6:map<string,string> props} service contactmanager{void Save (1:contact contact) void Remove (1:I32 ID) list<contact> getAll (); list<contact> query (1:map<string,string> conditions)}
2. Generate Code
Download windows version of thrift-0.9.1
Thrift-0.9.1.exe-r--gen Java thriftdemo.thrift java code generation
Thrift-0.9.1.exe-r--gen php thriftdemo.thrift generate PHP code
3. Implement the business logic (i.e. the method of implementing the service in the interface definition file)
package org.acooly.thrift.demo.server;import java.util.arraylist;import java.util.calendar; import java.util.list;import java.util.map;import org.acooly.thrift.demo.generalcode.contact; Import org.acooly.thrift.demo.generalcode.contactmanager;import org.apache.thrift.texception;public class contactmanagerimpl implements contactmanager.iface{ public list<contact> getall () throws TException { List<Contact> contacts = new ArrayList<Contact> (); contacts.add (New contact (1, "Zhangpu", Calendar.getinstance (). Gettimeinmillis (), "1389612222", "192.168.2.1", null)); return contacts; } public list<contact> query (Map <string, string> conDitions) throws TException { List<Contact> contacts = new ArrayList<Contact> (); contacts.add (New contact (1, "Zhangpu", Calendar.getinstance (). Gettimeinmillis (), "1389612222", " 192.168.2.1 ", null)); return contacts; } public void remove (Int id) throws texception { system.out.println ("invoke: remove,id = " + id); } public void save (Contact contact) throws texception { system.out.println ("Invoke: save,contact = " + contact); } }
4. Writing Server code
package org.acooly.thrift.demo.server;import org.acooly.thrift.demo.generalcode.contactmanager; import org.acooly.thrift.demo.generalcode.contactmanager.iface;import org.apache.thrift.protocol.tcompactprotocol;import org.apache.thrift.protocol.tcompactprotocol.factory; import org.apache.thrift.server.tserver;import org.apache.thrift.server.tsimpleserver;import Org.apache.thrift.server.tserver.args;import org.apache.thrift.transport.tserversocket;public class thriftserver { public static void main (String[] args) throws exception{ tserversocket serversocket = new tserversocket (8111); Contactmanager.processor<iface> processor = new contactmanager.processor<iface > (New contactmanagerimpl ());   Factory factory = new tcompactprotocol.factory (); args ag = new args (ServerSocket); ag.outputprotocolfactory (Factory); Ag.inputprotocolfactory (Factory); ag.processor (processor); tserver server = new tsimpleserver (AG); server.serve (); } }
5. Writing Client code
Package org.acooly.thrift.demo.client;import java.util.calendar;import java.util.list;import org.acooly.thrift.demo.generalcode.contact;import org.acooly.thrift.demo.generalcode.contactmanager ;import org.apache.thrift.protocol.tcompactprotocol;import org.apache.thrift.protocol.tprotocol; Import org.apache.thrift.transport.tsocket;import org.apache.thrift.transport.ttransport;public class thriftclient { public static void main (String[] args) throws Exception{ ttransport transport = new tsocket ("localhost", 8111); tprotocol protocol = new tcompactprotocol ( transport); contactmanager.client client = new contactmanager.client (protocol), &Nbsp; transport.open (); list<contact> list = client.getall ( ); system.out.println (list); client.save (New Contact (1, "Zhangpu", Calendar.getinstance (). Gettimeinmillis (), "1389612222", "192.168.2.1", null)); client.remove (1); transport.close (); }}
Reference article:
Getting Started trial
http://acooly.iteye.com/blog/1098919
http://www.micmiu.com/soa/rpc/thrift-sample/
Http://www.tuicool.com/articles/vAzeim
Thrift Network Service Model
Http://www.it165.net/pro/html/201407/18571.html
This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1720976
Thrift Java Combat