dubbo rpc NettyClient连接

14 篇文章 0 订阅
11 篇文章 0 订阅

consumer获取channel

NettyChannel.CHANNEL_MAP(静态变量)中存储着channel。

/**
     * the cache for netty channel and dubbo channel
     */
    private static final ConcurrentMap<Channel, NettyChannel> CHANNEL_MAP = new ConcurrentHashMap<Channel, NettyChannel>();
    

通过NettyChannel类中的getOrAddChannel方法获取

    /**
     * Get dubbo channel by netty channel through channel cache.
     * Put netty channel into it if dubbo channel don't exist in the cache.
     *
     * @param ch      netty channel
     * @param url
     * @param handler dubbo handler that contain netty's handler
     * @return
     */
    static NettyChannel getOrAddChannel(Channel ch, URL url, ChannelHandler handler) {
        if (ch == null) {
            return null;
        }
        NettyChannel ret = CHANNEL_MAP.get(ch);
        if (ret == null) {
            NettyChannel nettyChannel = new NettyChannel(ch, url, handler);
            if (ch.isActive()) {//先尝试存入map
                nettyChannel.markActive(true);
                ret = CHANNEL_MAP.putIfAbsent(ch, nettyChannel);
            }
            if (ret == null) {//无论是否成功存入map,也要将其返回
                ret = nettyChannel;
            }
        }
        return ret;
    }

NettyChannel.CHANNEL_MAP中获取失败,就重新构建一个NettyChannel对象。
ch.isActive为true,将其存到NettyChannel.CHANNEL_MAP
ch.isActive等价于NioSocketChannel.ch.state==2 && NioSocketChannel.ch.open==true
在这里插入图片描述

consumer关闭客户端时

provider中会有警告(AbstractServer.disconnected中的代码)

[06/12/20 12:13:07:007 CST] NettyServerWorker-5-7  WARN transport.AbstractServer:  [DUBBO] All clients has disconnected from /192.168.161.1:20880. You can graceful shutdown now., dubbo version: 2.7.7, current host: 192.168.0.107

猜测其触发方式是由于套接字发出了什么事件。然后在轮询SingleThreadEventExecutor.runAllTasks时在taskQueue中检测到新的任务,开始执行任务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值