package com.casic.missiles.socket; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.AdaptiveRecvByteBufAllocator; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import lombok.extern.slf4j.Slf4j; /** * @author a203 */ @Slf4j public class BootNettyServer { public void bind(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { /** * ServerBootstrap 是一个启动NIO服务的辅助启动类 */ ServerBootstrap serverBootstrap = new ServerBootstrap(); /** * 设置group,将bossGroup,workerGroup线程组传递到ServerBootstrap */ serverBootstrap = serverBootstrap.group(bossGroup, workerGroup); /** * ServerSocketChannel是以NIO的selector为基础进行实现的,用来接收新的连接,这里告诉Channel通过NioServerSocketChannel获取新的连接 */ serverBootstrap = serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap = serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 10496, 1048576)); serverBootstrap = serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 10496, 1048576)); /** * 设置 I/O处理类,主要用于网络I/O事件,记录日志,编码、解码消息 */ serverBootstrap = serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>()); log.info("端口已开启,占用" + port + "端口号...."); /** * 绑定端口,同步等待成功 */ ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); /** * 等待服务器监听端口关闭 */ channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { /** * 退出,释放线程池资源 */ bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }