Thrift使用实例
当然,以下是一个详细的Thrift示例代码,演示了如何使用Thrift进行客户端和服务端的通信:
- 定义Thrift文件(example.thrift):
namespace java com.example.thrift
struct Student {
1: i32 id,
2: string name,
3: i32 age
}
service StudentService {
Student getStudentById(1: i32 id),
void saveStudent(1: Student student)
}
- 使用Thrift编译器生成Java代码:
将上述定义的Thrift文件(example.thrift)使用Thrift编译器生成Java代码:
thrift --gen java example.thrift
这将生成一个名为com.example.thrift
的Java包,并包含相关的Thrift数据类型和服务接口。
- 实现服务端代码:
import com.example.thrift.Student;
import com.example.thrift.StudentService;
import org.apache.thrift.TException;
import java.util.HashMap;
import java.util.Map;
public class StudentServiceImpl implements StudentService {
private Map<Integer, Student> studentMap = new HashMap<>();
@Override
public Student getStudentById(int id) throws TException {
if (studentMap.containsKey(id)) {
return studentMap.get(id);
} else {
throw new TException("Student not found");
}
}
@Override
public void saveStudent(Student student) throws TException {
studentMap.put(student.getId(), student);
System.out.println("Saved student: " + student.getName());
}
}
在上述代码中,我们实现了StudentService
接口,并提供了getStudentById
和saveStudent
方法的具体实现。在getStudentById
方法中,我们根据学生ID从studentMap
中查找学生信息。在saveStudent
方法中,我们将学生信息保存到studentMap
中,并打印保存的学生姓名。
- 实现服务端代码:
import com.example.thrift.Student;
import com.example.thrift.StudentService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
public class ThriftServer {
public static void main(String[] args) {
try {
// 创建TServerSocket
TServerSocket serverTransport = new TServerSocket(9090);
// 创建StudentService实现类
StudentService.Processor<StudentServiceImpl> processor =
new StudentService.Processor<>(new StudentServiceImpl());
// 创建TBinaryProtocol.Factory
TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
// 创建TServer.Args
TServer.Args serverArgs = new TServer.Args(serverTransport);
serverArgs.processor(processor);
serverArgs.protocolFactory(protocolFactory);
// 创建TServer
TServer server = new TSimpleServer(serverArgs);
System.out.println("Starting the server...");
server.serve();
} catch (TTransportException ex) {
ex.printStackTrace();
}
}
}
在上述代码中,我们首先创建了一个TServerSocket
对象,用于指定服务端的监听端口。然后,我们创建了一个StudentService.Processor
对象,并将StudentServiceImpl
作为
构造函数的参数传入。接下来,我们创建了一个TBinaryProtocol.Factory
对象,用于指定使用二进制协议进行序列化和反序列化。最后,我们使用TServer.Args
构建服务端的参数,并创建了一个TSimpleServer
对象,然后调用serve()
方法启动服务端。
- 实现客户端代码:
import com.example.thrift.Student;
import com.example.thrift.StudentService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
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) {
try {
// 创建TTransport
TTransport transport = new TSocket("localhost", 9090);
// 创建TProtocol
TProtocol protocol = new TBinaryProtocol(transport);
// 创建StudentService.Client
StudentService.Client client = new StudentService.Client(protocol);
// 打开Transport
transport.open();
// 调用服务方法
Student student = client.getStudentById(1);
System.out.println("Received student: " + student.getName());
Student newStudent = new Student(2, "John", 20);
client.saveStudent(newStudent);
System.out.println("Saved new student: " + newStudent.getName());
// 关闭Transport
transport.close();
} catch (TTransportException | TException ex) {
ex.printStackTrace();
}
}
}
在上述代码中,我们首先创建了一个TTransport
对象,用于指定客户端连接的目标地址和端口。然后,我们创建了一个TProtocol
对象,使用二进制协议进行序列化和反序列化。接下来,我们创建了一个StudentService.Client
对象,将protocol
作为构造函数的参数传入。然后,我们调用open()
方法打开transport
,建立与服务端的连接。接着,我们调用服务方法getStudentById
和saveStudent
来与服务端进行通信,并打印相应的结果。最后,我们调用close()
方法关闭transport
。
请确保在编译和运行代码之前,您已经正确配置了Thrift的依赖项,并且生成了相应的Java代码。