java Netty 线程模型在客户端连接多的情况下是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7895964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:53:11  来源:igfitidea点击:

How does the Netty threading model work in the case of many client connections?

javaasynchronousnetwork-programmingnionetty

提问by Jiddo

I intend to use Netty in an upcoming project. This project will act as both client and server. Especially it will establish and maintain many connections to various servers while at the same time serving its own clients.

我打算在即将到来的项目中使用 Netty。该项目将同时充当客户端和服务器。特别是它会在为自己的客户端提供服务的同时建立和维护与各种服务器的许多连接。

Now, the documentation for NioServerSocketChannelFactoryfairly specifies the threading model for the server side of things fairly well - each bound listen port will require a dedicated bossthread throughout the process, while connected clients will be handled in a non-blocking fashion on workerthreads. Specifically, one worker thread will be able to handle multiple connected clients.

现在,NioServerSocketChannelFactory的文档相当好地指定了服务器端的线程模型——每个绑定的侦听端口在整个过程中都需要一个专用的老板线程,而连接的客户端将在工作线程上以非阻塞方式处理。具体来说,一个工作线程将能够处理多个连接的客户端。

However, the documentation for NioClientSocketChannelFactoryis less specific. This also seems to utilize both bossand workerthreads. However, the documentation states:

但是,NioClientSocketChannelFactory的文档不太具体。这似乎也同时利用了老板工人线程。但是,文档指出:

One NioClientSocketChannelFactory has one boss thread. It makes a connection attempt on request. Once a connection attempt succeeds, the boss thread passes the connected Channel to one of the worker threads that the NioClientSocketChannelFactory manages.

一个 NioClientSocketChannelFactory 有一个 boss 线程。它根据请求进行连接尝试。一旦连接尝试成功,boss 线程将连接的 Channel 传递给 NioClientSocketChannelFactory 管理的工作线程之一。

Worker threads seem to function in the same way as for the server case too.

工作线程似乎也以与服务器案例相同的方式运行。

My question is, does this mean that there will be one dedicated bossthread for each connection from my program to an external server? How will this scale if I establish hundreds, or thousands of such connections?

我的问题是,这是否意味着从我的程序到外部服务器的每个连接都会有一个专用的老板线程?如果我建立数百或数千个这样的连接,这将如何扩展?

As a side note. Are there any adverse side effects for re-using a single Executor (cached thread pool) as both the bossExecutorand workerExecutorfor a ChannelFactory? What about also re-using between different client and/or server ChannelFactory instances? This is somewhat discussed here, but I do not find those answers specific enough. Could anyone elaborate on this?

作为旁注。将单个 Executor(缓存线程池)重新用作 ChannelFactory的bossExecutorworkerExecutor是否有任何不利的副作用?在不同的客户端和/或服务器 ChannelFactory 实例之间重用怎么样?这在此处有所讨论,但我认为这些答案不够具体。有人可以详细说明一下吗?

采纳答案by Abe

This is not a real answer to your question regarding how the Netty client thread model works. But you can use the same NioClientSocketChannelFactoryto create single ClientBootstrapwith multiple ChannelPipelineFactorys , and in turn make a large number of connections. Take a look at the example below.

这不是您关于 Netty 客户端线程模型如何工作的问题的真正答案。但是您可以使用相同的NioClientSocketChannelFactory方法创建ClientBootstrap带有多个ChannelPipelineFactorys 的single ,进而建立大量连接。看看下面的例子。

public static void main(String[] args)
{
    String host = "localhost";
    int port = 8090;
    ChannelFactory factory = new NioClientSocketChannelFactory(Executors
            .newCachedThreadPool(), Executors.newCachedThreadPool());
    MyHandler handler1 = new MyHandler();
    PipelineFactory factory1 = new PipelineFactory(handler1);
    AnotherHandler handler2 = new AnotherHandler();
    PipelineFactory factory2 = new PipelineFactory(handler2);
    ClientBootstrap bootstrap = new ClientBootstrap(factory);
    // At client side option is tcpNoDelay and at server child.tcpNoDelay
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);
    for (int i = 1; i<=50;i++){
        if(i%2==0){
            bootstrap.setPipelineFactory(factory1);
        }else{
            bootstrap.setPipelineFactory(factory2);
        }

        ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
                port));

        future.addListener(new ChannelFutureListener()
        {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception
            {
                future.getChannel().write("SUCCESS");
            }
        });
    }
}

It also shows how different pipeline factories can be set for different connections, so based on the connection you make you can tweak your encoders/decoders in the channel pipeline.

它还显示了如何为不同的连接设置不同的管道工厂,因此根据您建立的连接,您可以调整通道管道中的编码器/解码器。

回答by jpayne

I am not sure your question has been answer. Here's my answer: there's a single Boss thread that is managing simultaneously all the pending CONNECTs in your app. It uses nio to process all the current connects in a single (Boss) thread, and then hands each successfully connected channel off to one of the workers.

我不确定你的问题是否得到了回答。这是我的答案:有一个 Boss 线程同时管理您的应用程序中所有待处理的 CONNECT。它使用 nio 在单个(Boss)线程中处理所有当前连接,然后将每个成功连接的通道交给其中一个工作人员。

回答by Dominic Cerisano

Your question mainly concerns performance. Single threads scale very well on the client.

您的问题主要涉及性能。单线程在客户端上扩展性非常好。

Oh, and nabble has been closed. You can still browse the archive there.

哦,nabble 已经关闭了。您仍然可以在那里浏览存档。