Netty系列之服务端启动流程
服务端启动Demo
1 | public class NettyServer { |
从Demo可以看到前面基本上都是在加载参数,我们直接就看一下ChannelFuture future = serverBootstrap.bind(8080).sync();
的实现原理,先上一张流程图,原图地址:https://blog.csdn.net/akfly/article/details/53240028
bind()
1 | public ChannelFuture bind(SocketAddress localAddress) { |
doBind()
1 | private ChannelFuture doBind(final SocketAddress localAddress) { |
Channel注册
initAndRegister()——实例化一个regFuture
1 | final ChannelFuture initAndRegister() { |
通过反射机制实例化NioServerSocketChannel类
ReflectiveChannelFactory.newChannel()
1 | public T newChannel() { |
NioServerSocketChannel
1 | public NioServerSocketChannel() { |
NioServerSocketChannel的父类AbstractNioChannel中,在此类和其父类AbstractChannel中,初始化了pipeline和unsafe以及一些其他参数的属性。
1 | protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) { |
AbstractChannel
1 | protected AbstractChannel(Channel parent) { |
Channel注册selector
regFuture.cause()——判断Channel是否是CauseHolder类型,不是则返回null。
1 | public Throwable cause() { |