栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

netty源码分析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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() {
            @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)ChannelConfigChannelIdUnsafePipeline(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() {
        @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));
                }
            });
        }
    });
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号