|
52 | 52 | import java.util.HashSet; |
53 | 53 | import java.util.List; |
54 | 54 | import java.util.Map; |
| 55 | +import java.util.Objects; |
55 | 56 | import java.util.Optional; |
56 | 57 | import java.util.Set; |
57 | 58 | import java.util.stream.Collectors; |
|
64 | 65 | import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; |
65 | 66 | import static org.apache.dubbo.common.Constants.ROUTERS_CATEGORY; |
66 | 67 | import static org.apache.dubbo.common.Constants.ROUTE_PROTOCOL; |
67 | | -import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; |
68 | 68 |
|
69 | 69 |
|
70 | 70 | /** |
@@ -124,7 +124,7 @@ public RegistryDirectory(Class<T> serviceType, URL url) { |
124 | 124 | this.queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY)); |
125 | 125 | this.overrideDirectoryUrl = this.directoryUrl = turnRegistryUrlToConsumerUrl(url); |
126 | 126 | String group = directoryUrl.getParameter(Constants.GROUP_KEY, ""); |
127 | | - this.multiGroup = group != null && ("*".equals(group) || group.contains(",")); |
| 127 | + this.multiGroup = group != null && (Constants.ANY_VALUE.equals(group) || group.contains(",")); |
128 | 128 | } |
129 | 129 |
|
130 | 130 | private URL turnRegistryUrlToConsumerUrl(URL url) { |
@@ -189,21 +189,30 @@ public void destroy() { |
189 | 189 |
|
190 | 190 | @Override |
191 | 191 | public synchronized void notify(List<URL> urls) { |
192 | | - List<URL> categoryUrls = urls.stream() |
| 192 | + Map<String, List<URL>> categoryUrls = urls.stream() |
| 193 | + .filter(Objects::nonNull) |
193 | 194 | .filter(this::isValidCategory) |
194 | 195 | .filter(this::isNotCompatibleFor26x) |
195 | | - .collect(Collectors.toList()); |
| 196 | + .collect(Collectors.groupingBy(url -> { |
| 197 | + if (UrlUtils.isConfigurator(url)) { |
| 198 | + return CONFIGURATORS_CATEGORY; |
| 199 | + } else if (UrlUtils.isRoute(url)) { |
| 200 | + return ROUTERS_CATEGORY; |
| 201 | + } else if (UrlUtils.isProvider(url)) { |
| 202 | + return PROVIDERS_CATEGORY; |
| 203 | + } |
| 204 | + return ""; |
| 205 | + })); |
196 | 206 |
|
197 | | - /** |
198 | | - * TODO Try to refactor the processing of these three type of urls using Collectors.groupBy()? |
199 | | - */ |
200 | | - this.configurators = Configurator.toConfigurators(classifyUrls(categoryUrls, UrlUtils::isConfigurator)) |
201 | | - .orElse(configurators); |
| 207 | + List<URL> configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList()); |
| 208 | + this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators); |
202 | 209 |
|
203 | | - toRouters(classifyUrls(categoryUrls, UrlUtils::isRoute)).ifPresent(this::addRouters); |
| 210 | + List<URL> routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList()); |
| 211 | + toRouters(routerURLs).ifPresent(this::addRouters); |
204 | 212 |
|
205 | 213 | // providers |
206 | | - refreshOverrideAndInvoker(classifyUrls(categoryUrls, UrlUtils::isProvider)); |
| 214 | + List<URL> providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList()); |
| 215 | + refreshOverrideAndInvoker(providerURLs); |
207 | 216 | } |
208 | 217 |
|
209 | 218 | private void refreshOverrideAndInvoker(List<URL> urls) { |
@@ -283,7 +292,7 @@ private void refreshInvoker(List<URL> invokerUrls) { |
283 | 292 |
|
284 | 293 | private List<Invoker<T>> toMergeInvokerList(List<Invoker<T>> invokers) { |
285 | 294 | List<Invoker<T>> mergedInvokers = new ArrayList<>(); |
286 | | - Map<String, List<Invoker<T>>> groupMap = new HashMap<String, List<Invoker<T>>>(); |
| 295 | + Map<String, List<Invoker<T>>> groupMap = new HashMap<>(); |
287 | 296 | for (Invoker<T> invoker : invokers) { |
288 | 297 | String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, ""); |
289 | 298 | groupMap.computeIfAbsent(group, k -> new ArrayList<>()); |
@@ -343,11 +352,11 @@ private Optional<List<Router>> toRouters(List<URL> urls) { |
343 | 352 | * @return invokers |
344 | 353 | */ |
345 | 354 | private Map<String, Invoker<T>> toInvokers(List<URL> urls) { |
346 | | - Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>(); |
| 355 | + Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<>(); |
347 | 356 | if (urls == null || urls.isEmpty()) { |
348 | 357 | return newUrlInvokerMap; |
349 | 358 | } |
350 | | - Set<String> keys = new HashSet<String>(); |
| 359 | + Set<String> keys = new HashSet<>(); |
351 | 360 | String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY); |
352 | 361 | for (URL providerUrl : urls) { |
353 | 362 | // If protocol is configured at the reference side, only the matching protocol is selected |
@@ -393,7 +402,7 @@ private Map<String, Invoker<T>> toInvokers(List<URL> urls) { |
393 | 402 | enabled = url.getParameter(Constants.ENABLED_KEY, true); |
394 | 403 | } |
395 | 404 | if (enabled) { |
396 | | - invoker = new InvokerDelegate<T>(protocol.refer(serviceType, url), url, providerUrl); |
| 405 | + invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl); |
397 | 406 | } |
398 | 407 | } catch (Throwable t) { |
399 | 408 | logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t); |
@@ -426,7 +435,7 @@ private URL mergeUrl(URL providerUrl) { |
426 | 435 | this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // Merge the provider side parameters |
427 | 436 |
|
428 | 437 | if ((providerUrl.getPath() == null || providerUrl.getPath() |
429 | | - .length() == 0) && "dubbo".equals(providerUrl.getProtocol())) { // Compatible version 1.0 |
| 438 | + .length() == 0) && Constants.DUBBO_PROTOCOL.equals(providerUrl.getProtocol())) { // Compatible version 1.0 |
430 | 439 | //fix by tony.chenl DUBBO-44 |
431 | 440 | String path = directoryUrl.getParameter(Constants.INTERFACE_KEY); |
432 | 441 | if (path != null) { |
@@ -474,7 +483,7 @@ private URL overrideWithConfigurators(List<Configurator> configurators, URL url) |
474 | 483 | private void destroyAllInvokers() { |
475 | 484 | Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference |
476 | 485 | if (localUrlInvokerMap != null) { |
477 | | - for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) { |
| 486 | + for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) { |
478 | 487 | try { |
479 | 488 | invoker.destroy(); |
480 | 489 | } catch (Throwable t) { |
@@ -505,7 +514,7 @@ private void destroyUnusedInvokers(Map<String, Invoker<T>> oldUrlInvokerMap, Map |
505 | 514 | for (Map.Entry<String, Invoker<T>> entry : oldUrlInvokerMap.entrySet()) { |
506 | 515 | if (!newInvokers.contains(entry.getValue())) { |
507 | 516 | if (deleted == null) { |
508 | | - deleted = new ArrayList<String>(); |
| 517 | + deleted = new ArrayList<>(); |
509 | 518 | } |
510 | 519 | deleted.add(entry.getKey()); |
511 | 520 | } |
@@ -597,7 +606,7 @@ public boolean isAvailable() { |
597 | 606 | } |
598 | 607 | Map<String, Invoker<T>> localUrlInvokerMap = urlInvokerMap; |
599 | 608 | if (localUrlInvokerMap != null && localUrlInvokerMap.size() > 0) { |
600 | | - for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) { |
| 609 | + for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) { |
601 | 610 | if (invoker.isAvailable()) { |
602 | 611 | return true; |
603 | 612 | } |
|
0 commit comments