本文介绍了Netty 线程模型在客户端连接多的情况下如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,NioServerSocketChannelFactory 相当好地为服务器端指定了线程模型 - 每个绑定的监听端口在整个过程中都需要一个专用的 boss 线程,而连接的客户端将在一个非- worker 线程上的阻塞时尚.具体来说,一个工作线程将能够处理多个连接的客户端.

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

但是,NioClientSocketChannelFactory 不太具体.这似乎也利用了 bossworker 线程.但是,文档指出:

However, the documentation for NioClientSocketChannelFactory is less specific. This also seems to utilize both boss and worker threads. However, the documentation states:

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

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

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

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

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

作为旁注.将单个 Executor(缓存线程池)同时用作 ChannelFactory 的 bossExecutorworkerExecutor 是否有任何不利的副作用?在不同的客户端和/或服务器 ChannelFactory 实例之间重用呢?这里有些讨论,但我发现这些答案不够具体.谁能详细说明一下?

As a side note. Are there any adverse side effects for re-using a single Executor (cached thread pool) as both the bossExecutor and workerExecutor for 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?

推荐答案

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

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

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.

这篇关于Netty 线程模型在客户端连接多的情况下如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 15:05