温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - How to run multiple T(Threaded)Server?

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

发布于 2020-03-27 16:16:30

我想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,但是如果没有,我将需要帮助来启动它们。

查看更多

查看更多

提问者
UmBottesWillen
被浏览
24
JensG 2020-01-31 17:13

但是,如果客户端尝试连接到最后启动的TThreadedPoolServer,它将始终拒绝该连接。我已经尝试过在不同线程中运行服务器,但无济于事。

这不是(线程)服务器的问题,而是想将两个服务器绑定到一个套接字时会遇到的一个普遍问题。

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

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览