分享

基于Thrift WebService实现的JavaSe项目

问题导读:
1.如何编写一个book.thrift文件?
2.如何通过编译工具生成一个Java文件?
3.服务端与客户端你认为该如何实现?






Thrift 之前属于facebook,2007年提交给Apache。目前版本为0.9.1,采用Ant管理。在成功的构建之后会有libthrift-0.9.1.jar 。是其java实现。Thrift的诱人之处在于跨平台,类http的rmp协议在性能上有明显的优势。

除了libthrift-0.9.1.jar 外,在Windows下开发还需要thrift-0.9.1.exe编译工具。

现在完成一个简单的Java标准项目,其中包含其服务端和客户端。

1.编写一个book.thrift文件,这是一个使用接口定义语言 (interface definition language,IDL) 来定义数据类型和服务的文件。

  1. namespace java com.oran.thrift.dao
  2. service BookService{
  3.         string buyBook(1:string bookName)
  4. }
复制代码

2.使用编译工具生成java文件,命令为:
  1. thrift-0.9.1.exe -r -gen java book.thrift
复制代码

生成的Java文件缩略图为:
20140612191251062.jpg

将这个接口文件拷贝到对应的package下面。

3.实现接口BookService.Iface:


  1. public class BookServiceImpl implements com.oran.thrift.dao.BookService.Iface {
  2.         @Override
  3.         public String buyBook(String bookName) throws TException {
  4.                 System.out.println("bookName = " + bookName);
  5.                 return bookName+" 我买了...";
  6.         }
  7. }
复制代码

现在开始应用这个实现.




4.服务端实现:

  1. package com.oran.test.main;
  2. import org.apache.thrift.TProcessor;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.server.TServer;
  5. import org.apache.thrift.server.TSimpleServer;
  6. import org.apache.thrift.transport.TServerSocket;
  7. import org.apache.thrift.transport.TTransportException;
  8. import com.oran.thrift.dao.BookService;
  9. import com.oran.thrift.dao.impl.BookServiceImpl;
  10. public class RunServer {
  11.         private static final int SERVER_PORT = 8090;
  12.        
  13.         public static void main(String[] args) throws TTransportException {
  14.                
  15.                 System.out.println("BookService is running...");
  16.                 TProcessor processor = new BookService.Processor<BookService.Iface>(new BookServiceImpl());
  17.                 TServerSocket serverSocket = new TServerSocket(SERVER_PORT);
  18.                 TServer.Args tArgs = new TServer.Args(serverSocket);
  19.                 tArgs.processor(processor);
  20.                 tArgs.protocolFactory(new TBinaryProtocol.Factory());
  21.                
  22.                 TServer tServer = new TSimpleServer(tArgs);
  23.                 tServer.serve();
  24.         }
  25. }
复制代码

5.客户端实现:
  1. package com.oran.test.main;
  2. import org.apache.thrift.protocol.TBinaryProtocol;
  3. import org.apache.thrift.protocol.TProtocol;
  4. import org.apache.thrift.transport.TSocket;
  5. import org.apache.thrift.transport.TTransport;
  6. import com.oran.thrift.dao.BookService;
  7. public class RunClient {
  8.         public static final String SERVER_IP = "localhost" ;
  9.         public static final int SERVER_PORT = 8090 ;
  10.         public static final int TIMEOUT = 30000 ;
  11.        
  12.         public static void main(String[] args) {
  13.                 TTransport transport = null;
  14.                 try{
  15.                         transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
  16.                         TProtocol protocol = new TBinaryProtocol(transport);
  17.                         BookService.Client client = new BookService.Client(protocol);
  18.                         transport.open();
  19.                         String result = client.buyBook("老人与海");
  20.                         System.out.println("THRIFT RETURN = "+result);
  21.                 }catch(Exception e){
  22.                        
  23.                 }
  24.         }
  25. }
复制代码

OK,代码部分完毕。

  1. <!-- 服务端结果 -->
  2. BookService is running...
  3. <!-- 客户端结果 -->
  4. THRIFT RETURN = 老人与海 我买了...
复制代码








没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条