netty源码分析

//Aceptor线程池
private static EventLoopGroup bossGroup = new NioEventLoopGroup(1);
//Sub线程池,处理IO读写任务
private static EventLoopGroup workerGroup = new NioEventLoopGroup();
//netty 服务端入口
ServerBootstrap b = new ServerBootstrap();
//绑定线程池,主从多线程模式
b.group(bossGroup, workerGroup)
         //表明是服务端
        .channel(NioServerSocketChannel.class)
         //SocketChannel的处理器
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(
                        //设置心跳
                        new IdleStateHandler(0,0,5, TimeUnit.SECONDS),
                        //处理连接,IO读写的具体逻辑
                        new Dispatcher(proxyConstant, proxyAccountService));
            }
        })
        //关闭自动读取
        .childOption(ChannelOption.AUTO_READ, false)
        //绑定本地端口
        .bind(proxyConstant.getLocalPort()).sync()
        .addListener((ChannelFutureListener) future -> log.info("Proxying on:" + proxyConstant.getLocalPort() + " ..."));

//初始化很多东西
private ChannelFuture doBind(final SocketAddress localAddress) {
    //初始化并注册
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
}
//
final ChannelFuture initAndRegister() {
//创建ServerSocketChannel,channelFactory由.channel(NioServerSocketChannel.class)包装,newChannel()实际上NioServerSocketChannel的无参构造器
生成NioServerSocketChannel,以及以下对象
  • Channel(Nio的ServerSocketChannle)
  • ChannelConfig
  • ChannelId
  • Unsafe
  • Pipeline(tail为Outbound和InBound)
  • ChannelHander
channel = channelFactory.newChannel();
init(channel);
//把channel注册到eventLoop上的selector上,底层如下
ChannelFuture regFuture = config().group().register(channel);
//绑定端口
doBind0(regFuture, channel, localAddress, promise);
}

//jdk channel注册Selector上
void doRegister(){
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this); 
}

//jdk绑定端口
void doBind(SocketAddress localAddress) throws Exception {
        if (PlatformDependent.javaVersion() >= 7) {
            javaChannel().bind(localAddress, config.getBacklog());
        } else {
            javaChannel().socket().bind(localAddress, config.getBacklog());
        }
}
//初始化
void init(Channel channel) {
    setChannelOptions(channel, options0().entrySet().toArray(newOptionArray(0)), logger);
    setAttributes(channel, attrs0().entrySet().toArray(newAttrArray(0)));

    ChannelPipeline p = channel.pipeline();

...
    //添加一个的ChannelHandler
    p.addLast(new ChannelInitializer<Channel>() {
        @Override
        public void initChannel(final Channel ch) {
            final ChannelPipeline pipeline = ch.pipeline();
            ChannelHandler handler = config.handler();
            if (handler != null) {
                pipeline.addLast(handler);
            }
            //从主Reactor线程池启动一个Acceptor处理accept
            ch.eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    pipeline.addLast(new ServerBootstrapAcceptor(
                            ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                }
            });
        }
    });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值