首先先安裝好 thirft
1、添加依賴 jar
2、編寫idl檔案 hello.thrift
service hello {
string hellostring(1:string para)
i32 helloint(1:i32 para)
bool helloboolean(1:bool para)
void hellovoid()
string hellonull()
}
3、生成代碼
生成代碼縮略圖:
4、編寫實作類、實作hello.iface:
縮略圖:
5、編寫服務端,釋出(阻塞式io + 多線程處理)服務。
/**
* 阻塞式、多線程處理
*
* @param args
*/
@suppresswarnings({ "unchecked", "rawtypes" })
public static void main(string[] args) {
try {
//設定傳輸通道,普通通道
tservertransport servertransport = new tserversocket(7911);
//使用高密度二進制協定
tprotocolfactory profactory = new tcompactprotocol.factory();
//設定處理器helloimpl
tprocessor processor = new hello.processor(new helloimpl());
//建立伺服器
tserver server = new tthreadpoolserver(
new args(servertransport)
.protocolfactory(profactory)
.processor(processor)
);
system.out.println("start server on port 7911...");
server.serve();
} catch (exception e) {
e.printstacktrace();
}
}
6、編寫用戶端,調用(阻塞式io + 多線程處理)服務:
public static void main(string[] args) throws exception {
// 設定傳輸通道 - 普通io流通道
ttransport transport = new tsocket("localhost", 7911);
transport.open();
//使用高密度二進制協定
tprotocol protocol = new tcompactprotocol(transport);
//建立client
hello.client client = new hello.client(protocol);
long start = system.currenttimemillis();
for(int i=0; i<10000; i++){
client.helloboolean(false);
client.helloint(111);
client.hellonull();
client.hellostring("dongjian");
client.hellovoid();
system.out.println("耗時:" + (system.currenttimemillis() - start));
//關閉資源
transport.close();
現在已完成整個開發過程,超級無敵簡單。
其中服務端使用的協定需要與用戶端保持一緻。
-------------------------------------------------------------------------------------------------------------------
上面展示了普通且常用的服務端和用戶端,下面請看非阻塞io,即java中的nio:
基于非阻塞io(nio)的服務端:
public static void main(string[] args) {
//傳輸通道 - 非阻塞方式
tnonblockingservertransport servertransport = new tnonblockingserversocket(7911);
//異步io,需要使用tframedtransport,它将分塊緩存讀取。
ttransportfactory transportfactory = new tframedtransport.factory();
//設定處理器 helloimpl
tserver server = new tthreadedselectorserver(
.transportfactory(transportfactory)
調用非阻塞io(nio)服務的用戶端:
//設定傳輸通道,對于非阻塞服務,需要使用tframedtransport,它将資料分塊發送
ttransport transport = new tframedtransport(new tsocket("localhost", 7911));
client.hellostring("360buy");
-----------------------------------------------------------------------------------------------------------------------------------
用戶端異步調用:
/** 調用[非阻塞io]服務,異步 */
//異步調用管理器
tasyncclientmanager clientmanager = new tasyncclientmanager();
//設定傳輸通道,調用非阻塞io。
final tnonblockingtransport transport = new tnonblockingsocket("localhost", 7911);
//設定協定
tprotocolfactory protocol = new tcompactprotocol.factory();
//建立client
final hello.asyncclient client = new hello.asyncclient(protocol, clientmanager, transport);
// 調用服務
system.out.println("開始:" + system.currenttimemillis());
client.helloboolean(false, new asyncmethodcallback<hello.asyncclient.helloboolean_call>() {
public void onerror(exception exception) {
system.out.println("錯誤1: " + system.currenttimemillis());
}
public void oncomplete(helloboolean_call response) {
system.out.println("完成1: " + system.currenttimemillis());
try {
client.helloboolean(false, new asyncmethodcallback<hello.asyncclient.helloboolean_call>() {
public void onerror(exception exception) {
system.out.println("錯誤2: " + system.currenttimemillis());
}
public void oncomplete(helloboolean_call response) {
system.out.println("完成2: " + system.currenttimemillis());
transport.close();
});
} catch (texception e) {
e.printstacktrace();
}
});
system.out.println("結束:" + system.currenttimemillis());
thread.sleep(5000);
使用ssl的服務端:
調用基于ssl服務端的用戶端: