下载netty源码:https://github.com/netty/netty/releases/tag/netty-4.1.6.Finalgit
看一个官方的例子github
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任务等ide
ServerBootstrap 属于一个服务整体的聚合类,里面能够设置不一样的服务协议和处理handleroop
后续详细说明其中的细节线程