【转】Thrift入门试用
在新的项目中公司在平台内部系统间使用Thrift通讯,都没有听说过。然后听同事说,是跨语言Socket通讯的开源组件。
功能及特点
1.跨平台和语言的Socket通讯组件。
2.根据伪代码的结构语言定义对象和服务结构,然后生成各语言的代码和接口
3.各语言根据组件提供的库,编写客户端和服务器端程序。服务器端实现接口并编写业务逻辑。
4.服务器端支持多种序列化方式(Binary,Compact,JSON等)和多种服务器实现
太晚了,以后在完善,先贴代码了
本测试使用WINDOW环境和JAVA语言
1.下载和安装
下载地址:http://thrift.apache.org
下载最新版本,当前0.7.0
数据类型:
点击(此处)折叠或打开
2.建立测试工程
普通JAVA工程,目录如下:
src
+ org.acooly.thrift.demo.client 客户端代码
+ org.acooly.thrift.demo.generalcode 通过thrift工具生成的代码
+ org.acooly.thrift.demo.server 服务器端代码
lib
+拷贝前面ant编译后的build/lib下的jar和编译生成的thrift-x.x.x.jar
tools
+ thrift.exe 前面下载的
+ thriftdemo.thrift 伪代码
3.编写伪代码文件*.thrift
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) }
4.生成代码
cmd模式进入 tools目录,运行
thrift.exe -gen java thriftdemo.thrift
运行成功后,在本目录会生成gen-java目录,拷贝该目录下生成的代码到工程中对应的包。
5.服务器代码和实现业务逻辑
实现业务逻辑
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); } }
编写服务器代码
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(); } }
6.客户端代码
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(); } }
7.启动和测试运行
1.运行ThriftServer
2.运行ThriftClient
ThriftServer输出:
invoke: save,contact = Contact(id:1, name:zhangpu, birthday:1308591769148, phoneNo:1389612222, ipAddress:192.168.2.1, props:null)
invoke: remove,id = 1
ThriftClient输出:
[Contact(id:1, name:zhangpu, birthday:1308591769131, phoneNo:1389612222, ipAddress:192.168.2.1, props:null)]