Thrift Java實戰

來源:互聯網
上載者:User

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();    }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.