Thrift入门

本文只是学习thrift一个入门的笔记,刚刚看了一些资料,写了个hello word 级别的代码,只为认识一下thrift,哈哈。

下载安装

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

这就安装成功了。

新建一个maven工程



    4.0.0

    com.yefengyu.thrift
    thrift-test
    1.0-SNAPSHOT

    
        
            org.apache.thrift
            libthrift
            0.13.0
        
    

新建thrift接口定义文件

新建文件 src/main/java/com/yefengyu/thrift/idl/data.thrift。注意 com/yefengyu/thrift/idl 是包路径,在创建data.thrift后,idea会让下载thrift插件,下载重启idea即可。

data.thrift内容为:

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, 进入相关目录

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

使用thrift命令生成代码

thrift --gen java idl/data.thrift

此时生成的代码和com/yefengyu/thrift在同一个目录层级下面。注意此时的目录为:gen-java.generated.file。我把gen-java这级目录删除了。

此时生成了三个文件,由于文件内容太多,都是thrift自动生成,就不贴代码了。

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

接口实现

新建 PersonServiceImpl 实现 PersonService 接口。

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

代码没有真实业务逻辑,纯粹演示而已。

编写服务端启动类

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 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)。

你可能感兴趣的:(javathrift)