Netty学习笔记十七、Netty模型代码现实(编写)

一描述

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

二、代码实现

1、server端代码


package com.hao.netty.studynetty;

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


/**
 * @author haoxiansheng
 * @date 2020-05-23
 */
public class NettyServer {
    public static void main(String[] args) throws InterruptedException {

        // 创建BossGroup 和workerGroup
        // 说明
        // 1、创建两个线程组boosGroup 和workerGroup
        // 2、bossGroup 只是处理连接请求,真正的和客户端业务处理,会交给workerGroup 完成
        // 3、两个都是无限循环
        // 4、boosGroup 和workerGroup 含有的子线程(NioEventLoopGroup)的个数
        //    默认实际 cpu核数*2
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            // 创建服务器端的启动对象 配置参数
            ServerBootstrap bootstrap = new ServerBootstrap();

            // 使用链式编程来进行设置
            bootstrap.group(bossGroup, workerGroup) // 设置两个线程
                    .channel(NioServerSocketChannel.class) // 使用NioServerSocketChannel 作为服务器的通道实现
                    .option(ChannelOption.SO_BACKLOG, 128) // 设置线程队链得到连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // 设置保持活动连接状态
                    .childHandler(new ChannelInitializer<SocketChannel>() { // 创建一个通道测试对象(匿名对象)
                        //  给pipeline 设置处理器
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyServerHandler()); // 将handler 加入到pipeline
                        }
                    }); // 给workerGroup 的 EventLoop 对应的管道设置处理器

            System.out.println("== 服务器 配置好了 ");

            // 绑定一个端口并且同步, 生成一个ChannelFuture 对象
            // 启动服务器(并绑定端口)
            ChannelFuture cf = bootstrap.bind(6668).sync();

            // 对关闭通道进行监听
            cf.channel().closeFuture().sync();
        } finally {
            // 优雅关闭
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

package com.hao.netty.studynetty;

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

/**
 * @author haoxiansheng
 * @date 2020-05-23
 * @Description 1、自定义一个Handler 需要继承netty 规定好的某一个HandlerAdapter(规范)
 * 2、这时自定义个Handler 才能称为一个Handler
 */

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

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

        System.out.println("服务器读取线程是 =>" + Thread.currentThread().getName());
        System.out.println("server ctx=" + ctx);
        System.out.println("看一下啊pipeline 和 channel 的关系");
        Channel channel = ctx.channel();
        ChannelPipeline pipeline = ctx.pipeline(); // 本质是一个双向链表 出栈入栈
        System.out.println();
        // 将msg 转换成一个ByteBuf
        // ByteBuf 是netty 提供的不是 NIO ByteBuffer
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("客户端发送的消息是 =>" + byteBuf.toString(CharsetUtil.UTF_8));
        System.out.println("客户端地址 =>" + channel.remoteAddress());
    }

    /**
     * 数据读取完毕
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // writeAndFlush  = writer + flush
        // 将数据写入缓冲 并刷新(将缓冲的数据刷新到pipeline)
        // 对发送的数据进行编码
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端", CharsetUtil.UTF_8));
    }

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


2、Client端代码


package com.hao.netty.studynetty;

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

/**
 * @author haoxiansheng
 * @date 2020-05-23
 */
public class NettyClinet {
    public static void main(String[] args) throws Exception {

        // 客户端需要一个事件循环组
        EventLoopGroup eventExecutors = new NioEventLoopGroup();

        try {

            // 创建客户端启动对象
            // 注意客户端不是 ServerBootStrap 而是 BootStrap
            // 注意不要引错包  import io.netty.bootstrap.Bootstrap;
            Bootstrap bootstrap = new Bootstrap();

            // 设置相关参数
            bootstrap.group(eventExecutors) // 设置线程组
                    .channel(NioSocketChannel.class) // 设置客户端通道的实现类(反射)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyClientHandler()); // 加入对应的处理器(自己的)
                        }
                    });

            System.out.println("客户端配置完成 ==> ");

            // 启动客户端去连接服务器端
            // channelFuture 涉及到netty的异步模型
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
            // 给关闭通道进行监听
            channelFuture.channel().closeFuture().sync();

        } finally {
            // 优雅关闭
            eventExecutors.shutdownGracefully();
        }
    }
}


package com.hao.netty.studynetty;

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;

/**
 * @author haoxiansheng
 * @date 2020-05-23
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 当通道就绪就会触发该方法
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client ctx =>" + ctx);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello Sever", CharsetUtil.UTF_8)); // 非池化Unpooled 做事情
    }

    /**
     * 当通道有读取事件时,会触发
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("服务器回复的消息为 =>" + byteBuf.toString(CharsetUtil.UTF_8));
        System.out.println("服务器的地址 =>" + ctx.channel().remoteAddress());
    }

    /**
     * 发生异常关闭 ctx
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.fillInStackTrace();
        //ctx.channel().close();
        ctx.close();
    }
}


3、获取当前系统Cpu核数


package com.hao.netty.studynetty;

import io.netty.util.NettyRuntime;


/**
 * @author haoxiansheng
 * @date 2020-05-23
 */
public class TestCpu {
    public static void main(String[] args) {
        // 测试机器有几核
        System.out.println(NettyRuntime.availableProcessors());
    }
}

在这里插入图片描述
在这里插入图片描述
4、执行先启动服务端 再启动客户端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值