1、添加依賴 jar
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.8.0</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version></dependency>
2、編寫IDL檔案 Hello.thrift
namespace java service.demo
service Hello {
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
3、產生代碼
thrift -o <output directory> -gen java Hello.thrift
產生代碼縮圖:
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();//設定處理器HelloImplTProcessor 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);//建立ClientHello.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) {try {//傳輸通道 - 非阻塞方式TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);//非同步IO,需要使用TFramedTransport,它將分塊緩衝讀取。TTransportFactory transportFactory = new TFramedTransport.Factory();//使用高密度二進位協議TProtocolFactory proFactory = new TCompactProtocol.Factory();//設定處理器 HelloImplTProcessor processor = new Hello.Processor(new HelloImpl());//建立伺服器TServer server = new TThreadedSelectorServer(new Args(serverTransport).protocolFactory(proFactory).transportFactory(transportFactory).processor(processor));System.out.println("Start server on port 7911...");server.serve();} catch (Exception e) {e.printStackTrace();}}
調用非阻塞IO(NIO)服務的用戶端:
public static void main(String[] args) throws Exception {//設定傳輸通道,對於非阻塞服務,需要使用TFramedTransport,它將資料分塊發送TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));transport.open();//使用高密度二進位協議TProtocol protocol = new TCompactProtocol(transport);//建立ClientHello.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("360buy");client.helloVoid();}System.out.println("耗時:" + (System.currentTimeMillis() - start));//關閉資源transport.close();}
-----------------------------------------------------------------------------------------------------------------------------------
用戶端非同步呼叫:
/** 調用[非阻塞IO]服務,非同步 */public static void main(String[] args) {try {//非同步呼叫管理器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);} catch (Exception e) {e.printStackTrace();}}
-----------------------------------------------------------------------------------------------------------------------------------
使用SSL的服務端:
調用基於SSL服務端的用戶端: