diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 41b55b2d59e..526742d35f4 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -217,9 +217,7 @@ private void refreshOverrideAndInvoker(List urls) { private void refreshInvoker(List invokerUrls) { Assert.notNull(invokerUrls, "invokerUrls should not be null"); - if (invokerUrls.size() == 1 && invokerUrls.get(0) != null && Constants.EMPTY_PROTOCOL.equals(invokerUrls - .get(0) - .getProtocol())) { + if (invokerUrls.size() == 1 && Constants.EMPTY_PROTOCOL.equals(invokerUrls.get(0).getProtocol())) { this.forbidden = true; // Forbid to access this.invokers = Collections.emptyList(); routerChain.setInvokers(this.invokers); @@ -241,8 +239,14 @@ private void refreshInvoker(List invokerUrls) { } Map> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map - // state change - // If the calculation is wrong, it is not processed. + /** + * If the calculation is wrong, it is not processed. + * + * 1. The protocol configured by the client is inconsistent with the protocol of the server. + * eg: consumer protocol = dubbo, provider only has other protocol services(rest). + * 2. The registration center is not robust and pushes illegal specification data. + * + */ if (CollectionUtils.isEmptyMap(newUrlInvokerMap)) { logger.error(new IllegalStateException("urls to invokers error .invokerUrls.size :" + invokerUrls.size() + ", invoker.size :0. urls :" + invokerUrls .toString())); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index 9b1c9ba8daa..96a2e9976e4 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -153,6 +153,10 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { + if (cancelFutureIfOffline()) { + return; + } + if (!isConnected()) { connect(); } else { @@ -173,7 +177,29 @@ public void run() { } } } + + private boolean cancelFutureIfOffline() { + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return true; + } + return false; + } }; + reconnectExecutorFuture = reconnectExecutorService.scheduleWithFixedDelay(connectStatusCheckCommand, reconnect, reconnect, TimeUnit.MILLISECONDS); } } @@ -345,14 +371,14 @@ public void reconnect() throws RemotingException { @Override public void close() { try { - if (executor != null) { - ExecutorUtil.shutdownNow(executor, 100); - } + super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { - super.close(); + if (executor != null) { + ExecutorUtil.shutdownNow(executor, 100); + } } catch (Throwable e) { logger.warn(e.getMessage(), e); }