主要源码如下

/*
@author https://yunp.top
 */
package top.yunp.nettyweb;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.logging.Logger;

public class Main {

    private static final Logger LOG = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) {
        var bossGroup = new NioEventLoopGroup();
        var workerGroup = new NioEventLoopGroup();
        try {
            new ServerBootstrap()
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel channel) throws Exception {
                            channel.pipeline()
                                    .addLast(new IdleStateHandler(5, 5, 5))
                                    .addLast(new HttpServerCodec())
                                    .addLast(new MyHandler());
                        }
                    }).bind(8001).addListener((ChannelFutureListener) channelFuture -> {
                        if (channelFuture.isSuccess()) {
                            LOG.info("Server started on port 8001");
                        } else {
                            LOG.warning("Failed to start server");
                            channelFuture.cause().printStackTrace();
                        }
                    }).channel().closeFuture().sync();
        } catch (InterruptedException e) {
            LOG.info("Interrupted");
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
            LOG.info("Server stopped");
        }
    }
}