thrift-client 的连接只有在这个客户端第一次启动后才会成功

Connection of thrift-client will only be successful after the first starting of this client

这是重新启动我的 java 节俭客户端后第一次有用。然后它显示了套接字超时。抛出异常:

org.apache.thrift.transport.TTransportException: java.net.SocketTimeoutException: Read timed out

这是我的 python thrift 服务器;

if __name__ == '__main__':
 handler = MessageServiceHandler()
    processor = MessageService.Processor(handler)
    transport = TSocket.TServerSocket(host="127.0.0.1", port=9090)
    tFactory = TTransport.TFramedTransportFactory()
    pFactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = TServer.TSimpleServer(processor, transport, tFactory, pFactory)
    print("python thrift message service start")
    server.serve()
    print("python thrift message service exit")

这是我的 Java 储蓄客户:

   public MessageService.Client getMessageService() {
        TSocket socket = new TSocket("localhost", 9090, 3000);
        TTransport transport = new TFramedTransport(socket);
        try {
            transport.open();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        TProtocol protocol = new TBinaryProtocol(transport);
        MessageService.Client client = new MessageService.Client(protocol); 
        return client;
    }

我解决了这个问题,因为我使用了 'new Client',这会阻止下一次连接。所以我将代码更改为:

MessageService.Client messageServiceClient;
public MessageService.Client getMessageService() {
   if(messageServiceClient!=null){ 
       return messageServiceClient;
   }
   TSocket socket = new TSocket("localhost", 9090, 3000);
   TTransport transport = new TFramedTransport(socket);
   try {
       transport.open();
   } catch (TTransportException e) {
       e.printStackTrace();
   }
   TProtocol protocol = new TBinaryProtocol(transport);
   messageServiceClient = new MessageService.Client(protocol); 
   return messageServiceClient;
}