RPC_THRIFT使用范例

1.下载与安装

  官方网站:http://thrift.apache.org/

  下载地址:http://labs.renren.com/apache-mirror//thrift/0.6.1/thrift-0.6.1.tar.gz

  安装Thrift:

  1. cd /usr/local/src
  2. wget http://labs.renren.com/apache-mirror//thrift/0.6.1/thrift-0.6.1.tar.gz
  3. tar zxvf avro-src-1.5.1.tar.gz
  4. cd thrift-0.6.1
  5. ./configure
  6. make
  7. make install

  进入lib/java目录,执行以下命令编译并生成jar包,请确保已经安装了ant和maven.

  1. ant

  安装后,libthrift-0.6.1-snapshot.jar位于thrift-0.6.1/lib/java/build目录

 

2.消息结构与服务接口

  基本数据类型:

  1. bool: A boolean value (true or false)
  2. byte: An 8-bit signed integer
  3. i16: A 16-bit signed integer
  4. i32: A 32-bit signed integer
  5. i64: A 64-bit signed integer
  6. double: A 64-bit floating point number
  7. string: A text string encoded using UTF-8 encoding

  特殊数据类型:

  1. binary: a sequence of unencoded bytes. Translates to a Java ByteBuffer

  容器:

  1. list<type>: An ordered list of elements. Translates to a Java ArrayList
  2. set<type>: An unordered set of unique elements. Translates to a Java HashSet
  3. map<type1,type2>: A map of strictly unique keys to values. Translates to a Java HashMap

  首先需要编写一个.thrift文件,定义一个消息结构。如下是message.thrift文件内容:

  1. namespace java thrift
  2.  
  3. struct Message {
  4.     1: string name,
  5.     2: i32 type,
  6.     3: double price,
  7.     4: bool valid,
  8.     5: binary content
  9. }
  10.  
  11. service MessageService {
  12.     Message getMessage(1:Message msg)
  13. }

  其中定义了1个结构叫做Message,位于包thrift中,有5个成员name、type、price、valid、content。还定义了1个服务叫做MessageService,其中有一个getMessage方法,需要输入一个参数,类型是message,返回值类型是message。

  编写完message.thrift文件后,执行以下命令,将会gen-java目录中生成相应的java文件,类MessageService的包是thrift。

  1. thrift --gen java message.thrift

3.rpc通信实现

  Thrift会生成结构类和服务类,假设生成的类分别是Message和MessageService,MessageService中定义了服务getMessage(Message msg)。

  服务接口实现MessageServiceImpl.java:

  1. package thrift;
  2.  
  3. import org.apache.thrift.TException;
  4. import thrift.MessageService.Iface;
  5.  
  6. public class MessageServiceImpl implements Iface {
  7.  
  8.     @Override
  9.     public Message getMessage(Message msg) throws TException {
  10.     // process the message
  11.         …
  12.         return msg;
  13.     }
  14.  
  15. }

  服务端实现Server.java:

  1. package thrift;
  2.  
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.protocol.TBinaryProtocol.Factory;
  5. import org.apache.thrift.server.TServer;
  6. import org.apache.thrift.server.TThreadPoolServer;
  7. import org.apache.thrift.transport.TServerSocket;
  8.  
  9. public class Server {
  10.     private int port;
  11.  
  12.     public Server(int port) {
  13.         this.port = port;
  14.     }
  15.  
  16.     public void run() {
  17.         try {
  18.             TServerSocket serverTransport = new TServerSocket(port);
  19.             MessageService.Processor processor = new MessageService.Processor(
  20.                     new MessageServiceImpl());
  21.             Factory protFactory = new TBinaryProtocol.Factory(true, true);
  22.             TThreadPoolServer.Args args = new TThreadPoolServer.Args(
  23.                     serverTransport);
  24.             args.processor(processor);
  25.             args.protocolFactory(protFactory);
  26.             TServer server = new TThreadPoolServer(args);
  27.             server.serve();
  28.         } catch (Exception e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         if (args.length != 1) {
  35.             System.out.println("Usage: Server port");
  36.             return;
  37.         }
  38.         int port = Integer.parseInt(args[0]);
  39.         new Server(port).run();
  40.     }
  41. }

  客户端实现Client.java:

  1. package thrift;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.util.Arrays;
  5.  
  6. import org.apache.thrift.protocol.TBinaryProtocol;
  7. import org.apache.thrift.protocol.TProtocol;
  8. import org.apache.thrift.transport.TSocket;
  9. import org.apache.thrift.transport.TTransport;
  10.  
  11. public class Client {
  12.     private int port;
  13.     private String host;
  14.     private int size;
  15.     private int count;
  16.  
  17.     public Client(int port, String host, int size, int count) {
  18.         super();
  19.         this.port = port;
  20.         this.host = host;
  21.         this.size = size;
  22.         this.count = count;
  23.     }
  24.  
  25.     public long run() {
  26.         long start = 0;
  27.         long end = 0;
  28.         TTransport transport = null;
  29.         try {
  30.             transport = new TSocket(host, port);
  31.             TProtocol protocol = new TBinaryProtocol(transport);
  32.             MessageService.Client client = new MessageService.Client(protocol);
  33.             transport.open();
  34.  
  35.             Message message = new Message();
  36.             // initiate the message
  37.             …
  38.  
  39.             start = System.currentTimeMillis();
  40.             for (int i = 0; i < count; i++) {
  41.                 client.getMessage(message);
  42.             }
  43.             end = System.currentTimeMillis();
  44.             System.out.println(end - start);
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.         } finally {
  48.             if (transport != null) {
  49.                 transport.close();
  50.             }
  51.         }
  52.  
  53.         return end - start;
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         if (args.length != 4) {
  58.             System.out.println("Usage: Client host port dataSize count");
  59.             return;
  60.         }
  61.         String host = args[0];
  62.         int port = Integer.parseInt(args[1]);
  63.         int size = Integer.parseInt(args[2]);
  64.         int count = Integer.parseInt(args[3]);
  65.  
  66.         new Client(port, host, size, count).run();
  67.     }
  68. }

4.参考资料

  (1) Thrift wiki: http://wiki.apache.org/thrift/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值