简体   繁体   English

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

[英]What is the Proper way to Poll for Client Connections — Apache Thrift Server

I have very little experience with server-side programming, and I am taking on a task where I need to implement part of a Back-end Server using Apache thrift.我对服务器端编程的经验很少,我正在承担一项任务,我需要使用 Apache thrift 来实现后端服务器的一部分。

Right now, my application consists of a "Front-End" server and a "Back-End" server.现在,我的应用程序由一个“前端”服务器和一个“后端”服务器组成。 The Front-End Server will make RPC calls to the Backend.前端服务器将对后端进行 RPC 调用。

Right now, if I start the backend server from the command-line, I want that backend server to stay alive and, in a sense, keep polling for other nodes that want to establish a connection with it.现在,如果我从命令行启动后端服务器,我希望该后端服务器保持活动状态,并且从某种意义上说,继续轮询其他想要与其建立连接的节点。 Currently, the backend just spits out an error when it's not immediately able to connect to a front-end node.目前,当后端无法立即连接到前端节点时,它只会吐出一个错误。 The backend code right now looks like this:现在的后端代码如下所示:

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();
}
}

I believe it's the last line that throws an Exception (java.net.ConnectException: Connection refused) when the front-end (FE) node is not available to connect.我相信当前端(FE)节点无法连接时,这是引发异常(java.net.ConnectException:连接被拒绝)的最后一行。 However, the intended behaviour is that the back-end server will stay alive and keep polling to establish a connection with the front-end node, until it finally is able to connect successfully.但是,预期的行为是后端服务器将保持活动状态并继续轮询以建立与前端节点的连接,直到它最终能够成功连接。 I am quite sure I am not supposed to use an infinite while loop with a try-catch block as that is inefficient and bad practice:我很确定我不应该对 try-catch 块使用无限循环,因为这是低效且不好的做法:

try{

     transport.open();
} catch() {

}

What is the best way to do this?做这个的最好方式是什么?

Given, I understand it right and the FE should connect the BE server.鉴于,我理解正确,FE应该连接BE服务器。 Then, the basic idea is this:那么,基本思路是这样的:

  • start the BE Thrift Server启动 BE Thrift服务器
  • have FE Thrift Client(s) connect to it让 FE Thrift客户端连接到它

Since the BE is intended to act as a Server, we have to start a Thrift Server , not a client:由于 BE 旨在充当服务器,因此我们必须启动Thrift 服务器,而不是客户端:

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();
  }
}

The infinite loop you are looking for is buried in server.serve();您正在寻找的无限循环隐藏在server.serve(); . . And if the idea is really backwards from what you wrote (you have variables named FEport ) and what you want is really to have BE connecting FE, then you have to switch all of the above: BE=client, FE=server.如果这个想法真的与你写的相反(你有名为FEport的变量)并且你真正想要的是让 BE 连接 FE,那么你必须切换上述所有内容:BE=client, FE=server.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用服务器Java和Javascript客户端使用Apache Thrift进行阵列 - Array with Apache Thrift using server Java & Javascript Client 处理字符串的正确方法是什么:Java客户端和C ++服务器 - What is the proper way to handle strings : Java client & C++ server 在服务器-客户端结构中向特定客户端发送消息的正确方法是什么? - What is the proper way to send a message to a particular client in a server-client structure? 带有Eclipse NoClassDefFoundError的节俭服务器/客户端 - Thrift server/client with Eclipse NoClassDefFoundError 使用Spring和DBCP处理JDBC连接的正确方法是什么? - What's the proper way to handle JDBC connections with Spring and DBCP? 使用模块初始化Jersey客户端的正确方法是什么? - What is the proper way to initialize Jersey client with a module? Apache Thrift教程客户端在使用2个客户端时陷入困境 - 如何使服务器执行多任务? - Apache Thrift tutorial client stuck when using 2 clients - how to make the server multitask? 使用Apache Thrift的第一个程序 - 我应该在哪里定义接口? 在客户端或服务器代码中 - First program with Apache Thrift - Where should I define the interface? in client or server code Apache Thrift 2路SSL相互认证 - Apache Thrift 2-way SSL mutual authentication 如何在java中集合thrift客户端(或至少重用tcp连接) - How to pool a thrift client (or at least reuse the tcp connections) in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM