dubbo 2.5.10 版本,netty仍然使用的是netty的3.10.5版本,我们从下面的代码可以看出,SPI默认使用的是“netty”,而不是“netty4”。
package com.alibaba.dubbo.remoting; import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.extension.Adaptive; import com.alibaba.dubbo.common.extension.SPI; import javax.sound.midi.Receiver; /** * Transporter. (SPI, Singleton, ThreadSafe) * <p> * <a href="http://en.wikipedia.org/wiki/Transport_Layer">Transport Layer</a> * <a href="http://en.wikipedia.org/wiki/Client%E2%80%93server_model">Client/Server</a> * * @see com.alibaba.dubbo.remoting.Transporters */ @SPI("netty") public interface Transporter {
不管是NettyClient,还是NettyServer创建Channel的工厂类ChannelFactory地创建方式都是一样的,代码如下:
// ChannelFactory's closure has a DirectMemory leak, using static to avoid // https://issues.jboss.org/browse/NETTY-424 private static final ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(new NamedThreadFactory("NettyClientBoss", true)), Executors.newCachedThreadPool(new NamedThreadFactory("NettyClientWorker", true)), Constants.DEFAULT_IO_THREADS);
顾名思义,ChannelFactory类是用来创建Channel的,那Channel是用来做什么的呢?一句话概括就是:所有与I/O相关的操作都是由Channel来实现的。从上面的代码可以看出Dubbo创建了2个I/O线程池,分别为Boss线程池和Workder线程池,这2个线程池都是初始化为“无边界”的cached线程池,也就是说刚开始都是“来者不拒”,但实际上Boss线程默认最大只允许1个线程,而Work线程池最大为Constants.DEFAULT_IO_THREADS指定的线程数,即:CPU核数+1与32二者取最小值。代码如下:
private static final int DEFAULT_BOSS_COUNT = 1; public NioClientSocketChannelFactory( Executor bossExecutor, Executor workerExecutor, int workerCount) { this(bossExecutor, workerExecutor, DEFAULT_BOSS_COUNT, workerCount); }
public static final int DEFAULT_IO_THREADS = Math.min(Runtime.getRuntime().availableProcessors() + 1, 32);