下载netty源码:https://github.com/netty/netty/releases/tag/netty-4.1.6.Final
看一个官方的例子
EventLoopGroup bossGroup = new NioEventLoopGroup(1);// 工作线程池,主要用于接受client的连接请求 EventLoopGroup workerGroup = new NioEventLoopGroup();//处理client上读写请求 try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class)// 定义服务的使用了NIO .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() {//创建各种连接器(filter) @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new DiscardServerHandler()); } }); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(PORT).sync();
对与上面出现的类做个解释 EventLoopGroup 工作线程,处理服务内部的定时任务和NIO任务等
ServerBootstrap 属于一个服务总体的聚合类,里面可以设置不同的服务协议和处理handler
后续详细说明其中的细节