thrift是一個跨語言服務的軟件框架,它能進行不同語言間進行通信。
首先通過一個簡單的案例說明每個類的作用。
我們知道對於RPC框架無非有一下三個部分:
第一個部分:通信協議
在不同機器進行通信時,通信各方必須遵守同樣的規則,才能進行通信,對於thrift,爲了與編程語言無關,則制定了利用.thrift文件作爲通信規則。
第二個部分:服務端
第三個部分:客戶端
案例如下:
thrift文件:UserService.thrift(用於提供獲取所有用戶的服務)
namespace java cn.stq.thrift
include "../Ex.thrift"
service UserService {
list<map<string,string>> getUser() throws (1:Ex.thriftDataException dataEx,2:Ex.thriftBusinessException businessEx,3:Ex.thriftSystemException systemEx)
}
服務的實現:UserServiceImpl.java
package cn.stq.thrift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.thrift.TException;
import cn.ruida.sms.portal.domain.User;
import cn.stq.thrift.exception.thriftBusinessException;
import cn.stq.thrift.exception.thriftDataException;
import cn.stq.thrift.exception.thriftSystemException;
public class UserServiceImpl implements UserService.Iface{
public List<Map<String, String>> getUser() throws thriftDataException,
thriftBusinessException, thriftSystemException, TException {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Map<String,String>map = new HashMap<String, String>();
map.put("loginName", "zhangsan");
map.put("birthday", "2014-01-01");
map.put("realName", "張三");
list.add(map);
return list;
}
private List<Map<String, String>> getUser(List<User> list,
List<Map<String, String>> users) {
for(User user:list){
if(user==null){
continue;
}
Map<String,String>map = new HashMap<String, String>();
map.put("loginName", user.getLoginName());
map.put("birthday", user.getBirthday());
map.put("realName", user.getRealName());
users.add(map);
}
return users;
}
}
客戶端實現:
package cn.stq.thrift;
import java.util.List;
import java.util.Map;
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;
import cn.stq.thrift.exception.thriftBusinessException;
import cn.stq.thrift.exception.thriftDataException;
import cn.stq.thrift.exception.thriftSystemException;
public class Client {
public static void main(String[] args) throws thriftDataException, thriftBusinessException, thriftSystemException, TException {
TTransport ts = new TSocket("192.168.1.109", 9999);
TProtocol p = new TBinaryProtocol(ts);
UserService.Client client = new UserService.Client(p);
ts.open();
List<Map<String, String>> user = client.getUser();
for(Map<String,String>map:user){
if(map==null){
continue;
}
System.out.println("真實姓名:"+map.get("realName")+",登錄名:"+map.get("loginName")+",出生日期:"+map.get("birthday"));
}
}
}
服務端:
package cn.stq.thrift;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
public class Server {
public static void startServer()throws Exception{
//步驟一:創建TProcessor
TProcessor processor = new UserService.Processor<UserService.Iface>(new UserServiceImpl());
//步驟二:創建Socket
TServerSocket serverSocket = new TServerSocket(9999);
TServer.Args tAgrs = new TServer.Args(serverSocket);
tAgrs.processor(processor);
tAgrs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tAgrs);
//啓動服務
System.out.println("服務啓動.....");
server.serve();
}
public static void main(String[] args) throws Exception{
startServer();
}
}
這個案例簡單的實現了thrift。詳細介紹如下:
首先介紹一下創建一個thrift RPC的步驟:
第一步:我們首先要編寫thrift文件,定義服務的接口
第二步:服務端的開發步驟:
1)實現服務的接口
2)創建TProcessor
3)創建TServerTransport
4)創建TProtocol
5)創建TServer
6)啓動Server
第三步:客戶端的開發步驟:
1)創建Transport
2)創建TProtocol
3)獲取Client
4)調用Client相應的方法
看到上面的例子,是不是對thrift框架產生了興趣,thrift框架讓我們無需關心底層的通信,客戶端調用服務端的服務對我們來說是透明的。接下來就進入thrift的世界。