dubbo check=true源碼分析

1. 問題

check=true到底檢查的是什麼?

 

2. debug查看

 

ReferenceConfig裏面的createProxy方法

if (shouldCheck() && !invoker.isAvailable()) {
    throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
}

判斷是否需要check

protected boolean shouldCheck() {
    Boolean shouldCheck = isCheck();
    if (shouldCheck == null && getConsumer() != null) {
        shouldCheck = getConsumer().isCheck();
    }
    if (shouldCheck == null) {
        // default true
        shouldCheck = true;
    }
    return shouldCheck;
}
// AbstractReferenceConfig配置中,check來自配置
public Boolean isCheck() {
    return check;
}

關鍵代碼:

invoker.isAvailable()

RegistryDirectory類

@Override
public boolean isAvailable() {
    if (isDestroyed()) {
        return false;
    }
    Map<String, Invoker<T>> localUrlInvokerMap = urlInvokerMap;
    if (localUrlInvokerMap != null && localUrlInvokerMap.size() > 0) {
        for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) {
            if (invoker.isAvailable()) {
                return true;
            }
        }
    }
    return false;
}

 DubboInvoker

@Override
public boolean isAvailable() {
    if (!super.isAvailable()) {
        return false;
    }
    for (ExchangeClient client : clients) {
        if (client.isConnected() && !client.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY)) {
            //cannot write == not Available ?
            return true;
        }
    }
    return false;
}

 // 連接是否已經建立

@Override
public boolean isConnected() {
    return channel.isConnected();
}

 

發佈了206 篇原創文章 · 獲贊 104 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章