ubuntu下Thrift快速入門

1、thrift簡介

2、thrift安裝

(1)安裝一些必要庫

sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

對於其餘系統,如centos、mac、windows,安裝要求見下面官網教程:php

http://thrift.apache.org/docs/install/java

(2)下載

http://thrift.apache.org/download 我下載的版本是0.9.1linux

(3)解壓安裝

解壓後,進入根目錄,輸入:sql

./configure && make

若是最後輸出一個ERROR,說 "libcrypto required",多是須要安裝apache

libssl - dev的包vim

sudo apt-get install libssl-dev

若是輸入thrift   -version,提示說thrift-compiler沒有安裝,怎安裝上。windows

sudo apt-get install thrift-compiler

安裝完compiler後,就能夠開始編寫thrift文件了。centos

3、thrift基本語法和架構

一、Thrift基礎架構

Thrift 是一個服務端和客戶端的架構體系,從我我的的感官上來看 Thrift 是一個相似XML-RPC+Java-to- IDL+Serialization Tools=Thrift 的東東,Thrift 具備本身內部定義的傳輸協議規範(TProtocol)和傳輸數據標準(TTransports),經過 IDL 腳本對傳輸數據的數據結構(struct) 和傳輸數據的業務邏輯(service)根據不一樣的運行環境快速的構建相應的代碼,而且經過本身內部的序列化機制對傳輸的數據進行簡化和壓縮提升高併發、 大型系統中數據交互的成本,下圖描繪了 Thrift 的總體架構,分爲 6 個部分:ruby

(1).你的業務邏輯實現(You Code)服務器

(2).客戶端和服務端對應的 Service

(3).執行讀寫操做的計算結果

(4).TProtocol

(6).底層 I/O 通訊

圖 中前面 3 個部分是 1.你經過 Thrift 腳本文件生成的代碼,2.圖中的褐色框部分是你

根據生成代碼構建的客戶端和處理器的代碼,3.圖中紅色的部分是 2 端產生的計算結

果。從 TProtocol 下面 3 個部分是 Thrift 的傳輸體系和傳輸協議以及底層 I/O 通訊,

Thrift 而且提供 堵塞、非阻塞,單線程、多線程的模式運行在服務器上,還能夠配合

服務器/容器一塊兒運行,能夠和現有 JEE 服務器/Web 容器無縫的結合。

二、數據類型

A、Base Type :基本類型

(1)bool:布爾類型(true或false)

(2)byte:8位有符號整數

(3)i16:16位有符號整數

(4)i32:32位有符號整數

(5)i64:64位有符號整數

(6)double:64位浮點數

(7)string:文本字符串,使用UTF-8編碼

B、Container:容器:如list、set、map

C、Struct:結構體類型

以下面的一個例子:

struct People {

1:string name;

}

struct和C裏面的結構體很相似。

D、Exception:異常類型

E、Service:定義對象的接口和一系列方法

以下面一個例子:

service  DemoService {

string getUserName(1:i32 id);

i32 add(1:i32 num1, 2:i32 num2);

}

一個service對應一個類,裏面是一些方法。

如getUserName方法就是根據一個id返回一個字符串,add方法是對num1和num2進行求和。

三、協議

Thrift能夠在kehudaun和服務端之間傳輸選擇通訊協議,協議整體上分爲文本(text)和二進制(binary)傳輸協議。

* TBinaryProtocol – 二進制編碼格式進行數據傳輸。

* TCompactProtocol – 這種協議很是有效的,使用 Variable-Length Quantity(VLQ) 編碼對數據進行壓縮。

* TJSONProtocol – 使用 JSON 的數據編碼協議進行數據傳輸。

* TSimpleJSONProtocol – 這種節約只提供 JSON 只寫的協議,適用於經過腳本語言解析

* TDebugProtocol – 在開發的過程當中幫助開發人員調試用的,以文本的形式展示方便閱讀。

四、傳輸層

* TSocket- 使用堵塞式 I/O 進行傳輸,也是最多見的模式。

* TFramedTransport- 使用非阻塞方式,按塊的大小,進行傳輸,相似於 Java 中的NIO。

* TFileTransport- 顧名思義按照文件的方式進程傳輸,雖然這種方式不提供 Java 的實現,可是實現起來很是簡單。

* TMemoryTransport- 使用內存 I/O,就比如 Java 中的 ByteArrayOutputStream實現。

* TZlibTransport- 使用執行 zlib 壓縮,不提供 Java 的實現。

五、服務端類型

* TSimpleServer - 單線程服務器端使用標準的堵塞式 I/O。

* TThreadPoolServer - 多線程服務器端使用標準的堵塞式 I/O。

* TNonblockingServer – 多線程服務器端使用非堵塞式 I/O,而且實現了 Java 中的NIO 通道。

 

4、thrift簡單例子

一、建立thrift文件

vim   demo.thrift
namespace java com.demo

service DemoService{

//根據id獲取用戶名 string getNameById(1:i32 id) }

namespace在java裏面就是對應package,這裏 namespace   java  com.demo,第二個關鍵字表示使用的語言

一個service會對應生成一個類文件

二、編譯建立代碼

thrift  -r   --gen  java  demo.thrift

接着生成了一個gen-java文件夾,經過tree gen-java查看文件夾的結構:

三、建立eclipse工程

(1)建立一個java project,而後添加以前編譯thrift生成的必要jar包,這裏,因爲咱們使用的是java語言,就到

/thrift-0.9.1/lib/java/build/lib和/thrift-0.9.1/lib/java/build/下添加jar包。

(2)將前面生成的DemoService.java複製到工程裏

(3)建立服務端

建立server的實現類:ServerImpl.java

package com.server;

import org.apache.thrift.TException; import com.demo.*; public class ServerImpl implements DemoService.Iface {  @Override  public String getNameById(int id) throws TException {     System.out.println("Your username is wgc");   return "wgc";  } }

建立Server啓動類:Server.java

package com.server;

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; import org.apache.thrift.transport.TTransportException; import com.demo.DemoService; public class Server {   public static final int SERVER_PORT = 8090;  public void startServer() throws TTransportException{   System.out.println("Server start ....");    TProcessor tprocessor = new DemoService.Processor<DemoService.Iface>(new ServerImpl());    TServerSocket serverTransport = new TServerSocket(SERVER_PORT);    TServer.Args tArgs = new TServer.Args(serverTransport);  tArgs.processor(tprocessor);  tArgs.protocolFactory(new TBinaryProtocol.Factory());  TServer server = new TSimpleServer(tArgs);  server.serve();  }   public static void main(String[] args) throws TTransportException {   Server server = new Server();   server.startServer();  } }

(4)建立客戶端

建立Client.java:

package com.client;

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 com.demo.DemoService; public class Client {   public static final String SERVER_IP = "localhost";  public static final int SERVER_PORT = 8090;  public static final int TIMEOUT = 30000;  public void startClient( ) throws TException {   TTransport transport = null;     transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);   // 協議要和服務端一致   TProtocol protocol = new TBinaryProtocol(transport);   // TProtocol protocol = new TCompactProtocol(transport);   // TProtocol protocol = new TJSONProtocol(transport);   DemoService.Client client = new DemoService.Client(protocol);   transport.open();   String result = client.getNameById(1);    }   public static void main(String[] args) throws TException {   Client client = new Client();   client.startClient();  } }

(5)測試程序

先啓動Server,再啓動客戶端,它會在終端輸出:

Server start ....

Your username is wgc

到這裏,一個簡單的客戶端、服務端程序就實現了。

(6)工程目錄結構

參考文獻:

(1) http://roclinux.cn/?p=3316

(2) http://www.dataguru.cn/forum.php?mod=viewthread&tid=219377

(3) http://wenku.baidu.com/link?url=x8Q7r1UtaFAu45HbP06pigiM_SmFTZbpQdAfk7fvp4--s70fWHBFQgte5pIgNpNJGlKOl3l2BvDthvBTCHsE3G3jfEQk5q3FT7bASItwrle

相關文章
相關標籤/搜索