MST
星途 面试题库

面试题:微服务架构下Apache Thrift的基础应用

在微服务架构中,简述Apache Thrift实现跨语言RPC通信的基本步骤,包括如何定义接口文件、生成不同语言的代码以及启动服务和客户端进行通信。
19.1万 热度难度
后端开发微服务架构

知识考点

AI 面试

面试题答案

一键面试
  1. 定义接口文件
    • 使用Thrift的IDL(接口定义语言)来定义服务接口。例如,创建一个.thrift文件,在其中定义服务的方法、参数和返回值类型。
    // 定义命名空间,这里以Java为例
    namespace java com.example.thrift
    
    // 定义服务
    service HelloService {
        string sayHello(1:string name)
    }
    
  2. 生成不同语言的代码
    • 安装Thrift编译器,根据不同语言的需求进行安装。
    • 使用Thrift编译器生成对应语言的代码。例如,对于Java语言:
      • 在命令行执行thrift -gen java HelloService.thrift,会在当前目录生成gen - java目录,里面包含生成的Java代码,包括服务接口、客户端和服务器端相关的基础代码。
    • 对于Python语言,执行thrift -gen py HelloService.thrift,会生成Python相关代码,结构类似,用于后续实现服务端和客户端。
  3. 启动服务
    • 以Java为例
      • 在生成的Java代码基础上,实现HelloService接口。
      package com.example.thrift;
      
      public class HelloServiceImpl implements HelloService.Iface {
          @Override
          public String sayHello(String name) throws org.apache.thrift.TException {
              return "Hello, " + name;
          }
      }
      
      • 编写服务器端启动代码:
      package com.example.thrift;
      
      import org.apache.thrift.TProcessor;
      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 HelloServer {
          public static void main(String[] args) {
              try {
                  TProcessor processor = new HelloService.Processor<>(new HelloServiceImpl());
                  TServerSocket serverTransport = new TServerSocket(9090);
                  TServer.Args args1 = new TServer.Args(serverTransport);
                  args1.protocolFactory(new TBinaryProtocol.Factory());
                  TServer server = new TSimpleServer(args1);
                  System.out.println("Starting the server...");
                  server.serve();
              } catch (TTransportException e) {
                  e.printStackTrace();
              }
          }
      }
      
    • 以Python为例
      • 实现HelloService接口。
      from HelloService import HelloService
      
      class HelloServiceImpl(HelloService.Iface):
          def sayHello(self, name):
              return "Hello, " + name
      
      • 编写服务器端启动代码:
      from thrift.transport import TSocket
      from thrift.transport import TTransport
      from thrift.protocol import TBinaryProtocol
      from thrift.server import TServer
      from HelloService import HelloService
      
      class HelloServiceImpl(HelloService.Iface):
          def sayHello(self, name):
              return "Hello, " + name
      
      if __name__ == '__main__':
          try:
              handler = HelloServiceImpl()
              processor = HelloService.Processor(handler)
              transport = TSocket.TServerSocket(port = 9090)
              tfactory = TTransport.TBufferedTransportFactory()
              pfactory = TBinaryProtocol.TBinaryProtocolFactory()
      
              server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
              print('Starting the server...')
              server.serve()
          except Exception as e:
              print(e)
      
  4. 客户端进行通信
    • 以Java为例
      • 编写客户端代码:
      package com.example.thrift;
      
      import org.apache.thrift.protocol.TBinaryProtocol;
      import org.apache.thrift.protocol.TProtocol;
      import org.apache.thrift.transport.TSocket;
      import org.apache.thrift.transport.TTransport;
      
      public class HelloClient {
          public static void main(String[] args) {
              try {
                  TTransport transport = new TSocket("localhost", 9090);
                  transport.open();
                  TProtocol protocol = new TBinaryProtocol(transport);
                  HelloService.Client client = new HelloService.Client(protocol);
                  String result = client.sayHello("World");
                  System.out.println("Result: " + result);
                  transport.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      
    • 以Python为例
      • 编写客户端代码:
      from thrift.transport import TSocket
      from thrift.transport import TTransport
      from thrift.protocol import TBinaryProtocol
      from HelloService import HelloService
      
      if __name__ == '__main__':
          try:
              transport = TSocket.TSocket('localhost', 9090)
              transport = TTransport.TBufferedTransport(transport)
              protocol = TBinaryProtocol.TBinaryProtocol(transport)
              client = HelloService.Client(protocol)
              transport.open()
              result = client.sayHello("World")
              print("Result: " + result)
              transport.close()
          except Exception as e:
              print(e)