netty入门例子

netty封装了java的nio。开发只需要按照netty提供的api方法,相对繁琐nio使开发变的相对简单很多。

服务端启动

复制代码
//创建两个线程池
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
//boot启动类
ServerBootstrap boot = new ServerBootstrap();
//绑定线程池,channel类型为NioServerSocketChannel
boot.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
               //添加pipeline
                channel.pipeline()
                        .addLast(new StringDecoder())
                        .addLast(new StringEncoder())
                        .addLast(new ChatServerHandler());
            }
        });
System.out.println("server start");
//绑定端口
ChannelFuture future = boot.bind(9000).sync();

ChatServerHandler就是简单继承SimpleChannelInboundHandler,重写channelRead0方法。

复制代码
public class ChatServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.println(s);
    }
}

客户端启动

复制代码
//创建线程组
EventLoopGroup group = new NioEventLoopGroup();
//启动类
Bootstrap boot = new Bootstrap();
//绑定线程组,设置channel类型为NioSocketChannel
boot.group(group).channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                //初始化pipeline
                channel.pipeline()
                        .addLast(new StringEncoder())
                        .addLast(new StringDecoder());
            }
        });
        //连接server
ChannelFuture future = boot.connect("localhost", 9000).sync();
Channel channel = future.channel();
//发送数据
channel.writeAndFlush("hi");
channel.closeFuture().sync();
group.shutdownGracefully();

服务端能收到客户端发送数据。

线程模型

使用reactor线程模型处理channel事件。

线程组:

线程组的创建通过NioEventLoopGroup类,可以入参指定线程组内child数量,不指定会根据当前机器的CPU进行计算child数量。每个child是一个NioEventLoop实例。NioEventLoop是一个单线程线程池类。

线程组内监听到Selector事件都会交由channel绑定的pipeline进行处理。

BossGroup:负责创建serverchannel,并且处理client连接事件,也就是accept事件。然后将新建的client连接与workGroup线程池进行绑定

workGroup:负责监听已绑定channel连接的读写事件。

参考:

https://www.alibabacloud.com/blog/essential-technologies-for-java-developers-io-and-netty_597367

相关推荐
不早睡不改名@5 天前
Netty源码分析---Reactor线程模型深度解析(一)
java·笔记·学习·netty
zs宝来了5 天前
Netty Reactor 模型:Boss、Worker 与 EventLoop
reactor·netty·源码解析·线程模型·eventloop
不早睡不改名@6 天前
Netty源码分析---Reactor线程模型深度解析(二)
java·网络·笔记·学习·netty
不早睡不改名@6 天前
Netty源码解析---FastThreadLocal-addToVariablesToRemove方法详解
java·网络·笔记·学习·netty
MrSYJ15 天前
有没有人懂socketChannel中的write,read方法啊,给我讲讲
java·程序员·netty
尽兴-20 天前
RocketMQ核心源码深度解读:架构原理与核心机制剖析
架构·rocketmq·netty·架构原理·消息持久化
Javatutouhouduan20 天前
Netty进阶指南:基础+中级+高级+架构行业运用+源码分析
java·netty·java面试·网络io·后端开发·java程序员·互联网大厂
MrSYJ23 天前
Netty异常传播机制
java·服务器·netty
qq_2320455725 天前
精积微半导体面试(部分)
netty·策略模式·nio·内存抖动·threadlocal·bitmap·复用
一叶飘零_sweeeet1 个月前
从 BIO 到 AIO 全链路拆解:Reactor 模型演进与高并发 IO 架构落地实战
netty·nio