http://blog.iyunv.com/images/authorship.gif 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开发者写的论文 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 to clipboardprint?
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),
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;
}
}
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
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.");
}
}
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客户端