第九章 Netty 初试

目录

        Netty模型

工作原理示意图1-简单版

工作原理示意图2-进阶版

工作原理示意图3-详细版  

Netty快速入门实例-TCP服务 

服务端

客户端

演示

TaskQueue 的 3 种典型使用场景

Netty 模型 再说明

异步模型

基本介绍

Future 说明

工作原理示意图

Future-Listener 机制

演示

Netty快速入门实例2-Http服务


Netty模型

工作原理示意图1-简单版

Netty 主要基于主从 Reactors 多线程模型(如图)做了一定的改进,其中主从 Reactor 多线程模型有多个 Reactor

image.png

  1. BossGroup 线程维护Selector , 只关注Accecpt
  2. 当接收到Accept事件,获取到对应的SocketChannel, 封装成 NIOScoketChannel并注册到Worker 线程(事件循环), 并进行维护
  3. 当Worker线程监听到selector 中通道发生自己感兴趣的事件后,就进行处理(就由handler), 注意handler 已经加入到通道

工作原理示意图2-进阶版

Netty 主要基于主从 Reactors 多线程模型(如图)做了一定的改进,其中主从 Reactor 多线程模型有多个 Reactor

image.png

工作原理示意图3-详细版  

  1. Netty抽象出两组线程池 BossGroup 专门负责接收客户端的连接, WorkerGroup 专门负责网络的读写
  2. BossGroup 和 WorkerGroup 类型都是 NioEventLoopGroup
  3. NioEventLoopGroup 相当于一个事件循环组, 这个组中含有多个事件循环 ,每一个事件循环是 NioEventLoop
  4. NioEventLoop 表示一个不断循环的执行处理任务的线程, 每个NioEventLoop 都有一个selector , 用于监听绑定在其上的socket的网络通讯
  5. NioEventLoopGroup 可以有多个线程, 即可以含有多个NioEventLoop
  6. 每个Boss NioEventLoop 循环执行的步骤有3步
    1. 轮询accept 事件
    2. 处理accept 事件 , 与client建立连接 , 生成NioScocketChannel , 并将其注册到某个worker NIOEventLoop 上的 selector  
    3. 处理任务队列的任务 , 即 runAllTasks
  7. 每个 Worker NIOEventLoop 循环执行的步骤
    1. 轮询read, write 事件
    2. 处理i/o事件, 即read , write 事件,在对应NioScocketChannel 处理
    3. 处理任务队列的任务 , 即 runAllTasks
  8. 每个Worker NIOEventLoop  处理业务时,会使用pipeline(管道), pipeline 中包含了 channel , 即通过pipeline 可以获取到对应通道, 管道中维护了很多的 处理器

image.png

Netty快速入门实例-TCP服务 

实例要求:

  1. Netty 服务器在 6668 端口监听,客户端能发送消息给服务器 "hello, 服务器~"
  2. 服务器可以回复消息给客户端 "hello, 客户端~"
  3. 目的:对Netty 线程模型 有一个初步认识, 便于理解Netty 模型理论

步骤:

  1. 编写服务端  
  2. 编写客户端
  3. 对netty 程序进行分析,看看netty模型特点

服务端

public class NettyServer {

    public static void main(String[] args) {

        // 1. 创建 两个线程组 bossGroup 和 workGroup
        // 2. bossGroup 知识处理连接请求,真正的业务处理会交给 workGroup 完成
        // 3. 两个都是无限循环
        // workGroup 和 bossGroup 的含有的子线程(NIOEventLoop)的个数 默认是CPU*2
        // DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
        //        "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
        // NettyRuntime.availableProcessors() 当前计算机CPU核心数
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workGroup = new NioEventLoopGroup(); //CPU核心数*2
        try {
            // 创建服务器端启动对象,并配置参数
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 使用链式编程来设置
            serverBootstrap.group(bossGroup, workGroup) // 设置两个线程组
                    .channel(NioServerSocketChannel.class) // 使用 NioServerSocketChannel 作为服务器端通道实现
                    .option(ChannelOption.SO_BACKLOG, 128) // 设置线程队列得到连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // 设置保持连接活动状态
//            .handler(null) 该 handler 对应bossGroup,childHandler 对应 workGroup
                    .childHandler(new ChannelInitializer<SocketChannel>() { // 创建一个通道初始化对象(匿名对象)
                        // 给pipeline 设置处理器
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            // 可以使用一个集合管理SocketChannel,在推送消息时,
                            // 可以将业务加入到各个Channel对应的NIOEventLoop的taskQueue中或者scheduleTaskQueue中
                            System.out.println("客户SocketChannel的HashCode是:" + ch.hashCode());
                            ch.pipeline().addLast(new NettyServerHandler());
                        }
                    }); // 给WorkGroup 的 EventLoop 对应的

            // 管道设置处理器
            System.out.println("服务器 is ready...");

            // 绑定一个端口,并且同步,生成一个 ChannelFuture 对象
            ChannelFuture cf = serverBootstrap.bind(6668).sync();
            // 给CF 注册监听器,监听我们关心的事件
            cf.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        System.out.println("监听6668端口成功");
                    } else {
                        System.out.println("监听6668端口失败");
                    }
                }
            });

            // 对关闭通道进行监听
            cf.channel().closeFuture().sync();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 优雅关闭
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}

NettyServerHandler.java

/**
 * 需要继承Netty 规定好的HandlerAdapter(规范),这个自定义Handler 才可以使用
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * 这里可以读取道客户端发送的数据
     *
     * @param ctx ChannelHandlerContext 上下文对象,含有管道pipeline,通道Channel,地址
     * @param msg 就是客户端发送的数据,默认Object
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {


        // 这里可以看出IO多路复用
        System.out.println("服务器读取线程 " + Thread.currentThread().getName() + " channel =" + ctx.channel());
        System.out.println("server ctx =" + ctx);
        System.out.println("看看channel 和 pipeline的关系");
        Channel channel = ctx.channel();
        ChannelPipeline pipeline = ctx.pipeline(); //本质是一个双向链接, 出站入站

        // 将 msg 转换成 为一个ByteBuf
        // ByteBuf 是netty提供的,不是NIO 提供的ByteBuffer
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("客户端发送的消息是:" + buf.toString(CharsetUtil.UTF_8));
        System.out.println("客户端地址:" + ctx.channel().remoteAddress());

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // writeAndFlush 是 write + flush
        // 将数据写入到缓存,并刷新
        // 一般,我们都会对这个数据进行编码
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端",CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

客户端

public class NettyClient {

    public static void main(String[] args) {
        // 客户端只需要一个事件循环组
        NioEventLoopGroup loopGroup = new NioEventLoopGroup();
        try {
            // 创建客户端启动对象,注意:这里是Bootstrap 而不是 ServerBootStrap
            Bootstrap bootstrap = new Bootstrap();
            // 设置相关参数
            bootstrap.group(loopGroup) // 设置线程组
                    .channel(NioSocketChannel.class) // 设置客户端通道实现类(反射)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyClientHandler()); // 加入自己的处理器
                        }
                    });
            System.out.println("客户端 is ok...");
            // 启动客户端 连接服务器
            // 关于ChannelFuture 要分析,涉及netty的异步模型
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
            // 关闭通道进行监听
            channelFuture.channel().closeFuture().sync();
        } catch (Exception ex) {
            // 优雅关闭
            loopGroup.shutdownGracefully();
        }
    }
}

NettyClientHandler.java

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * 当通道就绪就会触发该方法
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client:" + ctx);
        ctx.writeAndFlush(Unpooled.copiedBuffer("Hello Server", CharsetUtil.UTF_8));
    }

    /**
     * 当通道有读取事件时,会触发
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("服务器回复的消息:" + buf.toString(CharsetUtil.UTF_8));
        System.out.println("服务器地址:" + ctx.channel().remoteAddress());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

演示

image.png

image.png

TaskQueue 的 3 种典型使用场景

  1. 用户程序自定义的普通任务
  2. 用户自定义定时任务  
  3. 非当前 Reactor 线程调用 Channel 的各种方法  例如在推送系统的业务线程里面,根据用户的标识,找到对应的 Channel 引用,然后调用 Write 类方法向该用户推送消息,就会进入到这种场景。最终的 Write 会提交到任务队列中后被异步消费  

NIOServerHandler.java -- > channelRead

// 比如这里有一个非常耗时的任务,我们就需要异步执行 --> 提交到该channel 对应的NIOEventLoop 的 taskQueue中
// 方案一: 用户自定的普通任务
ctx.channel().eventLoop().execute(() -> {

    try {
        Thread.sleep(5 * 1000);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端2", CharsetUtil.UTF_8));
        System.out.println("channel code = " + ctx.channel().hashCode());

    } catch (Exception ex) {
        System.out.println("发生异常:" + ex.getMessage());
    }
});
ctx.channel().eventLoop().execute(() -> {

    try {
        Thread.sleep(5 * 1000);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端3", CharsetUtil.UTF_8));
        System.out.println("channel code = " + ctx.channel().hashCode());

    } catch (Exception ex) {
        System.out.println("发生异常:" + ex.getMessage());
    }
});

// 解决方案2:使用定时任务,任务提交到 scheduleTaskQueue中
ctx.channel().eventLoop().schedule(()->{
    try {
        Thread.sleep(5 * 1000);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端4", CharsetUtil.UTF_8));
        System.out.println("channel code = " + ctx.channel().hashCode());

    } catch (Exception ex) {
        System.out.println("发生异常:" + ex.getMessage());
    }
},5, TimeUnit.SECONDS);

System.out.println("go on ...");

Netty 模型 再说明

Netty 抽象出两组线程池,BossGroup 专门负责接收客户端连接,WorkerGroup 专门负责网络读写操作。

  • NioEventLoop 表示一个不断循环执行处理任务的线程,每个 NioEventLoop 都有一个 selector,用于监听绑定在其上的 socket 网络通道。
  • NioEventLoop 内部采用串行化设计,从消息的读取->解码->处理->编码->发送,始终由 IO 线程NioEventLoop 负责
  • NioEventLoopGroup 下包含多个 NioEventLoop
    • 每个 NioEventLoop 中包含有一个 Selector,一个 taskQueue
    • 每个 NioEventLoop 的 Selector 上可以注册监听多个 NioChannel
    • 每个 NioChannel 只会绑定在唯一的 NioEventLoop 上
    • 每个 NioChannel 都绑定有一个自己的 ChannelPipeline

异步模型

基本介绍

  • 异步的概念和同步相对。当一个异步过程调用发出后,调用者不能立刻得到结果。实际处理这个调用的组件在完成后,通过状态、通知和回调来通知调用者。
  • Netty 中的 I/O 操作是异步的,包括 Bind、Write、Connect 等操作会简单的返回一个 ChannelFuture。
  • 调用者并不能立刻获得结果,而是通过 Future-Listener 机制,用户可以方便的主动获取或者通过通知机制获得 IO 操作结果
  • Netty 的异步模型是建立在 future 和 callback 的之上的。callback 就是回调。重点说 Future,它的核心思想是:假设一个方法 fun,计算过程可能非常耗时,等待 fun返回显然不合适。那么可以在调用 fun 的时候,立马返回一个 Future,后续可以通过 Future去监控方法 fun 的处理过程(即 : Future-Listener 机制)

Future 说明

  1. 表示异步的执行结果, 可以通过它提供的方法来检测执行是否完成,比如检索计算等等.
  2. ChannelFuture 是一个接口 : public interface ChannelFuture extends Future<Void>我们可以添加监听器,当监听的事件发生时,就会通知到监听器.

工作原理示意图

image.png

说明:

在使用 Netty 进行编程时,拦截操作和转换出入站数据只需要您提供 callback 或利用future 即可。这使得链式操作简单、高效, 并有利于编写可重用的、通用的代码。

Netty 框架的目标就是让你的业务逻辑从网络基础应用编码中分离出来、解脱出来

Future-Listener 机制

  • 当 Future 对象刚刚创建时,处于非完成状态,调用者可以通过返回的 ChannelFuture 来获取操作执行的状态,注册监听函数来执行完成后的操作。
  • 常见有如下操作
    • 通过 isDone 方法来判断当前操作是否完成;
    • 通过 isSuccess 方法来判断已完成的当前操作是否成功;
    • 通过 getCause 方法来获取已完成的当前操作失败的原因;
    • 通过 isCancelled 方法来判断已完成的当前操作是否被取消;
    • 通过 addListener 方法来注册监听器,当操作已完成(isDone 方法返回完成),将会通知指定的监听器;如果 Future 对象已完成,则通知指定的监听器

演示

绑定端口是异步操作,当绑定操作处理完,将会调用相应的监听器处理逻辑

// 绑定一个端口,并且同步,生成一个 ChannelFuture 对象
ChannelFuture cf = serverBootstrap.bind(6668).sync();
// 给CF 注册监听器,监听我们关心的事件
cf.addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
        if (future.isSuccess()) {
            System.out.println("监听6668端口成功");
        } else {
            System.out.println("监听6668端口失败");
        }
    }
});

小结:相比传统阻塞 I/O,执行 I/O 操作后线程会被阻塞住, 直到操作完成;异步处理的好处是不会造成线程阻塞,线程在 I/O 操作期间可以执行别的程序,在高并发情形下会更稳定和更高的吞吐量

Netty快速入门实例2-Http服务

1. Netty 服务器在 6668 端口监听,浏览器发出请求 "http://localhost:6668/ "
2. 服务器可以回复消息给客户端 "Hello! 我是服务器 " ,  并对特定请求资源进行过滤.
3. 目的:Netty 也可以做Http服务开发,并且理解Handler实例和客户端及其请求的关系

image.png

 

HttpNettyServer.java

public class HttpNettyServer {

    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workGroup = new NioEventLoopGroup(); // CPU核心数*2
        try {
            // 创建服务器端启动对象,并配置参数
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 使用链式编程来设置
            serverBootstrap.group(bossGroup, workGroup) // 设置两个线程组
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpChannelInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(7892).sync();

            channelFuture.addListener((ChannelFutureListener) future -> {
                if(future.isSuccess()){
                    System.out.println("HTTP服务器 已经成功启动...");
                }
            });
            channelFuture.channel().closeFuture().sync();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 优雅关闭
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}

HttpChannelInitializer.java

public class HttpChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {

        // 向管道加入处理器
        // 获取 管道
        ChannelPipeline pipeline = ch.pipeline();
        // 加入netty 提供的 HttpSeverCodec  codec => [coder - decoder]
        // HttpSeverCodec 是 netty 提供的 处理Http 的编-解码器
        pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
        // 增加一个自定义的 handler
        pipeline.addLast("MyHttpServerHandler",new HttpServerHandler());

        System.out.println("ok~~~~");
    }
}

HttpServerHandler.java

/**
 * 1. SimpleChannelInboundHandler 继承自 ChannelInboundHandlerAdapter
 * 2. HttpObject 是 netty 封装的 用于客户端 和服务端通讯的协议
 */
public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    // 读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {

        System.out.println("1对应的channel=" + ctx.channel() + ",pipeline=" + ctx
                .pipeline() + " 通过pipeline获取channel" + ctx.pipeline().channel());

        System.out.println("2当前ctx的handler=" + ctx.handler());

        System.out.println("3msg类型:"+msg.getClass());
        System.out.println("4客户端地址:"+ctx.channel().remoteAddress());

        // 回复信息给浏览器[Http 协议]
        if (msg instanceof HttpRequest) {

            System.out.println("5ctx 类型="+ctx.getClass());
            System.out.println("6pipeline hashcode" + ctx.pipeline().hashCode() + " TestHttpServerHandler hash=" + this.hashCode());

            HttpRequest request = (HttpRequest) msg;
            // 获取Uri,过滤指定的资源
            String uri = request.uri();
            System.out.println(uri);
            if(uri.contains("favicon.ico")){
                return;
            }


            ByteBuf content = Unpooled.copiedBuffer("Hello,我是服务器", CharsetUtil.UTF_8);
            // 构造一个Http的响应,即httpResponse
            FullHttpResponse httpResponse
                    = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);

            httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=utf-8");
            httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());

            // 返回
            ctx.writeAndFlush(httpResponse);
        }

    }
}

image.png

image.png

  •  

声明:内容来源于网络,目的是为了学习和技术交流,如有侵犯,请联系删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值