Thrift 入門

前言
在微服務盛行的互聯網時代,各家都在構建本身的微服務體系,而這其中RPC框架也是其中比較重要的部分,筆者以前一直接觸使用的是 Dubbo 框架,對Thrift 徹底不瞭解,也是在研究學習的過程當中,不斷的記錄纔有了這篇博客。

參考博文:https://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/#ibm-pconjava

Thrift 是什麼?
Thrift 是一個跨語言的服務開發框架,採用接口描述語言定義並建立服務,所包含的代碼生成引擎能夠在多種語言中,其傳輸數據採用二進制格式,相對於xml與Json傳輸體積更小,對於高併發,大數據量和多語言更有優點。
Thrift 入門示例
學習課程以前先來個Hello World
主要包含:1. Thrift接口定義文件 2. Java 實現類 3.服務端代碼示例 4. 客戶端代碼示例

本文使用Thrift 版本
Thrift version 0.9.3linux

maven 包引入
<dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.9.3</version>
</dependency>
Thrift 文件
namespace java com.example.demo.thrift
service  Hello {
  string sayHello(1:string name)
}
編譯的Java 文件略,編譯命令
thrift -gen java Hello.thrift
接口實現
package com.example.demo.thrift.impl;

import com.example.demo.thrift.Hello;
import org.apache.thrift.TException;

public class HelloThriftImpl implements Hello.Iface {

    @Override
    public String sayHello(String name) throws TException {
        System.out.println(name);
        return "success";
    }
}
服務端代碼
package com.example.demo.thrift.server;

import com.example.demo.thrift.Hello;
import com.example.demo.thrift.impl.HelloThriftImpl;
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;

public class HelloThriftServer {

    private static int port = 8888;

    public static void main(String[] args) {
        try {
            //設置服務端的端口
            TServerSocket serverTransport = new TServerSocket(port);
            // 設置協議工廠爲 TBinaryProtocol.Factory
            TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory();
            // 關聯處理器與 Hello 服務的實現
            TProcessor processor = new Hello.Processor(new HelloThriftImpl());

            TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport);
            args1.inputProtocolFactory(proFactory);
            args1.processor(processor);
            TServer server = new TThreadPoolServer(args1);
            System.out.println("Start server on port " + port);
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
客戶端代碼
package com.example.demo.thrift.client;

import com.example.demo.thrift.Hello;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class HelloThriftClient {

    private static int port = 8888;
    public static void main(String[] args) {

        try {
            // 設置調用的服務地址爲本地,端口爲 8888
            TTransport transport = new TSocket("localhost", port);
            transport.open();
            // 設置傳輸協議爲 TBinaryProtocol
            TProtocol protocol = new TBinaryProtocol(transport);
            Hello.Client client = new Hello.Client(protocol);
            // 調用服務的 helloVoid 方法
            String helloRtn = client.sayHello("hello world");
            System.out.println(helloRtn);
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
代碼執行
先啓動Server
20190811181348.jpg
20190811181432.jpg
相關文章
相關標籤/搜索