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 449bb286dce..f843871b6db 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 @@ -52,6 +52,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -64,7 +65,6 @@ import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTE_PROTOCOL; -import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; /** @@ -124,7 +124,7 @@ public RegistryDirectory(Class serviceType, URL url) { this.queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY)); this.overrideDirectoryUrl = this.directoryUrl = turnRegistryUrlToConsumerUrl(url); String group = directoryUrl.getParameter(Constants.GROUP_KEY, ""); - this.multiGroup = group != null && ("*".equals(group) || group.contains(",")); + this.multiGroup = group != null && (Constants.ANY_VALUE.equals(group) || group.contains(",")); } private URL turnRegistryUrlToConsumerUrl(URL url) { @@ -189,21 +189,30 @@ public void destroy() { @Override public synchronized void notify(List urls) { - List categoryUrls = urls.stream() + Map> categoryUrls = urls.stream() + .filter(Objects::nonNull) .filter(this::isValidCategory) .filter(this::isNotCompatibleFor26x) - .collect(Collectors.toList()); + .collect(Collectors.groupingBy(url -> { + if (UrlUtils.isConfigurator(url)) { + return CONFIGURATORS_CATEGORY; + } else if (UrlUtils.isRoute(url)) { + return ROUTERS_CATEGORY; + } else if (UrlUtils.isProvider(url)) { + return PROVIDERS_CATEGORY; + } + return ""; + })); - /** - * TODO Try to refactor the processing of these three type of urls using Collectors.groupBy()? - */ - this.configurators = Configurator.toConfigurators(classifyUrls(categoryUrls, UrlUtils::isConfigurator)) - .orElse(configurators); + List configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList()); + this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators); - toRouters(classifyUrls(categoryUrls, UrlUtils::isRoute)).ifPresent(this::addRouters); + List routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList()); + toRouters(routerURLs).ifPresent(this::addRouters); // providers - refreshOverrideAndInvoker(classifyUrls(categoryUrls, UrlUtils::isProvider)); + List providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList()); + refreshOverrideAndInvoker(providerURLs); } private void refreshOverrideAndInvoker(List urls) { @@ -283,7 +292,7 @@ private void refreshInvoker(List invokerUrls) { private List> toMergeInvokerList(List> invokers) { List> mergedInvokers = new ArrayList<>(); - Map>> groupMap = new HashMap>>(); + Map>> groupMap = new HashMap<>(); for (Invoker invoker : invokers) { String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, ""); groupMap.computeIfAbsent(group, k -> new ArrayList<>()); @@ -343,11 +352,11 @@ private Optional> toRouters(List urls) { * @return invokers */ private Map> toInvokers(List urls) { - Map> newUrlInvokerMap = new HashMap>(); + Map> newUrlInvokerMap = new HashMap<>(); if (urls == null || urls.isEmpty()) { return newUrlInvokerMap; } - Set keys = new HashSet(); + Set keys = new HashSet<>(); String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY); for (URL providerUrl : urls) { // If protocol is configured at the reference side, only the matching protocol is selected @@ -393,7 +402,7 @@ private Map> toInvokers(List urls) { enabled = url.getParameter(Constants.ENABLED_KEY, true); } if (enabled) { - invoker = new InvokerDelegate(protocol.refer(serviceType, url), url, providerUrl); + invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl); } } catch (Throwable t) { logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t); @@ -426,7 +435,7 @@ private URL mergeUrl(URL providerUrl) { this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // Merge the provider side parameters if ((providerUrl.getPath() == null || providerUrl.getPath() - .length() == 0) && "dubbo".equals(providerUrl.getProtocol())) { // Compatible version 1.0 + .length() == 0) && Constants.DUBBO_PROTOCOL.equals(providerUrl.getProtocol())) { // Compatible version 1.0 //fix by tony.chenl DUBBO-44 String path = directoryUrl.getParameter(Constants.INTERFACE_KEY); if (path != null) { @@ -474,7 +483,7 @@ private URL overrideWithConfigurators(List configurators, URL url) private void destroyAllInvokers() { Map> localUrlInvokerMap = this.urlInvokerMap; // local reference if (localUrlInvokerMap != null) { - for (Invoker invoker : new ArrayList>(localUrlInvokerMap.values())) { + for (Invoker invoker : new ArrayList<>(localUrlInvokerMap.values())) { try { invoker.destroy(); } catch (Throwable t) { @@ -505,7 +514,7 @@ private void destroyUnusedInvokers(Map> oldUrlInvokerMap, Map for (Map.Entry> entry : oldUrlInvokerMap.entrySet()) { if (!newInvokers.contains(entry.getValue())) { if (deleted == null) { - deleted = new ArrayList(); + deleted = new ArrayList<>(); } deleted.add(entry.getKey()); } @@ -597,7 +606,7 @@ public boolean isAvailable() { } Map> localUrlInvokerMap = urlInvokerMap; if (localUrlInvokerMap != null && localUrlInvokerMap.size() > 0) { - for (Invoker invoker : new ArrayList>(localUrlInvokerMap.values())) { + for (Invoker invoker : new ArrayList<>(localUrlInvokerMap.values())) { if (invoker.isAvailable()) { return true; }