Apache Thrift 实战

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

一 添加依赖

<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

四 编写接口的实现类

  1. package thrift;
  2. import org.apache.thrift.TException;
  3. import thrift.generatecode.MyException;
  4. import thrift.generatecode.Student;
  5. import thrift.generatecode.StudentService;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class StudentServiceImpl implements StudentService.Iface {
  9. @Override
  10. public List<Student> queryStudents() throws TException {
  11. System.out.println("--Java服务端,模拟查询操作--");
  12. Student Student1 = new Student();
  13. Student1.setName("zs");
  14. Student1.setAge(23);
  15. Student Student2 = new Student();
  16. Student2.setName("ls");
  17. Student2.setAge(24);
  18. List<Student> Students = new ArrayList<>();
  19. Students.add(Student1);
  20. Students.add(Student2);
  21. System.out.println("--查询完毕--");
  22. return Students;
  23. }
  24. @Override
  25. public boolean addStudent(String name, int age) throws MyException, TException {
  26. System.out.println("--Java服务端,模拟增加操作--");
  27. System.out.println("增加成功:" + name + "," + age);
  28. return true;
  29. }
  30. }

五 编写服务端启动类

  1. package thrift;
  2. import org.apache.thrift.TProcessorFactory;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.server.THsHaServer;
  5. import org.apache.thrift.server.TServer;
  6. import org.apache.thrift.transport.TFramedTransport;
  7. import org.apache.thrift.transport.TNonblockingServerSocket;
  8. import org.apache.thrift.transport.TTransportException;
  9. import thrift.generatecode.StudentService;
  10. public class TestThriftServer {
  11. public static void main(String[] args) throws TTransportException {
  12. // 使用多线程、非阻塞式的工作模式
  13. TNonblockingServerSocket server = new TNonblockingServerSocket(8888);
  14. THsHaServer.Args ServerArgs = new THsHaServer.Args(server).minWorkerThreads(3).maxWorkerThreads(5);
  15. StudentService.Processor<StudentServiceImpl> processor = new StudentService.Processor<>(new StudentServiceImpl());
  16. // 使用二进制格式传输数据
  17. ServerArgs.protocolFactory(new TBinaryProtocol.Factory());
  18. // 使用 TFramedTransport 方式传输数据
  19. ServerArgs.transportFactory(new TFramedTransport.Factory());
  20. ServerArgs.processorFactory(new TProcessorFactory(processor));
  21. TServer tserver = new THsHaServer(ServerArgs);
  22. // 启动服务
  23. tserver.serve();
  24. }
  25. }

六 编写客户端

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

  1. package thrift;
  2. import org.apache.thrift.TException;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.protocol.TProtocol;
  5. import org.apache.thrift.transport.TFramedTransport;
  6. import org.apache.thrift.transport.TSocket;
  7. import org.apache.thrift.transport.TTransport;
  8. import thrift.generatecode.Student;
  9. import thrift.generatecode.StudentService;
  10. import java.util.List;
  11. public class TestThriftClient {
  12. public static void main(String[] args) {
  13. TTransport transport = new TFramedTransport(new TSocket("127.0.0.1", 8888), 1000);
  14. TProtocol protocol = new TBinaryProtocol(transport);
  15. // 创建用于访问服务端的对象
  16. StudentService.Client client = new StudentService.Client(protocol);
  17. try {
  18. // 与服务端建立连接
  19. transport.open();
  20. System.out.println("RPC调用服务端的queryStudents()方法");
  21. List<Student> Students = client.queryStudents();
  22. for (Student Student : Students) {
  23. System.out.println(Student.getName() + "\t" + Student.getAge());
  24. }
  25. System.out.println("RPC调用服务端的addStudent()方法");
  26. boolean result = client.addStudent("ww", 25);
  27. if (result) {
  28. System.out.println("增加成功!");
  29. } else {
  30. System.out.println("增加失败!");
  31. }
  32. } catch (TException e) {
  33. e.printStackTrace();
  34. } finally {
  35. transport.close();
  36. }
  37. }
  38. }

七 测试

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

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

--查询完毕--

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

增加成功:ww,25

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

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

zs    23

ls    24

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

增加成功!

相关文章