Apache Thrift 实战

x33g5p2x  于2022-06-06 转载在 其他  
字(4.0k)|赞(0)|评价(0)|浏览(352)

一 添加依赖

<dependency>

<groupId>org.apache.thrift</groupId>

<artifactId>libthrift</artifactId>

<version>0.11.0</version>

</dependency>

二 编写 IDL

通过 IDL(.thrift 文件)定义数据结构、异常和接口等数据,供各种编程语言使用
namespace * thrift.generatecode

typedef i32 int

typedef string String

typedef bool boolean

// 数据结构

struct Student{

1:optional String name,

2:optional int age

}

// 异常

exception MyException{

1:optional String data

}

// 接口

service StudentService{

list<Student> queryStudents() ,

boolean addStudent(1:required String name,2:int age) throws(1:MyException e)

}

三 根据 IDL 生成类、异常和接口

thrift --gen java src/thift/Student.thrift

四 编写接口的实现类

package thrift;

import org.apache.thrift.TException;
import thrift.generatecode.MyException;
import thrift.generatecode.Student;
import thrift.generatecode.StudentService;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl implements StudentService.Iface {
    @Override
    public List<Student> queryStudents() throws TException {
        System.out.println("--Java服务端,模拟查询操作--");
        Student Student1 = new Student();
        Student1.setName("zs");
        Student1.setAge(23);

        Student Student2 = new Student();
        Student2.setName("ls");
        Student2.setAge(24);

        List<Student> Students = new ArrayList<>();
        Students.add(Student1);
        Students.add(Student2);
        System.out.println("--查询完毕--");
        return Students;
    }

    @Override
    public boolean addStudent(String name, int age) throws MyException, TException {
        System.out.println("--Java服务端,模拟增加操作--");
        System.out.println("增加成功:" + name + "," + age);
        return true;
    }
}

五 编写服务端启动类

package thrift;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import thrift.generatecode.StudentService;

public class TestThriftServer {
    public static void main(String[] args) throws TTransportException {
        // 使用多线程、非阻塞式的工作模式
        TNonblockingServerSocket server = new TNonblockingServerSocket(8888);
        THsHaServer.Args ServerArgs = new THsHaServer.Args(server).minWorkerThreads(3).maxWorkerThreads(5);
        StudentService.Processor<StudentServiceImpl> processor = new StudentService.Processor<>(new StudentServiceImpl());
        // 使用二进制格式传输数据
        ServerArgs.protocolFactory(new TBinaryProtocol.Factory());
        // 使用 TFramedTransport 方式传输数据
        ServerArgs.transportFactory(new TFramedTransport.Factory());
        ServerArgs.processorFactory(new TProcessorFactory(processor));
        TServer tserver = new THsHaServer(ServerArgs);
        // 启动服务
        tserver.serve();
    }
}

六 编写客户端

编写客户端代码,用于和服务端之间进行 RPC 调用。需要注意,客户端和服务端所使用的传输方式和传输协议必须保持一致。

package thrift;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import thrift.generatecode.Student;
import thrift.generatecode.StudentService;

import java.util.List;

public class TestThriftClient {
    public static void main(String[] args) {
        TTransport transport = new TFramedTransport(new TSocket("127.0.0.1", 8888), 1000);
        TProtocol protocol = new TBinaryProtocol(transport);
        // 创建用于访问服务端的对象
        StudentService.Client client = new StudentService.Client(protocol);
        try {
            // 与服务端建立连接
            transport.open();
            System.out.println("RPC调用服务端的queryStudents()方法");
            List<Student> Students = client.queryStudents();
            for (Student Student : Students) {
                System.out.println(Student.getName() + "\t" + Student.getAge());
            }
            System.out.println("RPC调用服务端的addStudent()方法");
            boolean result = client.addStudent("ww", 25);
            if (result) {
                System.out.println("增加成功!");
            } else {
                System.out.println("增加失败!");
            }
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            transport.close();
        }
    }
}

七 测试

1 先启动服务端,整个过程服务端打印如下

--Java服务端,模拟查询操作--

--查询完毕--

--Java服务端,模拟增加操作--

增加成功:ww,25

2 再启动客户端,整个过程客户端打印如下

RPC调用服务端的queryStudents()方法

zs    23

ls    24

RPC调用服务端的addStudent()方法

增加成功!

相关文章