官网地址

程序猿升级课

热爱开源,喜欢摸索


Netty组件介绍

在学习Netty之前,建议首先学习一个NIO,对关键的NIO组件有一个清醒认识

Buffer

Selector

总览

  • Bootstrap or ServerBootstrap
  • EventLoop
  • EventLoopGroup
  • ChannelPipeline
  • Future or ChannelFuture
  • ChannelInitializer
  • ChannelHandler

ServerBootstrap

一个Netty应用通常由一个Bootstrap开始,它主要作用是配置整个Netty程序,串联起各个组件。

EventLoop

一个EventLoop可以为多个Channel服务。 EventLoopGroup会包含多个EventLoop

ChannelPipeline,ChannelHandler

从PipeLine这个单词中可以看出来,是一个管道,处理连接。我们的业务代码handler一般都是放在这个管道中的

那么疑问来了,这个管道中的处理顺序是什么样呢?

 ChannelPipeline cp = channel.pipeline(); 
 cp.addLast("encoder", new HttpResponseEncoder());//1.负责输出
 cp.addLast("decoder", new HttpRequestDecoder());//2.负责把客户端的数据解码
 cp.addLast("handler", new HttpDispatchServerHandler());//3.自定义的业务处理器

按照我们执行顺序肯定不是根据添加顺序来处理的,应该是:2,把客户端的数据解码->3.对解码数据处理->1.加密返回给客户端。

那么 Netty 是怎么处理的呢?

4279695-e5d457c57e93ba23

ChannelHandler有两个子类ChannelInboundHandler和ChannelOutboundHandler,这两个类对应了两个数据流向,如果数据是从外部流入我们的应用程序,我们就看做是inbound,相反便是outbound

4279695-2adc122361ee68fa

ChannelInitializer

顾名思义,这个就是channel初始化的容器,在这个里面设置处理器

 ServerBootstrap bootstrap = new ServerBootstrap();
        bossGroup = new NioEventLoopGroup();//负责绑定channel到selector
        workerGroup = new NioEventLoopGroup();//负责从selector中读取事件
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .localAddress(6969).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel channel) throws Exception {
                ChannelPipeline cp = channel.pipeline();
                cp.addLast("idleStateHandler", new IdleStateHandler(5, 5, 5, TimeUnit.SECONDS));
                cp.addLast("decoder", new HttpRequestDecoder());
                cp.addLast("encoder", new HttpResponseEncoder());
                cp.addLast("aggregator", new HttpObjectAggregator(1048576));
                cp.addLast("deflater", new HttpContentCompressor());
                cp.addLast("handler", new HttpDispatchServerHandler());
                cp.addLast("out", new AcceptorIdleStateTrigger());
            }
        }).option(ChannelOption.SO_BACKLOG, 128);
        try {
            channel = bootstrap.bind().awaitUninterruptibly().channel();
            showBanner(6969);
        } catch (Exception ie) {
            throw new RuntimeException(ie);
        }

Future or ChannelFuture

Netty中的连接都可以是异步的,但是也可以设置为非异步

异步:

posted @ 2017-07-15 23:27  chinesszz  阅读(103)  评论(0编辑  收藏  举报
ヾ(≧O≦)〃嗷~