Thrift Java實戰
Thrift與其他傳輸方式的比較xml與JSON相比體積太大,但是xml傳統,也不算複雜。json體積較小,新穎,但不夠完善。thrift體積超小,使用起來比較麻煩,不如前兩者輕便,但是對於1.高並發、2.資料轉送量大、3.多語言環境 socket是tcp網路層,http是應用程式層 1.編寫IDL介面定義檔案
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.產生代碼下載windows版本的thrift-0.9.1thrift-0.9.1.exe -r --gen java thriftdemo.thrift 產生java 代碼thrift-0.9.1.exe -r --gen php thriftdemo.thrift 產生php代碼 3.實現商務邏輯(也就是實現介面定義檔案中service的方法)
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.編寫伺服器代碼
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.編寫用戶端代碼
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); 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(); }}