下載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
後續詳細說明其中的細節線程