NettyServer端的初始化流程

先写一个通用的Netty服务端的启动代码, 而后逐步分析

public class NettyServer {

    private final EventLoopGroup parent;

    private final EventLoopGroup children;

    private final ServerBootstrap bootstrap;

    public NettyServer() {
        parent = new NioEventLoopGroup(1);
        children = new NioEventLoopGroup();

        bootstrap = new ServerBootstrap()
                .channel(NioServerSocketChannel.class)
                .group(parent, children)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<ServerChannel>() {
                    @Override
                    protected void initChannel(ServerChannel ch) throws Exception {
                        ch.pipeline();
                        // 添加自定义handler...
                    }
                });


    }

    public ChannelFuture bind(int port) throws InterruptedException {
        try {
            return bootstrap.bind(port).sync();
        } catch (InterruptedException e) {
            parent.shutdownGracefully();
            children.shutdownGracefully();
            throw e;
        }
    }

    public static void main(String[] args) throws InterruptedException {

        new NettyServer().bind(9989);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# EventLoopGroup初始化流程

# Channel配置

# EventLoop分配

# 初始化Channel

# 绑定端口

上次更新: 2022/3/11 15:12:48