Thrift學習


Thrift官方網站: http://thrift.apache.org

目前對Thrift的學習和理解都是HelloWorld級別的,所以就貼一個HelloWorld吧,作爲學習的第一步。

首先看一下需要建立的文件目錄結構。

image.png

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
 
    <groupId>com.mt</groupId> 
    <artifactId>thrift-test</artifactId> 
    <version>1.0-SNAPSHOT</version> 
 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-compiler-plugin</artifactId> 
                <configuration> 
                    <source>6</source> 
                    <target>6</target> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
    <dependencies> 
        <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.5.8</version> 
        </dependency> 
    </dependencies> 
</project>

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() 
}

Hello.java 文件系統可以自動生成。 鏈接:https://pan.baidu.com/s/1UV7XXzKlasSzh5RL-iMoMg  密碼:ccgo

本文使用本地安裝的Thrift進行生成。 進入Hello.thrift目錄,執行thrift –gen java Hello.thrift。,代碼爲自動生成所以不再展示。

實現自定義的Hello.thrift目標接口。

HelloServiceImpl.java(實際處理業務的實現類)

 
package service.demo; 
 
import org.apache.thrift.TException; 
 
public class HelloServiceImpl implements Hello.Iface { 
    @Override 
    public boolean helloBoolean(boolean para) throws TException { 
        return para; 
    } 
 
    @Override 
    public int helloInt(int para) throws TException { 
        try { 
            Thread.sleep(20000); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } 
        return para; 
    } 
 
    @Override 
    public String helloNull() throws TException { 
        return null; 
    } 
 
    @Override 
    public String helloString(String para) throws TException { 
        System.out.println("參數:" + para); 
        return "hello  " + para; 
    } 
 
    @Override 
    public void helloVoid() throws TException { 
        System.out.println("Hello World"); 
    } 
}

提供服務的服務端。

HelloServiceServer.java

package server; 
 
import org.apache.thrift.TProcessor; 
import org.apache.thrift.protocol.TBinaryProtocol; 
import org.apache.thrift.server.TServer; 
import org.apache.thrift.server.TThreadPoolServer; 
import org.apache.thrift.transport.TServerSocket; 
import org.apache.thrift.transport.TTransportException; 
import service.demo.Hello; 
import service.demo.HelloServiceImpl; 
 
public class HelloServiceServer { 
    /** 
     * 啓動 Thrift 服務器 
     * 
     * @param args 
     */ 
    public static void main(String[] args) { 
        try { 
            // 設置服務端口爲 1234 
            TServerSocket serverTransport = new TServerSocket(1234); 
            // 設置協議工廠爲 TBinaryProtocol.Factory 
            TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(); 
            // 關聯處理器與 Hello 服務的實現 
            TProcessor processor = new Hello.Processor(new HelloServiceImpl()); 
            TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport); 
            args1.processor(processor); 
            args1.protocolFactory(proFactory); 
            TServer server = new TThreadPoolServer(args1); 
            System.out.println("Start server on port 1234..."); 
            server.serve(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } 
    } 
}

客戶端。

HelloServiceClient.java

package 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 org.apache.thrift.transport.TTransportException; 
import service.demo.Hello; 
 
public class HelloServiceClient { 
    /** 
     * 調用 Hello 服務 
     * 
     * @param args 
     */ 
    public static void main(String[] args) throws TException { 
        try { 
            // 
            TTransport transport = new TSocket("localhost", 1234); 
            transport.open(); 
            // 設置傳輸協議爲 TBinaryProtocol 
            TProtocol protocol = new TBinaryProtocol(transport); 
            Hello.Client client = new Hello.Client(protocol); 
            // 調用服務的 helloString 方法 
            String resp = client.helloString("it's a beautiful day"); 
            System.out.println(resp); 
            transport.close(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } 
    } 
}

先運行服務端:HelloServiceServer.java.

啓動客戶端:HelloServiceClient.java.

結果截圖:

image.png

image.png

附上一個源碼鏈接。demo很簡單,但是初學可能也會遇到各種小問題。參考一下吧。

鏈接:https://pan.baidu.com/s/1UV7XXzKlasSzh5RL-iMoMg  密碼:ccgo


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章