[英]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:
那么,基本思路是这样的:
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.