Netty解读 - 1.从ServerBootstrap认识Netty

版权声明:本文为博主原创文章,未经博主允许不得转载。

摘要

该系列文章主要是分析Netty源码4.1.16.Final,了解Netty框架的设计和其中各种组件的优化手段。
这篇文章作为系列的第一篇文章,以最简单的EchoServer为例,从ServerBootstrap开始跟踪其初始化和启动流程,让读者在直观上对Netty框架有大概的认识。

EchoServer

我们从Netty提供的EchoServer开始,重要的代码片段如下:

        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .option(ChannelOption.SO_BACKLOG, 100)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ChannelPipeline p = ch.pipeline();
                     if (sslCtx != null) {
                         p.addLast(sslCtx.newHandler(ch.alloc()));
                     }
                     //p.addLast(new LoggingHandler(LogLevel.INFO));
                     p.addLast(new EchoServerHandler());
                 }
             });

            // Start the server.
            ChannelFuture f = b.bind(PORT).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down all event loops to terminate all threads.
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

ServerBootstrap

ServerBootstrap作为一个启动辅助类,通过他可以很方便的创建一个Netty服务端。从EchoServer可以看出,实现一个Server大概分为这样几个步骤:

  1. 设置bossGroup和workGroup。这里可以先理解为两个线程池,bossGroup设置一个线程,用于处理连接请求和建立连接,而workGroup线程池大小默认值2*CPU核数,在连接建立之后处理IO请求。EventLoopGroup体现了Netty对线程模型的抽象设计,之后会独立开篇介绍。
  2. 指定使用NioServerSocketChannel来处理连接请求。channel(NioServerSocketChannel.class)这段代码实际上在设置channelFactory,而ServerBootstrap会通过channelFactory.newChannel来生产channel。这里channelFactory是一个ReflectiveChannelFactory,顾名思义这个工厂类以反射的方式来构建channel实例,而实例的类型就是我们指定的NioServerSocketChannel。
public T newChannel() {
    ...  
        return clazz.getConstructor().newInstance();
    ...
}
  1. 配置TCP参数。
  2. 配置handler和childHandler,数据处理器。
  3. ServerBootstrap启动服务器。

真正的启动过程由ChannelFuture f = b.bind(PORT).sync();开始触发。调用链路:ServerBootstrap.bind → AbstractBootstrap.bind → AbstractBootstrap.doBind,重点分析一下doBind:

private ChannelFuture doBind(final SocketAddress localAddress) {
        final ChannelFuture regFuture = initAndRegister();
        final Channel channel = regFuture.channel();
        ...
            ChannelPromise promise = channel.newPromise();
            doBind0(regFuture, channel, localAddress, promise);
            return promise;
        ...
    }

这个过程分为两个关键步骤:

  1. initAndRegister - 包括Channel的创建、初始化和注册三个子步骤
  2. doBind0 - Channel绑定到监听端口

1.1 NioServerSocketChannel创建

final ChannelFuture initAndRegister() {
    ...
    channel = channelFactory.newChannel();
    init(channel);
    ...
    ChannelFuture regFuture = config().group().register(channel);
    ...
    return regFuture;
}

这里channelFactory就是上面提到的ReflectiveChannelFactory,他通过反射来创建NioServerSocketChannel实例,继续跟进NioServerSocketChannel的构造方法:

private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();

public NioServerSocketChannel() {
      this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}

private static ServerSocketChannel newSocket(SelectorProvider provider) {
      ...
      return provider.openServerSocketChannel();
      ...
}

public NioServerSocketChannel(ServerSocketChannel channel) {
      super(null, channel, SelectionKey.OP_ACCEPT);
      config = new NioServerSocketChannelConfig(this, javaChannel().socket());
}

因为获取provider的过程包含同步访问的消耗,所以NioServerSocketChannel缓存了provider并使用provider.openServerSocketChannel()来创建ServerSocketChannel实例。

除了创建ServerSocketChannel外,还要创建NioServerSocketChannelConfig。ChannelConfig的职责是统一保存channel配置,并提供读取和设置的接口。不同的ChannelConfig具体实现不一样,例如NioServerSocketChannelConfig提供的setReuseAddress接口,是为ServerSocketChannel设置useAddress参数。

NioServerSocketChannel的构造过程并没有这么简单,还要继续向上看看父类的构造函数。


NioServerSocketChannel类图.png

先来看AbstractNioChannel的构造方法:

protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
    super(parent);
    this.ch = ch;
    this.readInterestOp = readInterestOp;
    ...
       //设置channel非阻塞模式
       ch.configureBlocking(false);
    ...
   }
}

接下来是AbstractChannel类的构造方法:

protected AbstractChannel(Channel parent) {
    this.parent = parent;
    id = newId();
    unsafe = newUnsafe();
    pipeline = newChannelPipeline();
}

代码清楚的交代了3件事情:

  1. 为当前channel生成ID(由机器名,进程名,序列号,进程号和随机数组成)。
  2. 创建Unsafe实例(NioMessageUnSafe)。所有的UnSafe类都主要用于网络方面的操作,比如read,write,close等,Channel关于网络的操作都会委托给该unsafe来完成。
  3. 创建DefaultChannelPipeline,他就是大名鼎鼎的Pipeline机制。先从DefaultChannelPipeline构造函数入手:
protected DefaultChannelPipeline(Channel channel) {
     this.channel = ObjectUtil.checkNotNull(channel, "channel");
     ...
     tail = new TailContext(this);
     head = new HeadContext(this);
     head.next = tail;
     tail.prev = head;
}

pipeline内部会组织一个ChannelHandlerContext的链表,上下行的事件都会经过这个链表依次处理。但是在构造Pipeline时,就只有两个ChannelHandlerContext:HeadContext主要用于处理输出事件,而TailContext用于处理输入事件。用户添加的Handler也会被封装在新的Context然后插入到链表中,参与事件的处理,这一点后面会再提到。
先来看看TailContext构造函数:

TailContext(DefaultChannelPipeline pipeline) {
      super(pipeline, null, TAIL_NAME, true, false);
      ...
 }

AbstractChannelHandlerContext(DefaultChannelPipeline pipeline, EventExecutor executor, String name,
                                  boolean inbound, boolean outbound) {
      this.name = ObjectUtil.checkNotNull(name, "name");
      this.pipeline = pipeline;
      this.executor = executor;
      this.inbound = inbound;
      this.outbound = outbound;
      ordered = executor == null || executor instanceof OrderedEventExecutor;
}

值得注意的是inbound和outbound两个属性,TailContext(inbound=true,outbound=false),而HeadContext刚好相反(inbound=false,outbound=true),这两个属性由于标识该Context是用于处理输入事件还是输出事件的,当事件处理时会根据是事件输入还是输出而选择相应的Context来处理。
再来看HeadContext:

HeadContext(DefaultChannelPipeline pipeline) {
      super(pipeline, null, HEAD_NAME, false, true);
      unsafe = pipeline.channel().unsafe();
      ...
}

与TailContext
相比,HeadContext多了unsafe属性,是从pipeline中的channel中获取的,即NioSocketChannel构造函数中提到的unsafe对象。TailContext实现的很多方法都是空的,不做任何处理;而HeadContext实现的方法就比较多了,包括bind,connect,disconnect,read,write等,都是依赖unsafe来完成的。

到这里NioServerSocketChannel的创建过程才算结束,总结一下这个过程都包含了那些步骤:

  • ServerBootStrap通过ReflectiveChannelFactory创建NioServerSocketChannel实例。
  • NioServerSocketChannel实例内部创建了ServerSocketChannel。
  • 每个Channel(AbstractChannel)内部都会创建ChannelID,unsafe以及pipeline。

1.2 NioServerSocketChannel初始化

回到AbstractBootstrap.initAndRegister

final ChannelFuture initAndRegister() {
    ...
    channel = channelFactory.newChannel();
    init(channel);
    ...
    ChannelFuture regFuture = config().group().register(channel);
    ...
    return regFuture;
}

上一节介绍了channel的创建过程,接下来再看ServerBootstrap.init

void init(Channel channel) throws Exception {
        final Map<ChannelOption<?>, Object> options = options0();
        synchronized (options) {
            setChannelOptions(channel, options, logger);
        }

        final Map<AttributeKey<?>, Object> attrs = attrs0();
        synchronized (attrs) {
            for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
                @SuppressWarnings("unchecked")
                AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
                channel.attr(key).set(e.getValue());
            }
        }

        ChannelPipeline p = channel.pipeline();

       ...
        synchronized (childOptions) {
            currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
        }
        synchronized (childAttrs) {
            currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
        }

        p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(final Channel ch) throws Exception {
                final ChannelPipeline pipeline = ch.pipeline();
                ChannelHandler handler = config.handler();
                if (handler != null) {
                    pipeline.addLast(handler);
                }

                ch.eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        pipeline.addLast(new ServerBootstrapAcceptor(
                                ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                    }
                });
            }
        });
    }

该初始化过程可以划分为两个部分:

  1. 设置channel的配置参数和附属属性。
  2. 向channel的pipeline添加handler。这里的handler是通过ServerBootstrap.handler设置的,是在连接建立后的通用handler。

重点来看最后提到的ServerBootstrapAcceptor是什么?首先他是一个ChannelHandler,也会参与到事件的处理中,其次事件处理过程channelRead值得注意:

public void channelRead(ChannelHandlerContext ctx, Object msg) {
      final Channel child = (Channel) msg;
      child.pipeline().addLast(childHandler); 
      ...
         childGroup.register(child).addListener(new ChannelFutureListener() {
                    ...
         });
     ...
}

当新的连接SocketChannel建立后,会被包装在一个新建的NioSocketChannel中,之后开始触发pipeline.fireChannelRead,最终经过ServerBootstrapAcceptor.channelRead。在这里开始为这个NioSocketChannel添加childHandler,而childHandler则是我们在ServerBootstrap中指定的childHandler。接着在childGroup(也就是我们配置的workerGroup)中注册

1.3 NioServerSocketChannel注册

2.1 NioServerSocketChannel绑定

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容