相关资料参考链接:
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请求,并将结果返回给客户端。
分享到:
相关推荐
Thrift: Scalable Cross-Language Services Implementation
Facebook上的大多数服务都是使用Thrift for RPC编写的,某些存储系统使用Thrift来序列化磁盘上的记录。 Facebook Thrift不是的发行版。 这是Thrift的内部分支机构,Facebook于2014年2月重新发布到开源社区。...
阅读Thrift白皮书,将主要内容整理成了中文文档。
thrift属于facebook.com技术核心框架之一,使用不同开发语言开发的系统可以通过该框架实现彼此间的通讯,开发者只需编辑一份thrift脚本,即可自动获得其它开发语言的代码(比如 c++ java python ruby c# haskell ...
fackbook实现rpc协议,thrift
thrift入门学习教程
这是对Facebook开源框架Thrift的分享,并且附有示例代码。
一个例子c++php通讯的thrift例子。使用了facebook的thrift。学习一下。
Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨...
thrift-service-framework基于facebook thrift开源项目的服务框架,可同时供:后台服务相互以rpc方式调用,android/ios http方式调用,html5移动应用直接js调用###支持rpc调用支持常规的tcp/ip协议的rpc调用###支持...
NULL 博文链接:https://onlyor.iteye.com/blog/1700777
thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码...
thrift 安装包。
Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...
Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝...
thrift delphi的实例测试,适用于学习。知识在于共享 。
网上一直搜不到Windows下QT使用thrift的例子,自己整了个 QT版本 5.8.0 Boost版本 boost_1_61_0 Thrift版本 thrift-0.10.0
使用wireshark抓取thrift协议接口调用
thrift官方代码+与dubbo集成支持原生thrift协议
thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...