Thrift入門

簡介

Thrift最初由Facebook研發,主要用於各個服務之間的RPC通訊,支持跨語言,經常使用的語言好比C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml都支持。Thrift是一個典型的CS(客戶端/服務端)結構,客戶端和服務端能夠使用不一樣的語言開發。既然客戶端和服務端能使用不一樣的語言開發,那麼必定就要有一種中間語言來關聯客戶端和服務端的語言,沒錯,這種語言就是IDL(Interface Description Language)。
html

下載配置

下載地址
windows 下面下載exe,thrift在linux下面也有對應的安裝方式。將thrift-0.9.3.exe 下載下來重命名爲thrift.exe,並拷貝到windows--->system32裏面。
java

Maven artifact

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.9.3</version>
</dependency>

GIT Checkout

git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
cd thrift

基本概念

1.數據類型linux

  • bool:布爾值,true 或 false,對應 Java 的 boolean
  • byte:8 位有符號整數,對應 Java 的 byte
  • i16:16 位有符號整數,對應 Java 的 short
  • i32:32 位有符號整數,對應 Java 的 int
  • i64:64 位有符號整數,對應 Java 的 long
  • double:64 位浮點數,對應 Java 的 double
  • string:utf-8編碼的字符串,對應 Java 的 String
    結構體類型:
  • struct:定義公共的對象,相似於 C 語言中的結構體定義,在 Java 中是一個 JavaBean
    容器類型:
  • list:對應 Java 的 ArrayList
  • set:對應 Java 的 HashSet
  • map:對應 Java 的 HashMap
    異常類型:
  • exception:對應 Java 的 Exception
    服務類型:
  • service:對應服務的類

2.服務端編碼基本步驟:git

  • 實現服務處理接口impl
  • 建立TProcessor
  • 建立TServerTransport
  • 建立TProtocol
  • 建立TServer
  • 啓動Server

3.客戶端編碼基本步驟:apache

  • 建立Transport
  • 建立TProtocol
  • 基於TTransport和TProtocol建立 Client
  • 調用Client的相應方法

4.數據傳輸協議windows

  • TBinaryProtocol : 二進制格式.
  • TCompactProtocol : 壓縮格式
  • TJSONProtocol : JSON格式
  • TSimpleJSONProtocol : 提供JSON只寫協議, 生成的文件很容易經過腳本語言解析
  • tips:客戶端和服務端的協議要一致

代碼測試

1.建立文件,在D:\thrift下創建hello.thrift文件maven

namespace java com.tony.thrift.demo
 
service  HelloWorldService {
  string sayHello(1:string username)
}

thrift -r -gen java hello.thriftide

根據thrift 文件自動生成


2,將上述截圖生成的文件拷貝到工程中。測試

HelloWorldService編碼

public class HelloWorldService {

  public interface Iface {

    public String sayHello(String username) throws org.apache.thrift.TException;

  }
}

HelloWorldImpl

public class HelloWorldImpl implements HelloWorldService.Iface{

    @Override
    public String sayHello(String username) throws TException {
        return "Hi," + username + " welcome to thrift";
    }

}

HelloServerDemo

public class HelloServerDemo {

    public static final int SERVER_PORT = 7911;

    public void startServer() {
        try {
            System.out.println("Server start ....");

            TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
            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();

        } catch (Exception e) {
            System.out.println("Server start error!!!");
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        HelloServerDemo server = new HelloServerDemo();
        server.startServer();
    }

}

HelloClientDemo

public class HelloClientDemo
{
    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 7911;
    public static final int TIMEOUT = 30000;

    public void startClient(String userName) {
        TTransport transport = null;
        try {
            transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
            TProtocol protocol = new TBinaryProtocol(transport);
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            transport.open();
            String result = client.sayHello(userName);
            System.out.println(result);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }

    public static void main(String[] args) {
        HelloClientDemo client = new HelloClientDemo();
        client.startClient("tony");

    }

}

服務端啓動後,能夠測試客戶端。

https://thrift.apache.org/download

thrift優缺點

相關文章
相關標籤/搜索