<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
通过 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)
}
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();
}
}
}
--Java服务端,模拟查询操作--
--查询完毕--
--Java服务端,模拟增加操作--
增加成功:ww,25
RPC调用服务端的queryStudents()方法
zs 23
ls 24
RPC调用服务端的addStudent()方法
增加成功!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/125112729
内容来源于网络,如有侵权,请联系作者删除!