Netty Epoll 和 Nio 发表于 2018-07-13 | 更新于 2018-08-20 | 分类于 Java | 阅读次数: | 字数: 3.5k | 时长 ≈ 3 分钟 标签 Netty 入门1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import org.slf4j.Logger;import org.slf4j.LoggerFactory;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.ssl.SslContext;import io.netty.handler.ssl.SslContextBuilder;import io.netty.handler.ssl.util.SelfSignedCertificate;import io.netty.util.concurrent.DefaultEventExecutorGroup;import io.netty.util.concurrent.EventExecutorGroup;/** * Netty 启动器,初始化 spring boot 的时候会自动加载 */public class Server { private static final Logger LOG = LoggerFactory.getLogger(Server.class); /* * NioEventLoopGroup 默认创建数量为 CPU * 2,类型为 NioEventLoop 的实例。 * 每个 NioEventLoop 实例都持有一个线程,以及一个类型为 LinkedBlockingQueue 的任务队列 */ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); // 业务线程 EventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(16); static final boolean isSsl = System.getProperty("ssl") != null; /** * 初始化 Netty */ public void startNetty() { try { final SslContext sslCtx; if (isSsl) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // 引导辅助程序 ServerBootstrap bs = new ServerBootstrap(); // 通过NIO方式来接收连接和处理连接 bs.group(bossGroup, workerGroup); // 设置NIO的Channel bs.channel(NioServerSocketChannel.class); bs.childHandler(new HttpChannelInitializer(sslCtx, eventExecutorGroup)); bs.option(ChannelOption.SO_BACKLOG, 1024); // 连接数 // bs.childOption(ChannelOption.SO_KEEPALIVE, true); // 是否长链接 bs.childOption(ChannelOption.TCP_NODELAY, true); // 是否TCP延迟 // 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功 Channel ch = bs.bind(9999).sync().channel(); LOG.info("Server startup. listen on {}-{}", (isSsl ? "https" : "http"), 9999); // 应用程序会一直等待,直到Channel关闭 ch.closeFuture().sync(); } catch (Exception e) { LOG.error("server startup failed. {}", e.getMessage(), e); } finally { LOG.info("释放资源..."); bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); eventExecutorGroup.shutdownGracefully(); bossGroup = null; workerGroup = null; eventExecutorGroup = null; } } public static void main(String[] args) { new Server().startNetty(); }} 优化:Epoll 和 Nio123456private static final boolean isEpoll = Epoll.isAvailable();private EventLoopGroup bossGroup = isEpoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();private EventLoopGroup workerGroup = isEpoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(bossGroup, workerGroup) .channel(isEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class); 本文作者: forever杨 本文链接: https://blog.yl-online.top/posts/a8f7cc4d.html 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!