Netty(2)Netty模型与案例

前言

了解了主从Reactor多线程模型,接下来了解Netty模型

目录

  1. Netty模型
  2. Netty案例-TCP服务

Netty模型

Netty模型修改了主从Reactor多线程模型
在这里插入图片描述Netty模型工作原理:

  1. Netty抽象了两组线程池:BossGroup和WorkerGroup;BossGroup专门负责接收客户端连接;WorkGroup负责网络的读写(BossGroup和WorkerGroup都是NioEventLoopGroup类型)
  2. NioEventLoopGroup相当于是事件循环组,这个组包含了多个事件循环NioEventLoop
  3. NioEventLoop表示一个循环处理业务的线程,上面有两个对象,Selector对象监听绑定在Selector上的Socket的网络通信事件,TaskQueue是任务队列
  4. 每一个NioEventLoop线程启动会调用run方法,Boss Group中的NioEventLoop循环执行以下步骤
    (1)select方法轮询accept事件
    (2)processSelectedKeys(IO任务):处理accept事件,连接Client,生成NioSocketChannel(SocketChannel的Netty封装)注册到WorkerGroup的Selector
    (3)处理任务队列的事件,添加到taskQueue中的任务(非IO任务),如bind、channelActive等
  5. WorkGroup创建多个NioEventLoop线程处理业务,循环执行
    (1)select方法轮询read、write事件
    (2)处理IO事件:read、write事件,在对应的NioSocketChannel处理
    (3)处理任务队列事件
  6. 每个NioEventLoop都会有相应的管道(Pipeline),Pipeline中包含着Channel(相互包含,Channel也可以找到Pipeline),Pipeline维护了多个处理器(ChannelHandler)

中间有太多组件需要仔细了解,等待后续慢慢理解

Netty案例-TCP服务

一个简单的聊天系统

  1. 客户端发送“hello”消息
  2. 服务器接收打印该消息,并回复“hello”
  3. 客户端接收服务器的消息

服务器:

package Netty.Simple;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyServer {
    public static void main(String[] args) throws InterruptedException {
        //创建BossGroup和WorkerGroup
        //创建两个线程组
        //bossGroup只处理连接请求,交给workerGroup处理客户端业务
        //两个都是无限循环
        //bossGroup和workerGroup含有的子线程数(NioEventLoop)默认是cpu核数*2
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {


        //创建服务器端的启动对象,配置参数
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        //使用链式编程进行设置

        serverBootstrap.group(bossGroup,workerGroup)//设置两个线程组
                .channel(NioServerSocketChannel.class)//使用NioSocketChannel作为服务器的通道实现
                .option(ChannelOption.SO_BACKLOG,128)//设置线程队列得到连接个数
                .childOption(ChannelOption.SO_KEEPALIVE,true)//设置保持活动连接状态
                .childHandler(new ChannelInitializer<SocketChannel>() {//创建一个通道初始化对象
                    //给pipeline设置处理器
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NettyServerHandler());
                    }
                });//给我们的workerGroup的NioEventLoop对应的管道设置处理器

        System.out.println("服务器准备就绪");
        //绑定一个端口并且同步,生成一个channelFuture对象
        ChannelFuture future = serverBootstrap.bind(9999).sync();
        //对关闭通道的事件进行监听
        future.channel().closeFuture().sync();
        }finally {
            //关闭
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

服务器pipeline处理器:

package Netty.Simple;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;

/*
* 1.自定义的handler需要继承netty规定的handlerAdapter
*重写一些方法,自定义的handler才能称为一个handler
* */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    //读取客户端发送的数据
    //ChannelHandlerContext是上下文对象,含有管道pipeline,通道channel
    //Object msg客户端发送的数据,默认Object
    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("server ctx = "+ctx);
        //将msg转出ByteBuf,ByteBuf是nettty提供的,不是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 {
        
        //write加Flush方法,将数据写入缓存并刷新
        //对发送的数据进行编码
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端",CharsetUtil.UTF_8));



    }

    //处理异常,关闭通道
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

客户端:

package Netty.Simple;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {
    public static void main(String[] args) throws InterruptedException {
        //客户端需要一个事件循环组
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            //创建客户端启动对象Bootstrap
            Bootstrap bootstrap = new Bootstrap();
            //设置相关参数
            bootstrap.group(eventLoopGroup)//设置线程组
                    .channel(NioSocketChannel.class)//设置客户端通道的实现类
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyClientHandler());//加入自定义处理器
                        }
                    });
            System.out.println("客户端就绪。。。");
            //启动客户端去连接服务器端
            //ChannelFuture涉及到异步模型
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9999).sync();
            //给关闭通道进行监听
            channelFuture.channel().closeFuture().sync();
        }
        finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}

服务器处理器:

package Netty.Simple;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    //通道就绪就会触发该方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client :"+ctx);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,server。。。", CharsetUtil.UTF_8));

    }

    //当通道有读取事件时会触发
    @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();
    }
}

发布了95 篇原创文章 · 获赞 25 · 访问量 4184

猜你喜欢

转载自blog.csdn.net/key_768/article/details/104768060