?更多技術干貨請戳:聽云博客
Thrift實例1
功能描述:客戶端與服務器端分別是兩個應用,先啟動服務器端,再啟動客戶端,實現執行客戶端運行服務器端的加法方法。
源碼截圖(源碼在附件中):
Java yield?客戶端:
TestThriftClientServlet:
SendRequestController:
java構造方法?Pom.xml:
服務端:
TestThriftServlet:
java thread join。ThriftServerController:
IThriftServer:由thrift工具編譯生成
? ThriftServerServiceImpl:
java thread類,
運行說明:先運行服務端TestThriftServlet,再運行客戶端TestThriftClientServlet,觀察控制臺打印出81,即:77+5=81
Thrift實例2
1、業務邏輯描述:一個系統通過用戶身份證號獲取另一個系統里有關這個用戶的積分。
2、首先編寫腳本文件test.thrift:
java實例變量?命名空間:
namespace java com.test
namespace是關鍵字表示命名空間,也就是java的包,java表示腳本轉換成java類
請求:identitycard是身份證號
struct?UserRequest
{
1:string?identitycard
}
返回:code是成功標識 0表示成功;integral是用戶擁有的積分
java多線程實例。 Params是返回的參數集合這里只用一個字段積分(integral)做例子;
struct?UserResponse
{
1:string?code
2:map<string,string>?params
}
服務:
service?ThriftCase
{
UserResponse?integralService(1:?UserRequest?request)
}
3、到thrift官網下載
http://thrift.apache.org/
下載:thrift-0.6.1.tar.gz和Thrift compiler for Windows (thrift-0.6.1.exe)
java項目實例、4、用eclips里面的ant工具把thrift-0.6.1編譯出jar包
解壓:thrift-0.6.1.tar.gz
把解壓后的文件夾放到eclipse的某個工程下如圖:
如上圖thrift-0.6.1文件夾放在名字是thrift的java工程的src下,有紅叉不必管他,然后進入到thrift-0.6.1/lib/java中,右鍵點擊build.xml
java thread interrupt?
如圖點擊Ant Build。在build.xml同路徑下會產生一個build文件夾里面有生成的thrift-0.6.1.jar包。把這個包放到所做的工程的lib里就行了。
5、把thrift腳本編譯成java類
把thrift-0.6.1.exe和test.thrift文件放在同一個目錄下并把路徑寫進環境變量Path中,如:E:/soft/ccc/路徑下放入上述兩個文件并把路徑寫進環境變量Path中。
在運行中鍵入cmd打開dos界面,進到E:/soft/ccc/路徑下,執行thrift-0.6.1.exe文件,命令如下:
java案例,E:\soft\ccc\>Thrift-0.6.1 –gen java text.thrift
在這個目錄下會產生一個gen-java文件夾,文件夾中會產生相應的java類,
ThriftCase.java
UserRequest.java
UserResponse.java
java編程思想,將三個文件拷貝到thrift工程的src/com/test/ 路徑下。
6、編寫服務端代碼
服務端代碼包含兩個類:一個是業務邏輯類,一個是socket服務類。
業務邏輯類要實現ThriftCase.Iface服務接口
package?com.test;
import?java.util.HashMap;
import?java.util.Map;
public?class?ThriftCaseImpl?implements?ThriftCase.Iface{
public?UserResponse?integralService(UserRequest?request)??{
try{
UserResponse?urp=new?UserResponse();
if(request.identitycard.equals("32010619881231103X")){
urp.setCode("0");
Map?params=new?HashMap();
params.put("integral",?"10");
urp.setParams(params);
}
System.out.print("接收參數是:identitycard="+request.identitycard);
return?urp;
}catch(Exception?e){
e.printStackTrace();
}
return?null;
}
}
Socket服務類
package?com.test;
import?org.apache.thrift.protocol.TBinaryProtocol;
import?org.apache.thrift.protocol.TBinaryProtocol.Factory;
import?org.apache.thrift.server.TServer;
import?org.apache.thrift.server.TThreadPoolServer;
import?org.apache.thrift.server.TThreadPoolServer.Args;
import?org.apache.thrift.transport.TServerSocket;
import?org.apache.thrift.transport.TTransportException;
public?class?TestService?{
private?void?start(){
try?{
TServerSocket?serverTransport?=?new?TServerSocket(8899);
ThriftCase.Processor?processor?=?new?ThriftCase.Processor(new?ThriftCaseImpl());
//TBinaryProtocol?–?二進制編碼格式進行數據傳輸。
Factory?protFactory?=?new?TBinaryProtocol.Factory(true,true);
//TCompactProtocol?這種協議非常有效的,使用Variable-Length?Quantity?(VLQ)?編碼對數據進行壓縮
//Factory?protFactory?=?new?TCompactProtocol.Factory();
Args?args?=?new?Args(serverTransport);
args.processor(processor);
args.protocolFactory(protFactory);
TServer?server?=?new?TThreadPoolServer(args);
System.out.println("Starting?server?on?port?8899?...");
server.serve();
}?catch?(TTransportException?e)?{
e.printStackTrace();
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
TestService?srv?=?new?TestService();
srv.start();
}
}
java項目開發實例,7、客戶端代碼
package?com.test;
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?org.apache.thrift.transport.TTransportException;
import?com.test.ThriftCase.Client;
public?class?TestClient?{
private?void?start()?{TTransport?transport;try?{transport?=?new?TSocket("localhost",?8899);TProtocol?protocol?=?new?TBinaryProtocol(transport);Client?client?=?new?Client(protocol);UserRequest?request=new?UserRequest();request.setIdentitycard("32010619881231103X");transport.open();com.test.UserResponse?urp=client.integralService(request);if(urp.code!=null&&!urp.code.equals("")){System.out.println("返回代碼:"+urp.code+";?參數是:"+urp.params.get("integral"));}transport.close();}?catch?(TTransportException?e)?{e.printStackTrace();}?catch?(TException?e)?{e.printStackTrace();}}public?static?void?main(String[]?args)?{TestClient?c?=?new?TestClient();c.start();}
}
8、先開啟服務
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
TestService?srv?=?new?TestService();
srv.start();
}
控制臺顯示:
Starting?server?on?port?8899?...
9、再運行客戶端
public?static?void?main(String[]?args)?{TestClient?c?=?new?TestClient();c.start();}
控制臺顯示:
返回代碼:0; 參數是:10
thrift框架?10、所需第三方包
log4j-1.2.15,
slf4j-api-1.6.1.jar
slf4j-log4j12-1.5.8.jar
?
java thread方法、原文鏈接:http://blog.tingyun.com/web/article/detail/1082