官网示例:
http://thrift.apache.org/tutorial/java
软件下载:
http://thrift.apache.org/download
学习教程:
http://jnb.ociweb.com/jnb/jnbJun2009.html
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.1
thrift-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(); } }
参考文章:
入门试用
http://acooly.iteye.com/blog/1098919
http://www.micmiu.com/soa/rpc/thrift-sample/
http://www.tuicool.com/articles/vAzeim
Thrift网络服务模型
http://www.it165.net/pro/html/201407/18571.html
时间: 2024-10-14 05:50:08