本文共 4998 字,大约阅读时间需要 16 分钟。
本文是对Netty主要的一些知识点做一个总结,因为Netty的底层还是NIO,所以建议有学过NIO的伙伴们再看本文,如果没有NIO基础的小伙伴可以去看这一篇文件了解一下,这篇文章对NIO也做了比较详细的讲解。
本文主要知识来源:
Netty底层采用多路复用技术,是异步的。底层也是采用的NIO。
io.netty netty-all 4.1.39.Final
package com.hs.nettyPrimary;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.channel.ChannelInitializer;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.string.StringDecoder;public class HelloServer { public static void main(String[] args) { new ServerBootstrap() .group(new NioEventLoopGroup()) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer () { @Override protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { nioSocketChannel.pipeline().addLast(new StringDecoder()); nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("客户端接收的参数" + msg); } }); } }) .bind(8080); }} package com.hs.nettyPrimary;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.channel.ChannelInitializer;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;public class HelloClient { public static void main(String[] args) throws InterruptedException { new Bootstrap() .group(new NioEventLoopGroup()) .channel(NioSocketChannel.class) .handler(new ChannelInitializer () { @Override protected void initChannel(NioSocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new StringEncoder()); } }) .connect("localhost", 8080) .sync() .channel() .writeAndFlush("hello,netty!"); }} ServerBootstrap()。EventLoopGroup组,一个Selector+Thread = EventLoop。ServerSocketChannel实现。childHandler(),决定child能干什么事情。Bootstrap()。EventLoopGroup组。SocketChannel的实现。EventLoop。Handler处理实际事件。EventLoop本质上是一个Selector+Thread的执行器。EventLoop继承两个类:netty自己的OrderedEventExecutor接口。java.util.concurrent.ScheduledExecutorService接口。EventLoop,而是使用EventLoopGroup,因为通常需要多个EventLoop来处理多个channel的io操作。EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
eventLoopGroup.next().execute(() -> { // 执行普通任务});eventLoopGroup.next().scheduleAtFixedRate(() -> { // 执行定时任务}, 1, 2, TimeUnit.SECONDS);@Slf4jpublic class EventLoopGroupServerTest2 { public static void main(String[] args) { new ServerBootstrap() .group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class) .childHandler(<> { @Override protected void initChannel(NioSocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; log.debug("接收客户端的消息为:" + byteBuf.toString(Charsets.UTF_8)); } }); } }) .bind(8080); }}EventLoopGroup:EventLoopGroup eventLoopGroup = new DefaultEventLoopGroup();
EventLoopGroup,让每个EventLoopGroup专门处理特定的任务:new ServerBootstrap() .group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class) .childHandler(...) .bind(8080);
Netty是一个高性能的异步通信框架,通过多路复用技术实现了非阻塞的数据传输。其核心组件包括EventLoopGroup、channel、pipeline和handler等。通过合理配置和优化EventLoopGroup,可以更高效地处理网络通信任务。
转载地址:http://odcfk.baihongyu.com/