如何运行多个T(Threaded)Server?

How to run multiple T(Threaded)Server?

我想运行两个TThreadedPoolServers在同一个程序中处理不同种类的请求。但是,如果客户端尝试连接到最后启动的 TThreadedPoolServer,它将始终拒绝连接。我已经尝试 运行 不同线程中的服务器,但无济于事。

服务器 1:

public class RERunner extends Thread {
    private static ReceiveEndpoint receiveEndpoint;
    private static ReceiveEndpointInterface.Processor<ReceiveEndpoint> masterProcessor;

    public RERunner() throws TTransportException {
        receiveEndpoint = new ReceiveEndpoint();
        masterProcessor = new ReceiveEndpointInterface.Processor<>(receiveEndpoint);

        TServerTransport transport = new TServerSocket(Elements.MASTER_SERVER_RC_ENDPOINT_PORT);
        TSimpleServer server = new TSimpleServer(new TSimpleServer.Args(transport).processor(masterProcessor));
        server.serve();
    }
}

服务器 2:

public class RETableRunner extends Thread {
    private static ReceiveEndpointTable receiveEndpointTable;
    private static ReceiveEndpointTableInterface.Processor<ReceiveEndpointTable> masterProcessorTable;

    public RETableRunner() throws TTransportException {
        receiveEndpointTable = new ReceiveEndpointTable();
        masterProcessorTable = new ReceiveEndpointTableInterface.Processor<>(receiveEndpointTable);

        TServerTransport transportAll = new TServerSocket(Elements.MASTER_SERVER_RC_ENDPOINT_TABLE_PORT);
        TThreadPoolServer serverAll = new TThreadPoolServer(new TThreadPoolServer.Args(transportAll).processor(masterProcessorTable));
        serverAll.serve();
    }
}

客户端抛出异常:

org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
    at org.apache.thrift.transport.TSocket.open(TSocket.java:226)
    at DataPropagator.acquireEndpointTable(DataPropagator.java:27)
    at DataPropagator.run(DataPropagator.java:98)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/java.net.PlainSocketImpl.connect0(Native Method)
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)  
    at org.apache.thrift.transport.TSocket.open(TSocket.java:221)
    ... 2 more

解决方案不必包含两个 TThreadedPoolServers,但如果没有,我需要帮助启动它们。

我将这两个*.thrift文件和其中定义的服务组合在一起,这样我就可以通过一个TThreadedServer来访问这两个功能。

However, the TThreadedPoolServer that starts last will always refuse the connection if a client tries to connect to it. I've already tried to run the servers in different threads, but to no avail.

这与其说是(线程)服务器的问题,不如说是您在尝试将两个服务器绑定到一个套接字时 运行 遇到的一个普遍问题。

当您需要 运行 同一端点上的两个或多个服务器时,一种可能的解决方案是使用 multiplex protocol

如果旧客户端(未使用 TMultiplexProtocol)正在联系服务器,一些(并非全部)实现还提供对默认服务的兼容性回退。如果该实现当前不支持它,请注意将 TMultiplexProtocol 添加到现有和已发布的端点是一项重大更改。