学习Facebook 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.pdf thrift开发者写的论文

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,

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<User> getUsers()  
}   
 

第二步,生成java和python代码

thrift --gen java user.thrift

thrift --gen py user.thrift

 

第三步,实现java服务端

参见:http://wiki.apache.org/thrift/ThriftUsageJava

服务短的处理类实现 UserServiceHandler.java

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<User> getUsers() throws TException {  
        List<User> list = new ArrayList<User>();  
        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

 
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客户端

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<User> list = client.getUsers();  
            Iterator<User> it = list.iterator();  
            while(it.hasNext()){  
                User u = it.next();  
                System.out.println("name=" + u.getName());  
            }  
            transport.close();  
        } catch (TException x) {  
            x.printStackTrace();  
        }  
    }  
}  
第五步,实现python客户端
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)  

  
第六步:测试与结果,先启动服务端的Server类中的主函数main,负责端口监听和客户端请求处理及响应,然后在客户机上运行java或python的客户端请求,服务端会通过Thrift框架响应客户端的RPC请求,并将结果返回给客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值