4rfdf 发表于 2015-8-3 10:13:58

[zz]Apache Thrift学习小记

  参考:
  http://incubator.apache.org/thrift/
  http://wiki.apache.org/thrift/FrontPage
  
  http://jnb.ociweb.com/jnb/jnbJun2009.html非常好的入门教程
  http://developers.facebook.com/thrift/thrift-20070401.pdfthrift开发者写的论文
  http://wiki.apache.org/thrift/ThriftUsageC%2B%2B官方的c++文档
  
  
  Thrift是个啥东东?
  来自wiki.apache.org/thrift/FrontPage的定义
  
  Thrift is a software framework for scalable cross-language services development.
  Thrift是为了实现跨语言服务访问的一个框架
  
  Thrift allows you to define data types and service interfaces in a simple definition file.
  Thrift定义了数据和服务的描述方式,是一种IDL
  
  Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages.
  写一个定义文件,就可以使用thrift来生成某种语言RPC客户端和服务端程序框架。你只需要考虑如何实现你的服务就可以了。并且它支持很多种语言。
  
  这有点像web service, 定义好一个web service服务描述文件后,可以使用如axis等工具生成服务器端或客户端的框架程序。
  
  为什么还需要Thrift
  thrift-20070401.pdf中有解释。
  1、多语言开发的需要
  比如其中提到的搜索服务,LAMP本身没有这个功能,开发者可能使用C++开发,php如何访问这个服务呢?于是需要有一种高效的跨语言访问的方法。
  2、性能问题
  web service也可以实现多语言互访问的功能,但xml文件太大,性能不行。Thrift可以使用二进值的格式。
  
  开始测试
  实现一个简单的服务,就是根据loginName得到User, user中有userId,loginName, name, password
  
  第一步,写Thrift IDL文件 ,user.thrift,
  



view plaincopy
[*]namespace java mytest.thrift.gen
[*]namespace py mytest.thrift
[*]struct User {
[*]1: i32    userId,
[*]2: string loginName,
[*]3: string password,
[*]4: string name
[*]}
[*]exception UserNotFound {
[*]1: string message
[*]}
[*]service UserService {
[*]User getUser(1:string loginName) throws (1:UserNotFound unf),
[*]list getUsers()
[*]}
  
  
  第二步,生成java和python代码
  thrift --gen java user.thrift
  thrift --gen py user.thrift
  
  第三步,实现java服务端
  参见:http://wiki.apache.org/thrift/ThriftUsageJava
  
  服务实现 UserServiceHandler.java
  
  



view plaincopy
[*]package myserver;
[*]import java.util.ArrayList;
[*]import java.util.List;
[*]import mytest.thrift.gen.User;
[*]import mytest.thrift.gen.UserNotFound;
[*]import mytest.thrift.gen.UserService;
[*]import org.apache.thrift.TException;
[*]public class UserServiceHandler implements UserService.Iface {
[*]    @Override
[*]    public User getUser(String loginName) throws UserNotFound, TException {
[*]      if (!"login1".equals(loginName)) {
[*]            UserNotFound e = new UserNotFound("User not Found!");
[*]            throw e;
[*]      }
[*]      User user = new User();
[*]      user.setUserId(100);
[*]      user.setLoginName("login1");
[*]      user.setPassword("pwd1");
[*]      user.setName("user1");
[*]      return user;
[*]    }
[*]    @Override
[*]    public List getUsers() throws TException {
[*]      List list = new ArrayList();
[*]      User user = new User();
[*]      user.setUserId(100);
[*]      user.setLoginName("login1");
[*]      user.setPassword("pwd1");
[*]      user.setName("user1");
[*]      list.add(user);
[*]      User user2 = new User();
[*]      user2.setUserId(200);
[*]      user2.setLoginName("login2");
[*]      user2.setPassword("pwd2");
[*]      user2.setName("user2");
[*]      list.add(user2);
[*]      return list;
[*]    }
[*]}
  
  
  主程序 Server.java
  
  



view plaincopy
[*]package myserver;
[*]import mytest.thrift.gen.UserService;
[*]import org.apache.thrift.server.TServer;
[*]import org.apache.thrift.server.TSimpleServer;
[*]import org.apache.thrift.transport.TServerSocket;
[*]import org.apache.thrift.transport.TServerTransport;
[*]public class Server {
[*]    public static void main(String[] args) {
[*]      try {
[*]            UserServiceHandler handler = new UserServiceHandler();
[*]            UserService.Processor processor = new UserService.Processor(handler);
[*]            TServerTransport serverTransport = new TServerSocket(9090);
[*]            TServer server = new TSimpleServer(processor, serverTransport);
[*]            // Use this for a multithreaded server
[*]            // server = new TThreadPoolServer(processor, serverTransport);
[*]            System.out.println("Starting the server...");
[*]            server.serve();
[*]      } catch (Exception x) {
[*]            x.printStackTrace();
[*]      }
[*]      System.out.println("done.");
[*]    }
[*]}
  
  
  第四步,实现java客户端
  



view plaincopy
[*]package myclient;
[*]import java.util.Iterator;
[*]import java.util.List;
[*]import mytest.thrift.gen.User;
[*]import mytest.thrift.gen.UserNotFound;
[*]import mytest.thrift.gen.UserService;
[*]import org.apache.thrift.TException;
[*]import org.apache.thrift.protocol.TBinaryProtocol;
[*]import org.apache.thrift.protocol.TProtocol;
[*]import org.apache.thrift.transport.TSocket;
[*]import org.apache.thrift.transport.TTransport;
[*]public class Client {
[*]    public static void main(String[] args) {
[*]      try {
[*]            TTransport transport = new TSocket("localhost", 9090);
[*]            TProtocol protocol = new TBinaryProtocol(transport);
[*]            UserService.Client client = new UserService.Client(protocol);
[*]            transport.open();
[*]            System.out.println("test1");
[*]            try {
[*]                User user1 = client.getUser("login1");
[*]                System.out.println("name=" + user1.getName());
[*]            } catch (UserNotFound e) {
[*]                System.out.println(e.getMessage());
[*]            }
[*]            
[*]            System.out.println("test2");
[*]            try {
[*]                User user2 = client.getUser("login10");
[*]                System.out.println("name=" + user2.getName());
[*]            } catch (UserNotFound e) {
[*]                System.out.println(e.getMessage());
[*]            }
[*]            
[*]            System.out.println("test3");
[*]            List list = client.getUsers();
[*]            Iterator it = list.iterator();
[*]            while(it.hasNext()){
[*]                User u = it.next();
[*]                System.out.println("name=" + u.getName());
[*]            }
[*]            transport.close();
[*]      } catch (TException x) {
[*]            x.printStackTrace();
[*]      }
[*]    }
[*]}
  
  

第五步,实现python客户端


view plaincopy
[*]from mytest.thrift import UserService
[*]from mytest.thrift.ttypes import UserNotFound
[*]from thrift import Thrift
[*]from thrift.transport import TSocket
[*]from thrift.transport import TTransport
[*]from thrift.protocol import TBinaryProtocol
[*]try:
[*]    # Make socket
[*]    transport = TSocket.TSocket('localhost', 9090)
[*]    # Buffering is critical. Raw sockets are very slow
[*]    transport = TTransport.TBufferedTransport(transport)
[*]    # Wrap in a protocol
[*]    protocol = TBinaryProtocol.TBinaryProtocol(transport)
[*]    # Create a client to use the protocol encoder
[*]    client = UserService.Client(protocol)
[*]    # Connect!
[*]    transport.open()
[*]    try:   
[*]      user1 = client.getUser("login1")
[*]      print user1.name
[*]    except UserNotFound, io:
[*]      print '%r' % io
[*]    # Close!
[*]    transport.close()
[*]except Thrift.TException, tx:
[*]    print '%s' % (tx.message)
页: [1]
查看完整版本: [zz]Apache Thrift学习小记