Dubbo-聊聊通信模块设计( 七 )

AbstractChannelAbstractChannel的设计也是类似模板类的设计,对于不同的NIO框架来说有不同的Channel的实现,因此对于Dubbo来说也必须去抽象该实现,具体的不同交由子类进行实现,子类做映射 。该类内部只有有一个Send方法,为了判断当前的连接是否还在,没有实现具体的发送消息 。
Netty4NettyTransporterNettyTransporter实现Transporter,当SPI机制触发的时候会自动加载实现NettyServer、NettyClient初始化创建 。
NettyServer接下来我们来看下Netty4中关于doOpen方法的实现,此处就是Netty Server启动的核心,也是Dubbo网络通信的服务端能力的提供者,就是Dubbo和Netty结合的核心 。
    protected void doOpen() throws Throwable {        //创建ServerBootstrap        bootstrap = new ServerBootstrap();        //创建boss EventLoopGroup        bossGroup = NettyEventLoopFactory.eventLoopGroup(1, "NettyServerBoss");        //创建worker EventLoopGroup        workerGroup = NettyEventLoopFactory.eventLoopGroup(                getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),                "NettyServerWorker");        //创建一个Netty的ChannelHandler        final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);        //此处的Channel是Dubbo的Channel        channels = nettyServerHandler.getChannels();        //会话保持        boolean keepalive = getUrl().getParameter(KEEP_ALIVE_KEY, Boolean.FALSE);        bootstrap.group(bossGroup, workerGroup)                .channel(NettyEventLoopFactory.serverSocketChannelClass())                .option(ChannelOption.SO_REUSEADDR, Boolean.TRUE)                .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)                .childOption(ChannelOption.SO_KEEPALIVE, keepalive)                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)                .childHandler(new ChannelInitializer<SocketChannel>() {                    @Override                    protected void initChannel(SocketChannel ch) throws Exception {                        // FIXME: should we use getTimeout()?                        //连接空闲超时时间                        int idleTimeout = UrlUtils.getIdleTimeout(getUrl());                        //创建Netty实现的decoder和encoder                        NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);            &nb

经验总结扩展阅读