diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java index 3cc9edbe00a0..d555a50f6349 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java @@ -61,7 +61,7 @@ public abstract class AbstractRegistry implements Registry { private static final String URL_SPLIT = "\\s+"; // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); - // Local disk cache, where the special key value.registies records the list of registry centers, and the others are the list of notified service providers + // Local disk cache, where the special key value.registries records the list of registry centers, and the others are the list of notified service providers private final Properties properties = new Properties(); // File cache timing writing private final ExecutorService registryCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveRegistryCache", true)); @@ -90,6 +90,8 @@ public AbstractRegistry(URL url) { } } this.file = file; + // When starting the subscription center, + // we need to read the local cache file for future Registry fault tolerance processing. loadProperties(); notify(url.getBackupUrls()); } @@ -244,7 +246,7 @@ public List lookup(URL url) { } } } else { - final AtomicReference> reference = new AtomicReference>(); + final AtomicReference> reference = new AtomicReference<>(); NotifyListener listener = reference::set; subscribe(url, listener); // Subscribe logic guarantees the first notify to return List urls = reference.get(); @@ -364,6 +366,13 @@ protected void notify(List urls) { } } + /** + * Notify changes from the Provider side. + * + * @param url consumer side url + * @param listener listener + * @param urls provider latest urls + */ protected void notify(URL url, NotifyListener listener, List urls) { if (url == null) { throw new IllegalArgumentException("notify url == null"); @@ -379,6 +388,7 @@ protected void notify(URL url, NotifyListener listener, List urls) { if (logger.isInfoEnabled()) { logger.info("Notify urls for subscribe url " + url + ", urls: " + urls); } + // keep every provider's category. Map> result = new HashMap<>(); for (URL u : urls) { if (UrlUtils.isMatch(url, u)) { @@ -395,8 +405,10 @@ protected void notify(URL url, NotifyListener listener, List urls) { String category = entry.getKey(); List categoryList = entry.getValue(); categoryNotified.put(category, categoryList); - saveProperties(url); listener.notify(categoryList); + // We will update our cache file after each notification. + // When our Registry has a subscribe failure due to network jitter, we can return at least the existing cache URL. + saveProperties(url); } } diff --git a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java index 06835dac6e54..86d871fe69a4 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java +++ b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java @@ -397,7 +397,7 @@ public void unsubscribe(URL url, NotifyListener listener) { @Override public List lookup(URL url) { - List urls = new ArrayList(); + List urls = new ArrayList<>(); Map> notifiedUrls = getNotified().get(url); if (notifiedUrls != null && notifiedUrls.size() > 0) { for (List values : notifiedUrls.values()) {