Netty初识

netty一个网络编程的框架,在网络编程的地位,相当于spring在J2EE中的地位。

netty对一些底层的内容进行了优化

  • FastThreadLocal => ThreadLocal
  • ByteBuf => ByteBuffer 

总之就是一个很厉害的框架。

简单例子

服务器端:

package com.test.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class HelloServer {
    public static void main(String[] args) {
        //1、启动类,负责组装netty组件,启动服务器
        new ServerBootstrap()
                //2、添加EventLoop(BossEventLoop、WorkerEventLoop),group
                .group(new NioEventLoopGroup())
                //3、选择服务器的channel实现
                //OIO=BIO
                .channel(NioServerSocketChannel.class)
                //4、添加处理器
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    //5、建立连接后被调用,初始化其他的handler
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        //6、添加具体的handler
                        //转码的 handler
                        nioSocketChannel.pipeline().addLast(new StringDecoder());
                        //接收数据
                        nioSocketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                System.out.println(String.format("服务器端结果: %s", s));
                            }
                        });
                    }
                })
                //7、绑定监听端口
                .bind(8080);
    }
}

客户端:

package com.test.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        //1、启动类
        new Bootstrap()
                //2、添加EventLoop
                .group(new NioEventLoopGroup())
                //3、选择客户端channel实现
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        //建立连接后调用
                        channel.pipeline().addLast(new StringEncoder());
                    }
                })
                //5、连接到服务器
                .connect(new InetSocketAddress("127.0.0.1", 8080))
                .sync()
                .channel()
                //6、向服务器发送信息
                .writeAndFlush("你就是我的宝贝!");
    }
}

执行流程

借用老师上课讲的图片

相关组件理解 

  • channel就是数据流通的通道
  • msg 流动的数据,也就是服务器和客户端之间传递的信息,输入的是ByteBuf,然后它会经过pipeline中的各种handler加工,最终形成想要的东西,最后输出为ByteBuf
  • handler就是处理传输数据的工序
    • 工序有很多到,通过pipeline进行连接,pipeline负责发布事件(读、写、读取完成...)传播给每个handler,handler对自己感兴趣的事件进行处理(就是重写相关的方法),就是pipeline中有多个handler,数据会以此经过handler进行处理
    • handler分为Inbound入站、Outbound出站
  • EventLoop可以理解为处理数据的工人
    • 一个EventLoop可以管理多个channel,一个channel一旦交给了某个EventLoop处理,它们之间就会进行绑定,EventLoop会一直处理完这个channel
    • EventLoop可以进行任务处理,每个EventLoop都有自己的任务队列,队列堆放多个channel的待处理任务
    • EventLoop按照pipeline中的顺序,执行handler中的规划处理数据,可以为handler制定不同的EventLoop

Guess you like

Origin blog.csdn.net/liming0025/article/details/119942121