Thrift使用实例

发布时间 2023-05-30 17:28:32作者: 田野与天

当然!这是三个使用Java实现的Thrift示例代码,用于演示Thrift的基本用法和通信模式:

示例1:简单的服务端和客户端

在此示例中,我们将创建一个简单的Thrift服务端和客户端,客户端向服务端发送请求并接收响应。

Thrift定义文件(.thrift文件):

namespace java com.example.thriftdemo

service GreetingService {
    string sayHello(1: string name)
}

服务端实现类

import org.apache.thrift.TException;
import com.example.thriftdemo.*;

public class GreetingServiceImpl implements GreetingService.Iface {
    @Override
    public String sayHello(String name) throws TException {
        return "Hello, " + name + "!";
    }
}

服务端启动类

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import com.example.thriftdemo.*;

public class Server {
    public static void main(String[] args) {
        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            GreetingService.Processor processor = new GreetingService.Processor(new GreetingServiceImpl());

            TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));

            System.out.println("Server started...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

客户端实现类

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 com.example.thriftdemo.*;

public class Client {
    public static void main(String[] args) {
        try {
            TTransport transport = new TSocket("localhost", 9090);
            transport.open();

            TProtocol protocol = new TBinaryProtocol(transport);
            GreetingService.Client client = new GreetingService.Client(protocol);

            String result = client.sayHello("John");
            System.out.println(result);

            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在示例1中,我们定义了一个名为GreetingService的Thrift服务,其中包含一个名为sayHello的RPC方法。

服务端实现类GreetingServiceImpl实现了自动生成的接口GreetingService.Iface,并重写了sayHello方法。在该方法中,我们构造欢迎消息并返回给客户端。

服务端启动类创建了一个TServerTransport来监听指定的端口,并创建了GreetingService.Processor来处理请求。然后,创建一个TSimpleServer实例并启动服务。

客户端实现类创建了一个TTransport来连接到服务端的指定地址和端口,并使用TBinaryProtocol和GreetingService.Client来创建客户端代理。然后,调用sayHello方法发送请求并接收响应,并将结果打印出来。

运行服务器和客户端代码后,您将看到客户端成功向服务端发送请求,并接收到服务端返回的欢迎消息。

示例2:使用自定义数据类型

在此示例中,我们

将使用自定义的Thrift结构体和枚举类型来定义服务的输入和输出。

Thrift定义文件(.thrift文件):

namespace java com.example.thriftdemo

struct Person {
    1: required string name,
    2: required i32 age,
    3: optional string email
}

enum Gender {
    MALE,
    FEMALE,
    OTHER
}

service PersonService {
    void savePerson(1: Person person),
    Person getPerson(1: string name),
    list<Person> getAllPersons(),
    Gender getGender(1: Person person)
}

服务端和客户端的实现类与示例1中的代码相同,只需在代码中使用新的Thrift定义文件。

示例3:使用传输层和协议

在此示例中,我们将使用Thrift提供的不同传输层和协议来演示不同的通信方式。

服务端和客户端实现类与示例1中的代码相同,只需在代码中使用不同的传输层和协议。

以下是一个使用TServerSocket和TBinaryProtocol的示例:

TServerTransport serverTransport = new TServerSocket(9090);
TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
TServer server = new TSimpleServer(new TServer.Args(serverTransport).protocolFactory(protocolFactory).processor(processor));

以下是一个使用TNonblockingServerSocket和TCompactProtocol的示例:

TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(9090);
TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
TServer server = new TNonblockingServer(new TNonblockingServer.Args(serverTransport).protocolFactory(protocolFactory).processor(processor));

这些示例代码演示了Thrift在Java中的基本用法和通信模式。请确保您已正确配置Thrift和相关依赖项,并根据需要更改主机和端口。