Thrift入門

本文只是學習thrift一個入門的筆記,剛剛看了一些資料,寫了個hello word 級別的代碼,只爲認識一下thrift,哈哈。java

下載安裝

C:\Users\yefengyu>thrift -version
Thrift version 0.13.0

這就安裝成功了。apache

新建一個maven工程

<?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.yefengyu.thrift</groupId>
    <artifactId>thrift-test</artifactId>
    <version>1.0-SNAPSHOT</version>

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

</project>

新建thrift接口定義文件

新建文件 src/main/java/com/yefengyu/thrift/idl/data.thrift。注意 com/yefengyu/thrift/idl 是包路徑,在建立data.thrift後,idea會讓下載thrift插件,下載重啓idea便可。socket

data.thrift內容爲:maven

namespace java generated.file  
typedef i16 short  
typedef i32 int  
typedef i64 long  
typedef bool boolean  
typedef string String  
  
struct Person{  
    1: optional String username,  
    2: optional int age,  
    3: optional boolean married  
}  
  
exception DataException{  
    1: optional String message,  
    2: optional String callStack,  
    3: optional String data  
}  
  
service PersonService{  
    Person getPersonByUserName(1: required String username) throws(1: DataException dataException),  
    void SavePerson(1: required Person person) throws(1: DataException dataException)  
}

Thrift生成代碼

打開idea的控制檯 Terminal, 進入相關目錄ide

cd src/main/java/com/yefengyu/thrift

使用thrift命令生成代碼學習

thrift --gen java idl/data.thrift

此時生成的代碼和com/yefengyu/thrift在同一個目錄層級下面。注意此時的目錄爲:gen-java.generated.file。我把gen-java這級目錄刪除了。測試

此時生成了三個文件,因爲文件內容太多,都是thrift自動生成,就不貼代碼了。ui

  • Person.class
  • DataException.class
  • PersonService.class

接口實現

新建 PersonServiceImpl 實現 PersonService 接口。idea

package com.yefengyu.thrift.server;  
  
import com.yefengyu.thrift.generated.file.DataException;  
import com.yefengyu.thrift.generated.file.Person;  
import com.yefengyu.thrift.generated.file.PersonService;  
import org.apache.thrift.TException;  
  
public class PersonServiceImpl implements PersonService.Iface {  
  
    @Override  
  public Person getPersonByUserName(String username) throws DataException, TException {  
        System.out.println("Got Client Param: " + username);  
        Person person = new Person();  
        person.setAge(20);  
        person.setUsername(username);  
        person.setMarried(true);  
        return person;  
    }  
  
    @Override  
  public void SavePerson(Person person) throws DataException, TException {  
        System.out.println("Got Client Param: ");  
        System.out.println(person.getAge());  
        System.out.println(person.getUsername());  
        System.out.println(person.isMarried());  
    }  
}

代碼沒有真實業務邏輯,純粹演示而已。spa

編寫服務端啓動類

package com.yefengyu.thrift.server;  
  
import com.yefengyu.thrift.generated.file.PersonService;  
import org.apache.thrift.TProcessorFactory;  
import org.apache.thrift.protocol.TCompactProtocol;  
import org.apache.thrift.server.THsHaServer;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.transport.TFastFramedTransport;  
import org.apache.thrift.transport.TNonblockingServerSocket;  
import org.apache.thrift.transport.TTransportException;  
  
public class ThriftServer {  
    public static void main(String[] args) throws TTransportException {  
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8888);  
        THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);  
        PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());  
        arg.protocolFactory(new TCompactProtocol.Factory());  
        arg.transportFactory(new TFastFramedTransport.Factory());  
        arg.processorFactory(new TProcessorFactory(processor));  
        TServer server = new THsHaServer(arg);  
        System.out.println("服務端啓動成功");  
        server.serve();  
    }  
}

編寫客戶端

package com.yefengyu.thrift.client;  
  
import com.yefengyu.thrift.generated.file.Person;  
import com.yefengyu.thrift.generated.file.PersonService;  
import org.apache.thrift.protocol.TCompactProtocol;  
import org.apache.thrift.protocol.TProtocol;  
  
import org.apache.thrift.transport.TFastFramedTransport;  
import org.apache.thrift.transport.TSocket;  
import org.apache.thrift.transport.TTransport;  
import org.apache.thrift.transport.TTransportException;  
  
  
public class ThriftClient {  
    public static void main(String[] args) throws TTransportException {  
        TTransport tTransport = new TFastFramedTransport(new TSocket("localhost", 8888), 600);  
        TProtocol protocol = new TCompactProtocol(tTransport);  
        PersonService.Client client = new PersonService.Client(protocol);  
  
        try {  
            tTransport.open();  
            Person person = client.getPersonByUserName("張三");  
            System.out.println(person.getUsername());  
            System.out.println(person.getAge());  
            System.out.println(person.isMarried());  
  
            Person person1 = new Person();  
            person1.setMarried(false);  
            person1.setUsername("李四");  
            person1.setAge(34);  
            client.SavePerson(person1);  
  
        } catch (Exception e) {  
            throw new RuntimeException(e.getMessage(), e);  
        } finally {  
            tTransport.close();  
        }  
    }  
}

目錄層級

image.png

總結

暫時沒有詳細瞭解代碼的方方面面,只爲理解thrift的功能。

thrift 文件就是一種接口協議,服務方定義以後(data.thrift),生成相關接口(thrift --gen java idl/data.thrift),編寫實現類(PersonServiceImpl),而後發佈服務(ThriftServer)。客戶方放到接口協議(data.thrift),生成相關接口(thrift --gen java idl/data.thrift),編寫客戶端調用遠程服務(ThriftClient)。
相關文章
相關標籤/搜索