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