Netty05-实战
2025年2月21日小于 1 分钟
源代码
https://github.com/coder-xuyong/netty
基础 Server
private final ServerBootstrap serverBootstrap = new ServerBootstrap();
private final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
private final EventLoopGroup workerGroup = new NioEventLoopGroup(2);
public NettyServer() {
serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
ch.pipeline().addLast(new StringDecoder());
}
});
}
public void start(int port) {
ChannelFuture channelFuture = serverBootstrap.bind(port);
channelFuture.addListener(future -> {
if (future.isSuccess()) {
log.info("端口[{}]绑定成功", port);
} else {
log.error("端口[{}]绑定异常!", port);
}
});
try {
// 阻塞在此处,直到绑定端口完成
channelFuture.sync();
} catch (InterruptedException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}