diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java index 64647f4ba60..0f5dffc0f4c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java @@ -93,6 +93,7 @@ public Registry getRegistry(URL url) { if (registry != null) { return registry; } + //create registry by spi/ioc registry = createRegistry(url); if (registry == null) { throw new IllegalStateException("Can not create registry " + url); diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java index 2c6b8c1f1ec..72604d64474 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java @@ -71,15 +71,12 @@ public DubboRegistry(Invoker registryInvoker, RegistryService r this.registryService = registryService; // Start reconnection timer this.reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT); - reconnectFuture = reconnectTimer.scheduleWithFixedDelay(new Runnable() { - @Override - public void run() { - // Check and connect to the registry - try { - connect(); - } catch (Throwable t) { // Defensive fault tolerance - logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t); - } + reconnectFuture = reconnectTimer.scheduleWithFixedDelay(() -> { + // Check and connect to the registry + try { + connect(); + } catch (Throwable t) { // Defensive fault tolerance + logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t); } }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS); } diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java index ea9fc802ce1..27d90ed57c9 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java @@ -56,7 +56,7 @@ private static URL getRegistryURL(URL url) { .addParameterIfAbsent(Constants.TIMEOUT_KEY, "10000") .addParameterIfAbsent(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "10000") .addParameterIfAbsent(Constants.CONNECT_TIMEOUT_KEY, "10000") - .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ",")) + .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet<>(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ",")) //.addParameter(Constants.STUB_KEY, RegistryServiceStub.class.getName()) //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect") @@ -79,7 +79,7 @@ public void setCluster(Cluster cluster) { @Override public Registry createRegistry(URL url) { url = getRegistryURL(url); - List urls = new ArrayList(); + List urls = new ArrayList<>(); urls.add(url.removeParameter(Constants.BACKUP_KEY)); String backup = url.getParameter(Constants.BACKUP_KEY); if (backup != null && backup.length() > 0) { @@ -88,7 +88,7 @@ public Registry createRegistry(URL url) { urls.add(url.setAddress(address)); } } - RegistryDirectory directory = new RegistryDirectory(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString())); + RegistryDirectory directory = new RegistryDirectory<>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString())); Invoker registryInvoker = cluster.join(directory); RegistryService registryService = proxyFactory.getProxy(registryInvoker); DubboRegistry registry = new DubboRegistry(registryInvoker, registryService); diff --git a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java index d37276345df..c0c7c22ff22 100644 --- a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java +++ b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java @@ -69,7 +69,7 @@ public class RedisRegistry extends FailbackRegistry { private final String root; - private final Map jedisPools = new ConcurrentHashMap(); + private final Map jedisPools = new ConcurrentHashMap<>(); private final ConcurrentMap notifiers = new ConcurrentHashMap(); @@ -360,12 +360,12 @@ public void doSubscribe(final URL url, final NotifyListener listener) { admin = true; Set keys = jedis.keys(service); if (keys != null && !keys.isEmpty()) { - Map> serviceKeys = new HashMap>(); + Map> serviceKeys = new HashMap<>(); for (String key : keys) { String serviceKey = toServicePath(key); Set sk = serviceKeys.get(serviceKey); if (sk == null) { - sk = new HashSet(); + sk = new HashSet<>(); serviceKeys.put(serviceKey, sk); } sk.add(key); @@ -400,8 +400,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) { } private void doNotify(Jedis jedis, String key) { - for (Map.Entry> entry : new HashMap>(getSubscribed()).entrySet()) { - doNotify(jedis, Arrays.asList(key), entry.getKey(), new HashSet(entry.getValue())); + for (Map.Entry> entry : new HashMap<>(getSubscribed()).entrySet()) { + doNotify(jedis, Arrays.asList(key), entry.getKey(), new HashSet<>(entry.getValue())); } } @@ -411,7 +411,7 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< return; } long now = System.currentTimeMillis(); - List result = new ArrayList(); + List result = new ArrayList<>(); List categories = Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0])); String consumerService = url.getServiceInterface(); for (String key : keys) { @@ -425,7 +425,7 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< if (!categories.contains(Constants.ANY_VALUE) && !categories.contains(category)) { continue; } - List urls = new ArrayList(); + List urls = new ArrayList<>(); Map values = jedis.hgetAll(key); if (values != null && values.size() > 0) { for (Map.Entry entry : values.entrySet()) { diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java index d7a8ee097ac..37f1f21a16f 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java @@ -50,7 +50,7 @@ public class ZookeeperRegistry extends FailbackRegistry { private final String root; - private final Set anyServices = new ConcurrentHashSet(); + private final Set anyServices = new ConcurrentHashSet<>(); private final ConcurrentMap> zkListeners = new ConcurrentHashMap>(); @@ -67,15 +67,12 @@ public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) { } this.root = group; zkClient = zookeeperTransporter.connect(url); - zkClient.addStateListener(new StateListener() { - @Override - public void stateChanged(int state) { - if (state == RECONNECTED) { - try { - recover(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } + zkClient.addStateListener(state -> { + if (state == StateListener.RECONNECTED) { + try { + recover(); + } catch (Exception e) { + logger.error(e.getMessage(), e); } } }); @@ -133,21 +130,18 @@ public void doSubscribe(final URL url, final NotifyListener listener) { String root = toRootPath(); ConcurrentMap listeners = zkListeners.get(url); if (listeners == null) { - zkListeners.putIfAbsent(url, new ConcurrentHashMap()); + zkListeners.putIfAbsent(url, new ConcurrentHashMap<>()); listeners = zkListeners.get(url); } ChildListener zkListener = listeners.get(listener); if (zkListener == null) { - listeners.putIfAbsent(listener, new ChildListener() { - @Override - public void childChanged(String parentPath, List currentChilds) { - for (String child : currentChilds) { - child = URL.decode(child); - if (!anyServices.contains(child)) { - anyServices.add(child); - subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, - Constants.CHECK_KEY, String.valueOf(false)), listener); - } + listeners.putIfAbsent(listener, (parentPath, currentChilds) -> { + for (String child : currentChilds) { + child = URL.decode(child); + if (!anyServices.contains(child)) { + anyServices.add(child); + subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, + Constants.CHECK_KEY, String.valueOf(false)), listener); } } }); @@ -164,21 +158,16 @@ public void childChanged(String parentPath, List currentChilds) { } } } else { - List urls = new ArrayList(); + List urls = new ArrayList<>(); for (String path : toCategoriesPath(url)) { ConcurrentMap listeners = zkListeners.get(url); if (listeners == null) { - zkListeners.putIfAbsent(url, new ConcurrentHashMap()); + zkListeners.putIfAbsent(url, new ConcurrentHashMap<>()); listeners = zkListeners.get(url); } ChildListener zkListener = listeners.get(listener); if (zkListener == null) { - listeners.putIfAbsent(listener, new ChildListener() { - @Override - public void childChanged(String parentPath, List currentChilds) { - ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds)); - } - }); + listeners.putIfAbsent(listener, (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds))); zkListener = listeners.get(listener); } zkClient.create(path, false); @@ -218,7 +207,7 @@ public List lookup(URL url) { throw new IllegalArgumentException("lookup url == null"); } try { - List providers = new ArrayList(); + List providers = new ArrayList<>(); for (String path : toCategoriesPath(url)) { List children = zkClient.getChildren(path); if (children != null) { @@ -274,7 +263,7 @@ private String toUrlPath(URL url) { } private List toUrlsWithoutEmpty(URL consumer, List providers) { - List urls = new ArrayList(); + List urls = new ArrayList<>(); if (providers != null && !providers.isEmpty()) { for (String provider : providers) { provider = URL.decode(provider); diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java index 6fd33431b56..d702d0184cb 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java @@ -29,6 +29,10 @@ public class ZookeeperRegistryFactory extends AbstractRegistryFactory { private ZookeeperTransporter zookeeperTransporter; + /** + * Invisible injection of zookeeper client via IOC/SPI + * @param zookeeperTransporter + */ public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { this.zookeeperTransporter = zookeeperTransporter; }