官方地址:http://thrift.apache.org/
项目一直有用到thrift这东西,thrift的介绍网上也挺多的。
描述:thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。
今天写了个servlet服务器端的http请求与as3客户端请求的列子。这里做个笔记哈。
test.thrift
namespace java com.xzw.thrift
namespace as3 com.xzw.thrift
struct User{
1:i32 userId,
2:string loginName,
3:string password,
4:string name
}
exception UserNotFound{
1:string msg //as3中由于message是关键字,所以避免使用message关键字。
}
service UserService{
User getUser(1:string loginName) throws (1:UserNotFound unf),
list<User> getUsers()
}
通过命令生成java代码
>thrift --gen java test.thrift
以上命令会生成gen-java的目录拷贝里面的代码到你的项目中。如图com.xzw.thrift就是
工具生成java类。
其中我们需要去实现UserService类中的IFace接口。代码如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.xzw.service;
import com.xzw.thrift.User;
import com.xzw.thrift.UserNotFound;
import com.xzw.thrift.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.apache.thrift.TException;
/**
*
* @author xzw
*/
publicclass UserServiceHandler implements UserService.Iface{
public User getUser(String loginName) throws UserNotFound, TException {
if(!"xuzhiwei".equals(loginName)){
UserNotFound e = new UserNotFound("用户无法找到!");
throw e;
}
User user = new User();
user.setUserId(100);
user.setLoginName("xuzhiwei");
user.setPassword("123456");
user.setName("user1");
Logger.getLogger("user=>"+user.toString());
return user;
}
public List
getUsers() throws TException { List
list = new ArrayList (); User user = new User();
user.setUserId(100);
user.setLoginName("xuzhiwei");
user.setPassword("123456");
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);
Logger.getLogger("user list=>"+list.toString());
return list;
}
}
编写servlet,servlet需要继承thrift提供的类包TServlet即可,不需要去重写doGet,doPost方法。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.xzw.service;
import com.xzw.thrift.UserService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServlet;
/**
*
* @author xzw
*/
publicclass UserServlet extends TServlet{
public UserServlet(){
super(new UserService.Processor(new UserServiceHandler()),new TBinaryProtocol.Factory(true, true));
}
}
以上就完成服务器端的编写了。
可以使用junit测试一下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.xzw.thrift.User;
import com.xzw.thrift.UserService;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.junit.*;
/**
*
* @author xzw
*/
publicclass TestServlet {
@Test
publicvoid test(){
String serveltUrl = "http://localhost:8080/test_thrift/userServlet";
try {
THttpClient thc = new THttpClient(serveltUrl);
TProtocol lopFactory = new TBinaryProtocol(thc);
UserService.Client client = new UserService.Client(lopFactory);
List users = client.getUsers();
System.out.println("-->>"+users.toString());
} catch (TException ex) {
Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
publicvoid test2(){
String serveltUrl = "http://localhost:8080/test_thrift/userServlet";
try {
THttpClient thc = new THttpClient(serveltUrl);
TProtocol lopFactory = new TBinaryProtocol(thc);
UserService.Client client = new UserService.Client(lopFactory);
User user = client.getUser("xuzhiwei");
System.out.println("-->"+user.toString());
} catch (TException ex) {
Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
接下来生成as3代码
>thrift --gen as3 test.thrift
以上代码会生成gen-as3的目录,拷贝目录下的代码到你的工程中。
编写as3客户端类:
package
{
import com.xzw.thrift.User;
import com.xzw.thrift.UserNotFound;
import com.xzw.thrift.UserService;
import com.xzw.thrift.UserServiceImpl;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.text.TextField;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
public class testthrift extends Sprite
{
private const SERVER_URL:String = "http://localhost:8080/test_thrift/userServlet";
private var textField:TextField;
publicfunction testthrift()
{
init();
connServer();
}
private function init():void
{
textField = new TextField();
textField.width = 300;
textField.height = 500;
textField.autoSize = "left";
addChild(textField);
}
private function connServer():void
{ //实例化URLRequest
var urlRequest:URLRequest = new URLRequest(SERVER_URL);
//实例化THttpClient
var thc:THttpClient = new THttpClient(urlRequest);
var protocol:TProtocol = new TBinaryProtocol(thc);
var usImpl:UserService = new UserServiceImpl(protocol);
usImpl.getUser("xuzhiwei",onError,onUserSuccess);
//usImpl.getUsers(onError,onSuccess);
}
private function onError(e:UserNotFound):void{
trace(e);
}
private function onUserSuccess(user:User):void{
trace("success:");
trace(user);
textField.text = user.toString();
}
private function onSuccess(user:Array):void{
trace(user);
textField.text = user.toString();
}
}
}
thrift目前越来越多人在使用它,但是as3的一直找不到比较完整的例子。今天写了与大家分享哈。
欢迎拍砖。
参考:
http://thrift.apache.org/
http://chamibuddhika.wordpress.com/tag/apache-thrift/
http://wiki.apache.org/thrift/Thrift%20%26%20Eclipse%20%26%20JUnit%20with%20TServlet