版本说明
<groupId>io.netty</groupId>
<artifactId>netty-parent</artifactId>
<version>4.1.37.Final</version>
1、常见的netty server端
//服务端
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
//就是一个死循环,不停地检测IO事件,处理IO事件,执行任务
//创建一个线程组:接受客户端连接 主线程
EventLoopGroup bossGroup = new NioEventLoopGroup(1);//cpu核心数*2
//创建一个线程组:接受网络操作 工作线程
EventLoopGroup workerGroup = new NioEventLoopGroup(); //cpu核心数*2
//是服务端的一个启动辅助类,通过给他设置一系列参数来绑定端口启动服务
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 我们需要两种类型的人干活,一个是老板,一个是工人,老板负责从外面接活,
// 接到的活分配给工人干,放到这里,bossGroup的作用就是不断地accept到新的连接,将新的连接丢给workerGroup来处理
serverBootstrap.group(bossGroup, workerGroup)
//设置使用NioServerSocketChannel作为服务器通道的实现
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128) //设置线程队列中等待连接的个数
.childOption(ChannelOption.SO_KEEPALIVE, true)//保持活动连接状态
//表示服务器启动过程中,需要经过哪些流程,这里NettyTestHendler最终的顶层接口为ChannelHander,
// 是netty的一大核心概念,表示数据流经过的处理器
.handler(new NettyTestHendler())
//表示一条新的连接进来之后,该怎么处理,也就是上面所说的,老板如何给工人配活
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder(), new NettyServerHendler());
}
});
System.out.println(".........server init..........");
// 这里就是真正的启动过程了,绑定9090端口,等待服务器启动完毕,才会进入下行代码
ChannelFuture future = serverBootstrap.bind(9090).sync();
System.out.println(".........server start..........");
//等待服务端关闭socket
future.channel().closeFuture().sync();
// 关闭两组死循环
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
2、创建事件循环组的流程
NioEventLoopGroup的继承体系非常复杂,所以其创建过程还是比较长的,请大家跟着源码,对着本文注释耐心阅读。
NioEventLoopGroup的创建流程:
1、默认无参构造方法线程数传的0
2、SelectorProvider.provider():为创建ServerSocketChannel做准备
3、DefaultSelectStrategyFactory.INSTANCE:默认选择策略工厂
4、RejectedExecutionHandlers.reject():线程池的拒绝策略
5、确定事件循环组的线程数,指定了线程数就使用指定线程数,为0(即没指定)就取CPU核数的两倍
6、DefaultEventExecutorChooserFactory.INSTANCE:默认事件执行选择器工厂
7、newDefaultThreadFactory:用于创建线程的线程工厂
8、ThreadPerTaskExecutor:每个task都开启一个线程去执行的执行器
9、循环创建NioEventLoop,循环次数就是线程数。事件循环组NioEventLoopGroup用来管理是事件循环NioEventLoop的,每个NioEventLoop都会绑定一个线程。
NioEventLoopGroup
public class NioEventLoopGroup extends MultithreadEventLoopGroup {
//默认无参构造方法,调用下一个构造器,线程数传的0
public NioEventLoopGroup() {
this(0);
}
//指定线程数 调用下一个构造器
public NioEventLoopGroup(int nThreads) {
this(nThreads, (Executor) null);
}
public NioEventLoopGroup(int nThreads, Executor executor) {
//executor默认为null
//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//调用下一个构造器
this(nThreads, executor, SelectorProvider.provider());
}
public NioEventLoopGroup(
int nThreads, Executor executor, final SelectorProvider selectorProvider) {
//nThreads默认为零
//executor默认为null
//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory() 默认选择策略工厂
//调用下一个构造器
this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
}
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
final SelectStrategyFactory selectStrategyFactory) {
//nThreads默认为零
//executor默认为null
//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施。
// 当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异常关闭。第二,任务数量超过线程池的最大限制。
//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler() ===>丢弃任务并抛出RejectedExecutionException异常。
//调用父类的构造器
super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
}
}
MultithreadEventLoopGroup
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup {
private static final int DEFAULT_EVENT_LOOP_THREADS;
static {
//默认线程数是CPU核数的两倍
DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
"io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
if (logger.isDebugEnabled()) {
logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
}
}
//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍
//调用父类构造
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
//nThreads默认为零
//executor默认为null
//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
}
}
MultithreadEventExecutorGroup
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup {
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
//nThreads默认为零 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍
//executor默认为null
//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
//DefaultEventExecutorChooserFactory.INSTANCE 默认事件执行选择器工厂
//调用下一个构造器
this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
}
/**
* 创建一个新实例。
*
* @param nThreads 这个实例将使用的线程数。
* @param executor 是要使用的执行器,如果使用默认值,则为{@code null}。
* @param chooserFactory 要使用的{@link EventExecutorChooserFactory}。
* @param args 参数将传递给每个{@link #newChild(Executor, Object…)}调用
*/
//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍
//executor默认为null
//chooserFactory=new DefaultEventExecutorChooserFactory() 默认 事件执行策略工厂
//args参数如下
//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
if (nThreads <= 0) {
throw new IllegalArgumentException(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if (executor == null) {
//newDefaultThreadFactory()=线程工厂 专门创建线程的
//newDefaultThreadFactory()调用的是 MultithreadEventLoopGroup.newDefaultThreadFactory()
//最终创建的是DefaultThreadFactory,他实现了继承自jdk的ThreadFactory
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍
children = new EventExecutor[nThreads];
for (int i = 0; i < nThreads; i ++) {
//出现异常标识
boolean success = false;
try {
//创建nThreads个nioEventLoop保存到children数组中
//NioEventLoopGroup.newChild
children[i] = newChild(executor, args);
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
//出现异常处理
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
//children是 new NioEventLoop() 的对象数组
//chooser=GenericEventExecutorChooser/PowerOfTwoEventExecutorChooser
//后续从children中选择一个线程 就是调用chooser.next()
chooser = chooserFactory.newChooser(children);
final FutureListener<Object> terminationListener = new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
if (terminatedChildren.incrementAndGet() == children.length) {
terminationFuture.setSuccess(null);
}
}
};
for (EventExecutor e: children) {
e.terminationFuture().addListener(terminationListener);
}
//复制一份children 只读的
Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
Collections.addAll(childrenSet, children);
readonlyChildren = Collections.unmodifiableSet(childrenSet);
}
}
NioEventLoopGroup实现了父类MultithreadEventExecutorGroup的newChild方法
public class NioEventLoopGroup extends MultithreadEventLoopGroup {
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
//executor=new ThreadPerTaskExecutor(newDefaultThreadFactory());
//args参数如下
//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
}
NioEventLoopGroup#newChild创建了NioEventLoop对象,一个事件循环组NioEventLoopGroup包含nThreads个事件循环NioEventLoop。接下来我们看NioEventLoop的创建流程
3 创建事件循环
同NioEventLoopGroup一样,NioEventLoop的继承体系非常复杂,所以其创建流程代码也比较长,实际上没干啥。(但很重要)
NioEventLoop创建流程:
1、父类构造方法:创建任务队列
2、性能优化的点:创建java nio的selector,替换SelectorImpl的两个属性值,把SelectorImpl中使用的set替换成netty自己实现更轻量级的set
NioEventLoop
public final class NioEventLoop extends SingleThreadEventLoop {
NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider,
SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
//NioEventLoopGroup.this
//executor=new ThreadPerTaskExecutor(newDefaultThreadFactory());
//addTaskWakesUp:添加完任务到队列后 是否唤醒事件循环中的阻塞的selector 默认为fasle 表示唤醒
//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的
//strategy=DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
//rejectedExecutionHandler=RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
//调用父类构造方法
super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
//provider=SelectorProvider.provider()
provider = selectorProvider;
//创建java nio的selector
//替换SelectorImpl的两个属性值
final SelectorTuple selectorTuple = openSelector();
//子类包装的selector 底层数据结构也是被替换了的
selector = selectorTuple.selector;
//原生selector 替换了数据结构selectedKeys
unwrappedSelector = selectorTuple.unwrappedSelector;
//selectStrategy=new DefaultSelectStrategyFactory()
selectStrategy = strategy;
}
private static final class SelectorTuple {
//子类包装的selector
final Selector unwrappedSelector;
//替换了数据结构selectedKeys publicSelectedKeys的selector
final Selector selector;
SelectorTuple(Selector unwrappedSelector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = unwrappedSelector;
}
SelectorTuple(Selector unwrappedSelector, Selector selector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = selector;
}
}
private SelectorTuple openSelector() {
final Selector unwrappedSelector;
try {
//调用nio api创建selector
unwrappedSelector = provider.openSelector();
} catch (IOException e) {
throw new ChannelException("failed to open a new selector", e);
}
// System.out.println(unwrappedSelector.getClass());
//默认为false 禁止keySet优化
if (DISABLE_KEY_SET_OPTIMIZATION) {
//将selector保存到SelectorTuple
return new SelectorTuple(unwrappedSelector);
}
//加载SelectorImpl类 得到SelectorImpl的类类型
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
return Class.forName(
"sun.nio.ch.SelectorImpl",
false,
PlatformDependent.getSystemClassLoader());
} catch (Throwable cause) {
return cause;
}
}
});
//默认不成立
if (!(maybeSelectorImplClass instanceof Class) ||
// isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角度去判断。
//isAssignableFrom()方法是判断是否为某个类的父类,instanceof关键字是判断是否某个类的子类。
//使用方法 父类.class.isAssignableFrom(子类.class) 子类实例 instanceof 父类类型
!((Class<?>) maybeSelectorImplClass).isAssignableFrom(unwrappedSelector.getClass())) {
if (maybeSelectorImplClass instanceof Throwable) {
Throwable t = (Throwable) maybeSelectorImplClass;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, t);
}
return new SelectorTuple(unwrappedSelector);
}
final Class<?> selectorImplClass = (Class<?>) maybeSelectorImplClass;
//netty自定义的Set
final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
Object maybeException = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
if (PlatformDependent.javaVersion() >= 9 && PlatformDependent.hasUnsafe()) {
// Let us try to use sun.misc.Unsafe to replace the SelectionKeySet.
// This allows us to also do this in Java9+ without any extra flags.
long selectedKeysFieldOffset = PlatformDependent.objectFieldOffset(selectedKeysField);
long publicSelectedKeysFieldOffset =
PlatformDependent.objectFieldOffset(publicSelectedKeysField);
if (selectedKeysFieldOffset != -1 && publicSelectedKeysFieldOffset != -1) {
PlatformDependent.putObject(
unwrappedSelector, selectedKeysFieldOffset, selectedKeySet);
PlatformDependent.putObject(
unwrappedSelector, publicSelectedKeysFieldOffset, selectedKeySet);
return null;
}
// We could not retrieve the offset, lets try reflection as last-resort.
}
Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField, true);
if (cause != null) {
return cause;
}
cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField, true);
if (cause != null) {
return cause;
}
//替换SelectorImpl中原先使用的Set实现类 替换成netty自己实现的类
selectedKeysField.set(unwrappedSelector, selectedKeySet);
publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
return null;
} catch (NoSuchFieldException e) {
return e;
} catch (IllegalAccessException e) {
return e;
}
}
});
if (maybeException instanceof Exception) {
selectedKeys = null;
Exception e = (Exception) maybeException;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, e);
return new SelectorTuple(unwrappedSelector);
}
selectedKeys = selectedKeySet;
logger.trace("instrumented a special java.util.Set into: {}", unwrappedSelector);
return new SelectorTuple(unwrappedSelector,
new SelectedSelectionKeySetSelector(unwrappedSelector, selectedKeySet));
}
}
SingleThreadEventLoop
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
//NioEventLoopGroup.this
//executor=new ThreadPerTaskExecutor(newDefaultThreadFactory());
// addTaskWakesUp=false
//maxPendingTasks=2147483647 默认最大挂起任务
//rejectedExecutionHandler ===》 new RejectedExecutionHandler()
//调用父类构造方法
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
//tailTasks=new LinkedBlockingQueue<Runnable>(maxPendingTasks);
tailTasks = newTaskQueue(maxPendingTasks);
}
}
SingleThreadEventExecutor
public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
/**
* 创建一个新实例
*
* @param parent {@link EventExecutorGroup},它是这个实例的父实例,属于它
* @param executor 将用于执行的{@link Executor}
* @param addTaskWakesUp 当且仅当调用{@link #addTask(Runnable)}将唤醒 执行程序线程
* @param maxPendingTasks 将拒绝新任务之前的最大挂起任务数。
* @param rejectedHandler 要使用的{@link RejectedExecutionHandler}。
*/
protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedHandler) {
//NioEventLoopGroup.this
//executor=new ThreadPerTaskExecutor(new DefaultThreadFactory());
// addTaskWakesUp=false
//maxPendingTasks=2147483647
//rejectedExecutionHandler ===》 new RejectedExecutionHandler()
//父类构造方法中省略
super(parent);
// addTaskWakesUp=false
this.addTaskWakesUp = addTaskWakesUp;
//2147483647
this.maxPendingTasks = Math.max(16, maxPendingTasks);
//executor=执行器
this.executor = ThreadExecutorMap.apply(executor, this);
//new LinkedBlockingQueue<Runnable>(2147483647);
taskQueue = newTaskQueue(this.maxPendingTasks);
//new RejectedExecutionHandler()
rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
}
}