Thrift Demo示例

Posted 梅川高中程序员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thrift Demo示例相关的知识,希望对你有一定的参考价值。

1.下载thrift

http://thrift.apache.org/download


2.配置环境变量

如下载完成,重命名为thrift.ext,保存路径为C:\Thrift,添加系统环境变量path:

C:\Thrift
cmd输入thrift -version,出现版本就ok


3.jar包依赖

maven中央库搜thrift,目前最新版0.10.0,gradle配置:
org.apache.thrift:libthrift:0.10.0


4.编写IDL文件

data.thrift如下:

namespace java thrift.generated
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 date
}
service PersonService{
    Person getPersonByUsername(1 : required String username) throws (1: DataException dataException),
    void savePerson(1: Person person)throws (1: DataException dataException)
}


5.生成java代码

thrift –gen java src/thrift/data.thrift


6.编写业务代码

PersonServiceImpl.java

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.setUsername(username);
        person.setAge(20);
        person.setMarried(false);
        return person;
    }
    @Override
    public void savePerson(Person person) throws DataException, TException {
        System.out.println("Got Client Param: " + person);
    }
}

7.服务器端

ThriftServer.java

public class ThriftServer {
    public static void main(String[] args) throws Exception{
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        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 TFramedTransport.Factory());
        arg.processorFactory(new TProcessorFactory(processor));
        TServer server = new THsHaServer(arg);
        System.out.println("Thrift Server Started!");
        server.serve();
    }
}


8.客户端

ThriftClient.java

public class ThriftClient {
    public static void main(String[] args) {
        TTransport transport = new TFastFramedTransport(new TSocket("localhost",8899),600);
        TProtocol protocol = new TCompactProtocol(transport);
        PersonService.Client client = new PersonService.Client(protocol);
        try {
            transport.open();
            Person person = client.getPersonByUsername("zhangsan");
            System.out.println("Got from Server: " + person);
            System.out.println("-------------------");
            Person person2 = new Person();
            person2.setUsername("李四");
            person2.setAge(30);
            person2.setMarried(true);
            client.savePerson(person2);
        } catch (TException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            transport.close();
        }
    }
}

9.运行

启动服务器端,再启动客户端
客户端输出:

Received 1

Got from Server: Person(username:zhangsan, age:20, married:false)

Received 2

服务端输出:

Thrift Server Started!
Got Client Param: zhangsan
Got Client Param: Person(username:李四, age:30, married:true)

以上是关于Thrift Demo示例的主要内容,如果未能解决你的问题,请参考以下文章

Thrift RPC的一个简单c++ demo

Thrift快速入门

thrift 远程服务调用 简单示例

thrift简单示例 (基于C++)

Thrift介绍以及Java中使用Thrift实现RPC示例

thrift简单示例 (go语言)