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

标签 java thrift

我想在同一个程序中运行两个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

该解决方案不必包含两个 TThreadedPoolServer,但如果不包含,我将需要帮助来启动它们。

最佳答案

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.

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

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

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

关于java - 如何运行多个T(Threaded)Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59850914/

相关文章:

java - Jersey 方法不允许 405

java - Cassandra LongType 列名转字符串问题

hadoop - Thrift、Avro、Protocolbuffers——它们都死了吗?

java - Android httpGET JSON 数组

java - 使用 a4j :commandbutton vs h:commandbutton to present-new-pages in richfaces

java - 在 Java 中使用 new Thread(foo).start() 。寻找最佳实践

java - 内存泄漏运行 Apache Thrift 服务器

python - 在 Windows 7 32 位上安装带有 Python 绑定(bind)的 Thrift

build - Cmake 自动生成节俭代码

java - 有没有等同于@Startup的spring?