netty服务端初始化源码解析

Netty 服务端初始化

一、服务端启动代码示例

package com.ccy.nettyTest;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author 陈灿勇
 * @version 0.0.1
 * @since 14/02/2020 00:26
 */
public class NettyServer {
    public static void main(String[] args) {

        // 1. 初始化两个 NioEventLoopGroup,
        // bossGroup为主Reactor,workerGroup为从Reactor
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            // 2. 初始化引导
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 3. 配置引导
            // 设置主从线程
            serverBootstrap.group(bossGroup, workerGroup)
                    // 设置主Reactor的管道类型
                    .channel(NioServerSocketChannel.class)
                    // 设置主管道的属性
                    .option(ChannelOption.SO_BACKLOG, 128)
                    // 设置接收到的管道的初始化方法
                    .childHandler(new TestChannelInitializer());
            // 同步绑定端口
            ChannelFuture sync = serverBootstrap.bind(new InetSocketAddress(9876)).sync();
            // 同步监听服务端通道是否关闭
            sync.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 关闭
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

二、绑定端口方法

public ChannelFuture bind(SocketAddress localAddress) {
    // 检查当前启动引导是否有效
    validate();
    // 调用doBind()
    return doBind(ObjectUtil.checkNotNull(localAddress, "localAddress"));
}


private ChannelFuture doBind(final SocketAddress localAddress) {
    // 初始化以及注册到 eventloop
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
    if (regFuture.cause() != null) {
        return regFuture;
    }
    if (regFuture.isDone()) {
        ChannelPromise promise = channel.newPromise();
        doBind0(regFuture, channel, localAddress, promise);
        return promise;
    } else {
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Throwable cause = future.cause();
                if (cause != null) {
                    promise.setFailure(cause);
                } else {
                    promise.registered();
                    doBind0(regFuture, channel, localAddress, promise);
                }
            }
        });
        return promise;
    }
}

三、初始化server端管道以及注册

final ChannelFuture initAndRegister() {
    Channel channel = null;
    try {
        // 创建一个管道,根据示例配置信息,会返回一个NioServerSocketChannel类型的管道
        channel = channelFactory.newChannel();
		// 初始化管道
        init(channel);
    } catch (Throwable t) {
        if (channel != null) {
            channel.unsafe().closeForcibly();
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
        }
        return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
    }
    // 将管道注册到bossGroup中
    ChannelFuture regFuture = config().group().register(channel);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly();
        }
    }
    return regFuture;
}


// 初始化管道方法,将配置信息进行注入
void init(Channel channel) {
    setChannelOptions(channel, options0().entrySet().toArray(EMPTY_OPTION_ARRAY), logger);
    setAttributes(channel, attrs0().entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
    // 获取管道对应流水线
    ChannelPipeline p = channel.pipeline();
    final EventLoopGroup currentChildGroup = childGroup;
    final ChannelHandler currentChildHandler = childHandler;
    
    final Entry<ChannelOption<?>, Object>[] currentChildOptions =
            childOptions.entrySet().toArray(EMPTY_OPTION_ARRAY);
    final Entry<AttributeKey<?>, Object>[] currentChildAttrs = 	
        						childAttrs.entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY);
    // 添加初始化时的handler
    p.addLast(new ChannelInitializer<Channel>() {
        @Override
        public void initChannel(final Channel ch) {
            
            final ChannelPipeline pipeline = ch.pipeline();
            // 如果有配置handler,则将配置的handler添加到handler链尾
            ChannelHandler handler = config.handler();
            
            if (handler != null) {
                pipeline.addLast(handler);
            }
            
            ch.eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    // 给serverSocketChannel添加一个类型为ServerBootstrapAcceptor的的处理器
                    // 对接收新客户端连接做处理
                    pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, 
                                              currentChildHandler, currentChildOptions,
                                              currentChildAttrs));
                }
            });
        }
    });
}
发布了17 篇原创文章 · 获赞 1 · 访问量 644

猜你喜欢

转载自blog.csdn.net/c_c_y_CC/article/details/104754358