本文介绍了如何在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.

现在,该文档的的相当指定的东西服务器端的线程模型相当好 - 每个绑定的监听端口,就需要一个专门的老板的线程,而连接的客户端会在上工人的线程一个非阻塞的方式来处理。特别是,一个工作线程将能够处理多个连接的客户端。

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 是那么具体。这似乎也同时利用老板的和的工人的线程。但是,文档指出:

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

一NioClientSocketChannelFactory有一个老板线程。它使请求的连接尝试。一旦连接尝试成功,老板线程传递连接的通道,以使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 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?

作为一个方面说明。是否有任何不良副作用重新使用一个单一的执行人(缓存的线程池)为两个的 bossExecutor 的和的 workerExecutor 的一个的ChannelFactory?什么不同的客户端和/或服务器的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 以创建一个 ClientBootstrap 有多个 ChannelPipelineFactory s,而反过来又使大量的连接。看看下面的例子。

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");
            }
        });
    }
}

这也显示了如何不同的流水线工厂可以为不同的连接根据你让你可以调整你的连接codeRS /解codeRS在渠道管道在连接上进行设置,因此

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的线程在许多客户端连接的情况下,模型的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:20
查看更多