Thrift是什么,看这里:http://thrift.apache.org/
1.从官网下载thrift
Thrift官网:http://thrift.apache.org/,Windows 和 Linux请分别下载不同的版本。 在Windows上,将下载的压缩文件解压后,放到一个文件夹下,并为其配置环境变量,以便以后可以直接从命令行启用它。注意:在网上找找,将libthrift-0.9.0.jar和slfj-api-1.7.2.jar一起放入我们项目的lib文件夹下,并添加到项目的buildpath中,因为thrift生成的java文件中,将会引用到这两个包中的类。
2.编写thrift文件
thrift文件是一个文本文件,文件名自定义,后缀也自定义。但是为了方便理解,建议后缀写为:.thrift。一个thrift文件(Person.thrift)像下面这样:
1namespace java com.abc.gen 2struct Person{ 3 1:i32 id; 4 2:string name; 5 3:int age; 6 4:bool married; 7} 8service PersonService { 9 Person getPersonById(1:i32 id) , 10 bool deletePersonById(1:i32 id ) 11}
这个文件中,namespace描述了生成的类的包名;struct用于定义一种结构体(在Java中叫做对象),thrift将生成这个Person对象,Person对象中定义了几个基本的属性。service用于定义“服务”,这些“服务”将以接口的方式生成在类中。我们需要做的,就是待thrift为我们生成相应的文件之后,去实现这些接口。
3.用thrift生成自定义的类
使用命令:thrift -r -gen java Person.thrift 来生成我们需要的类。
其中-r表示递归,如果这个Person.thrift文件中又引用了其他thrift文件中定义的对象,那么在生成的时候,将会把引用的对象的thrift文件中定义的所有对象一起生成。
-gen Java 表示生成的是java语言的文件,最后一个参数表示定义thrift对象和服务的文件。
4.实现生成的接口
根据以上的thrift文件,生成之后的文件有Person.java和PersonService.java,其中PersonService.java类中有一个Iface接口,这个接口就是我们需要实现的。我们把这两个生成的Java类复制到我们的项目包中待用。现在,新建一个类PersonServiceImpl,这个类实现了PersonService.Iface。这里以方法getPersonById为例,为了简单起见,直接在PersonServiceImpl的getPersonById中new一个Person对象,设置相关的属性并返回。如下:
1import org.apache.thrift.TException; 2import com.abc.gen.Person; 3import com.abc.gen.PersonService; 4public class PersonServiceImpl implements PersonService.Iface { 5 @Override 6 public Person getPersonById(int id) throws TException { 7 Person p = new Person(); 8 p.setId(id); 9 p.setAge(20); 10 p.setName("name"); 11 p.isMarried(false); 12 return p; 13 } 14 @Override 15 public boolean deletePersonById(int id) throws TException { 16 // TODO Auto-generated method stub 17 return false; 18 } 19}
其中这两个复写的接口,就是我们在Person.thrift中定义的service。
5.编写ThriftServer
1import org.apache.thrift.server.TServer; 2import org.apache.thrift.server.TThreadPoolServer; 3import org.apache.thrift.server.TThreadPoolServer.Args; 4import org.apache.thrift.transport.TServerSocket; 5import org.apache.thrift.transport.TServerTransport; 6import com.abc.gen.PersonService; 7import com.abc.impl.PersonServiceImpl; 8public class ThriftServer { 9 public static void main(String[] args) { 10 PersonService.Processor processor = new PersonService.Processor(new PersonServiceImpl()); 11 try{ 12 TServerTransport serverTransport = new TServerSocket( new InetSocketAddress("127.0.0.1",9999)); 13 Args trArgs=new Args(serverTransport); 14 trArgs.processor(processor); 15 TServer server = new TThreadPoolServer(trArgs); 16 System.out.println("Thrift服务已开启..."); 17 server.serve(); 18 server.stop(); 19 System.out.println("Thrift服务已停止..."); 20 }catch(Exception e){ 21 throw new RuntimeException("thrift服务启动失败"+"\n"+e.getMessage()); 22 } 23 } 24}
6.编写ThriftClient
1import org.apache.thrift.TException; 2import org.apache.thrift.protocol.TBinaryProtocol; 3import org.apache.thrift.protocol.TProtocol; 4import org.apache.thrift.transport.TSocket; 5import org.apache.thrift.transport.TTransport; 6import com.abc.gen.Person; 7import com.abc.gen.PersonService; 8public class ThriftClient { 9 public static void main(String[] args) throws TException { 10 TTransport transport = new TSocket("127.0.0.1",9999); 11 long start=System.currentTimeMillis(); 12 TProtocol protocol = new TBinaryProtocol(transport); 13 PersonService.Client client=new PersonService.Client(protocol); 14 transport.open(); 15 System.out.println("调用开始..."); 16 Person person = client.getPersonById(1); 17 System.out.println("调用结果:" + person.toString()); 18 transport.close(); 19 System.out.println("消耗时间:" + (System.currentTimeMillis()-start) + "ms"); 20 } 21}
7.调用
先启动ThriftServer,在启动ThriftClient,这时会看到以下内容:
1Thrift服务已开启... 2调用开始... 3调用结果:Person(id:1, name:name, age:20, married:false) 4消耗时间:115ms