Netty学习笔记(三)Reactor线程模型

2018-11-11  本文已影响30人  云师兄

单线程模型

所有操作都在同一个NIO线程处理,在这个单线程中要负责接收请求,处理IO,编解码所有操作,相当于一个饭馆只有一个人,同时负责前台和后台服务,效率低。


单线程模型

多线程模型

多线程的优点在于有单独的一个线程去处理请求,另外有一个线程池创建多个NIO线程去处理IO。相当于一个饭馆有一个前台负责接待,有很多服务员去做后面的工作,这样效率就比单线程模型提高很多。


多线程模型

主从线程模型

多线程模型的缺点在于并发量很高的情况下,只有一个Reactor单线程去处理是来不及的,就像饭馆只有一个前台接待很多客人也是不够的。为此需要使用主从线程模型。
主从线程模型:一组线程池接收请求,一组线程池处理IO。


主从线程模型

Netty采用了第三种模型:主从线程模型。
在这个模型下,Netty提供了BootStrap类方便我们快速开发,下面是一个示例代码:

public class Server {
    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
                    .handler(new ServerHandler())
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                        }
                    });
            ChannelFuture f = b.bind(8888).sync();
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

下面我们对Netty示例代码进行分析:

设置ChannelHandler

现在单独分析下处理类handler,每一个Channel由多个handler共同组成管道pipeline。

ChannelHander
管道中的handler可以用netty官方提供的处理类,也可以自行定义处理类。在上面的示例代码中,childHandler方法中传入ChannelInitializer对象,它的initChannel()方法中可以自行扩展,下面是一个具体的例子:
public class CustomerHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

        Channel channel = ctx.channel(); // 获取通道
        System.out.println(channel.remoteAddress()); // 显示客户端的远程地址
        ByteBuf content = Unpooled.copiedBuffer("hello,netty", CharsetUtil.UTF_8);// 定义要返回的数据

        // 定义响应
        FullHttpResponse response =
                new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);

        // 定义请求头
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());

        ctx.writeAndFlush(response);

    }
}

这是个自定义的处理类,它继承SimpleChannelInboundHandler这个初始化类,在channelRead0方法内部实现读写缓冲区的操作:首先从上下文中获取连接后的通道,然后创建ByteBuf对象,里面保存要显示的字符串,接着创建一个Http response 对象,设置返回的报文头和内容,最后使用上下文放松请求,注意要使用writeAndFlush而不是write,这是因为没有执行flush操作,数据仍在缓冲区中。
写好了上面这个自定义的处理类后将它配置到启动类中:

            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
                    .handler(new ServerHandler())
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel channel) {
                            ChannelPipeline pipeline = channel.pipeline();
                            pipeline.addLast("HttpServerCodec",new HttpServerCodec());
                            pipeline.addLast("customerHandler",new CustomerHandler());
                        }
                    });

可以看出在childHandler中,pipeline添加了两个处理类,一个是HttpServerCodec,是Netty自带的对请求和响应进行编解码的处理类;另一个就是我们创建的自定义处理类。这样启动这个应用后,在浏览器访问http://localhost:8888/地址后,页面上就会显示hello,netty的字样,说明我们添加的处理类已经生效了。

上一篇 下一篇

猜你喜欢

热点阅读