Thrift RPC實戰(一) Thrift入門Demo

1.前言

Thrift是一種接口描述語言和二進制通信協議,它被用來定義和建立跨語言的服務。它被看成一個遠程過程調用(RPC)框架來使用,是由Facebook爲「大規模跨語言服務開發」而開發的。它經過一個代碼生成引擎聯合了一個軟件棧,來建立不一樣程度的、無縫的跨平臺高效服務,能夠使用C#、C++(基於POSIX兼容系統)、Cappuccino、Cocoa、Delphi、Erlang、Go、Haskell、Java、Node.js、OCaml、Perl、PHP、Python、Ruby和Smalltalk。另外,Thrift早期由Facebook開發,目前已成爲Apache軟件基金會的開源項目。java

2.基本架構

相似於計算機網絡,Thrift包含一套完整的棧來建立客戶端和服務端程序。apache

  • 頂層部分是由Thrift定義生成的代碼。而服務則由這個文件客戶端和處理器代碼生成。在生成的代碼裏會建立不一樣於內建類型的數據結構,並將其做爲結果發送。
  • 協議層和傳輸層是運行時庫的一部分。有了Thrift,就能夠定義一個服務或改變通信和傳輸協議,而無需從新編譯代碼。除了客戶端部分以外,Thrift還包括服務器基礎設施來集成協議和傳輸,如阻塞、非阻塞及多線程服務器。
  • IO層在棧中做爲基礎部分對於不一樣的語言則有不一樣的實現。

3.快送入門

根據官網,使用Thrift前須要下載Thrift而且構建和安裝Thrift編譯器。
在使用Thrift進行編程時,須要如下幾步(詳細過程可見Apache Thrift Tutorial):編程

1.建立hello.thrift文件

namespace java com.lpf.service

service HelloService {
    string hello(1: string name);
}

2.使用Thrift編譯器編譯爲特定語言的源碼

thrift --gen <language> <Thrift filename>

生成的Java關鍵代碼以下:(部分代碼)服務器

package com.lpf.service;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.11.0)", date = "2018-06-08")
public class HelloService {

  public interface Iface {

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

  }
}

3.新建服務端工程 serviceDemo

把生成的HelloService引入工程中網絡

(1)添加pom依賴數據結構

<dependencies>
    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.11.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
    </dependency>

</dependencies>

(2)編寫接口實現類 HelloServiceImpl多線程

public class HelloServiceImpl implements HelloService.Iface {
    public HelloServiceImpl() {
    }

    @Override
    public String sayHello(String username) {
        return "Hi," + username + " ,Welcome to the thrift's world !";
    }
}

(3)啓動監聽HelloServerDemo架構

public class HelloServerDemo {
    public static void main(String[] args) throws TTransportException {
        Logger logger = LoggerFactory.getLogger(HelloServerDemo.class);
        int port= 9000 ;
        // *) 傳輸層(Transport), 設置監聽端口爲9000
        TServerSocket serverTransport = new TServerSocket(port);

        // *) 協議層
        TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory(true, true);
        // *) 處理層(Processor)
        HelloServiceImpl handler = new HelloServiceImpl();
        HelloService.Processor<HelloServiceImpl> processor = new HelloService.Processor<HelloServiceImpl>(handler);

        // *) 服務層(Server)
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport)
                        .protocolFactory(protocolFactory)
                        .processor(processor));

        // *) 啓動監聽服務
        server.serve();

    }

}

4.客服端遠程調用app

(1)添加pom依賴 同上框架

(2)編寫客戶端測試類

一樣的客戶端也須要把生成的接口類引入項目中

public class HelloClientDemo {
    public static void main(String[] args) throws TException {
        // *) 傳輸層
        TTransport transport = new TSocket("localhost", 9000);
        transport.open();
        // *) 協議層, 與服務端對應
        TProtocol protocol = new TBinaryProtocol(transport);
        // *) 建立RPC客戶端
        HelloService.Client client = new HelloService.Client(protocol);
        // *) 調用服務
        System.out.println(client.sayHello("lpf"));
        // *) 關閉句柄
        transport.close();

    }

}

先運行服務端,後運行客戶端,觀察結果,能夠看到客戶端輸出結果:

相關文章
相關標籤/搜索