Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/netty4 #630

Merged
merged 12 commits into from
Oct 11, 2017
Merged

Feat/netty4 #630

merged 12 commits into from
Oct 11, 2017

Conversation

qinliujie
Copy link
Contributor

Netty 4 通信模块


private static final Logger logger = LoggerFactory.getLogger(NettyClient.class);

private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new NamedThreadFactory("NettyClientWorker", true));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的ThreadFactory最好使用netty4自己的DefaultThreadFactory,它里面覆盖了thread local,有些特殊的优化

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯的,确实忘记了 fastThreadlocal 这个东西,感谢提醒

}
}

private class InternalDecoder extends ByteToMessageDecoder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个InternalDecoder稍微写复杂了,很多逻辑ByteToMessageDecoder里已经实现了,基本上向下面这样就ok了
`private class InternalDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> out) throws Exception {
        NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
        try {

            Object msg = codec.decode(channel, new NettyBackedChannelBuffer(input));
            if (msg == Codec.NEED_MORE_INPUT) {
                return;
            }
            if (msg != null)
                out.add(msg);
        } catch (IOException ex) {
            throw ex;
        } finally {
            NettyChannel.removeChannelIfDisconnected(ctx.channel());
        }
    }
}`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saveReaderIndex 还是有必要的,要不然 Codec.NEED_MORE_INPUT 的情况, 可能需要回退重新读取;虽然 dubbo 的 codec 是有做了这个动作,但是其他 codec 没有做这个操作。。。考虑了一下,可能在这里做会更好一些

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也认为这个saveReaderIndex有必要咯。目前master上基于netty3的做法是会保存一个remaingBuffer(保存不完整的帧),每次会将input的ChannelBuffer中的所有bytes写入到remaingBuffer中然后dubbo codec从remaingBuffer中读取,这样做的目的是为啥呢?这次升4直接利用ByteBuf应该也不会有问题吧

bootstrap = new ServerBootstrap();

bossGroup = new NioEventLoopGroup(0, new NamedThreadFactory("NettyServerBoss", true));
workerGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new NamedThreadFactory("NettyServerWorker", true));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个Constants.DEFAULT_IO_THREADS是根据cpu核数取值,这个在docker里有坑,使用cpu quota这种没有什么好办法设置CPU核数,最后取的都是宿主的核数,导致启动了很多netty线程。应该设置个max值
Match.min(cpu cores, 8) 类似这样的。

@LNAmp
Copy link

LNAmp commented Sep 13, 2017

Netty 3->4 可以怎么参与进来呢?

 - 使用 netty 4 fastThreadLocal 线程工厂
 - 优化 worker 的默认值
 - 去掉一些无用代码
@chickenlj chickenlj added this to the 2.5.6 milestone Sep 14, 2017
@chickenlj chickenlj merged commit 6d47a70 into apache:master Oct 11, 2017
zhangyinyue pushed a commit to zhangyinyue/dubbo that referenced this pull request Oct 23, 2017
@bert82503
Copy link

看最新版本的Transporter还是@SPI("netty"),有公司在线上环境使用netty4吗?
建议官方对其做一些稳定性和性能测试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants