轮询客户端连接的正确方法是什么?— Apache Thrift Server

问题描述 投票:0回答:1

我对服务器端编程的经验很少,我正在执行一项任务,需要使用Apache thrift实现部分后端服务器。

现在,我的应用程序由“前端”服务器和“后端”服务器组成。前端服务器将对后端进行RPC调用。

现在,如果我从命令行启动后端服务器,我希望该后端服务器保持活动状态,并且从某种意义上讲,请继续轮询要与之建立连接的其他节点。当前,当后端无法立即连接到前端节点时,它只会吐出一个错误。后端代码现在看起来像这样:

public class BENode {
public static void main(String [] args) throws Exception {

...


    TSocket sock = new TSocket(hostFE, portFE);
    TTransport transport = new TFramedTransport(sock);
    TProtocol protocol = new TBinaryProtocol(transport);
    BcryptService.Client client = new BcryptService.Client(protocol);
    transport.open();
}
}

我相信,当前端(FE)节点不可用于连接时,这是引发Exception(java.net.ConnectException:连接被拒绝)的最后一行。但是,预期的行为是后端服务器将保持活动状态并继续轮询以建立与前端节点的连接,直到最终能够成功连接为止。我很确定我不应该在try-catch块中使用无限while循环,因为这样效率低下,而且是不好的做法:

try{

     transport.open();
} catch() {

}

最佳方法是什么?

java sockets server-side thrift
1个回答
0
投票

给出,我理解正确,并且FE应该连接BE服务器。

然后,基本思想是: *启动BE Thrift服务器 *将FE连接到它上

由于BE打算充当服务器,因此我们必须启动Thrift Server,而不是客户端:

public static void simple(Calculator.Processor processor) {
  try {
    TServerTransport serverTransport = new TServerSocket(9090);
    TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
    System.out.println("Starting the simple server...");
    server.serve();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.