This is an automated email from the ASF dual-hosted git repository.
carryxyh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 4b2b673 add javadoc for registry and some code (#3140)
4b2b673 is described below
commit 4b2b673ba346ae34a5c556f043321c4b987efedd
Author: Leishunyu <[email protected]>
AuthorDate: Fri Jan 4 15:23:04 2019 +0800
add javadoc for registry and some code (#3140)
add javadoc for registry and optimize code
---
.../registry/support/AbstractRegistryFactory.java | 1 +
.../apache/dubbo/registry/dubbo/DubboRegistry.java | 15 +++----
.../dubbo/registry/dubbo/DubboRegistryFactory.java | 6 +--
.../apache/dubbo/registry/redis/RedisRegistry.java | 14 +++---
.../registry/zookeeper/ZookeeperRegistry.java | 51 +++++++++-------------
.../zookeeper/ZookeeperRegistryFactory.java | 4 ++
6 files changed, 41 insertions(+), 50 deletions(-)
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 64647f4..0f5dffc 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 abstract class AbstractRegistryFactory implements
RegistryFactory {
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 2c6b8c1..72604d6 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 class DubboRegistry extends FailbackRegistry {
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 ea9fc80..27d90ed 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 @@ public class DubboRegistryFactory extends
AbstractRegistryFactory {
.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<String>(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 class DubboRegistryFactory extends
AbstractRegistryFactory {
@Override
public Registry createRegistry(URL url) {
url = getRegistryURL(url);
- List<URL> urls = new ArrayList<URL>();
+ List<URL> 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 class DubboRegistryFactory extends
AbstractRegistryFactory {
urls.add(url.setAddress(address));
}
}
- RegistryDirectory<RegistryService> directory = new
RegistryDirectory<RegistryService>(RegistryService.class,
url.addParameter(Constants.INTERFACE_KEY,
RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY,
url.toParameterString()));
+ RegistryDirectory<RegistryService> directory = new
RegistryDirectory<>(RegistryService.class,
url.addParameter(Constants.INTERFACE_KEY,
RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY,
url.toParameterString()));
Invoker<RegistryService> 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 d372763..c0c7c22 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<String, JedisPool> jedisPools = new
ConcurrentHashMap<String, JedisPool>();
+ private final Map<String, JedisPool> jedisPools = new
ConcurrentHashMap<>();
private final ConcurrentMap<String, Notifier> notifiers = new
ConcurrentHashMap<String, Notifier>();
@@ -360,12 +360,12 @@ public class RedisRegistry extends FailbackRegistry {
admin = true;
Set<String> keys = jedis.keys(service);
if (keys != null && !keys.isEmpty()) {
- Map<String, Set<String>> serviceKeys = new
HashMap<String, Set<String>>();
+ Map<String, Set<String>> serviceKeys = new
HashMap<>();
for (String key : keys) {
String serviceKey = toServicePath(key);
Set<String> sk = serviceKeys.get(serviceKey);
if (sk == null) {
- sk = new HashSet<String>();
+ sk = new HashSet<>();
serviceKeys.put(serviceKey, sk);
}
sk.add(key);
@@ -400,8 +400,8 @@ public class RedisRegistry extends FailbackRegistry {
}
private void doNotify(Jedis jedis, String key) {
- for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<URL,
Set<NotifyListener>>(getSubscribed()).entrySet()) {
- doNotify(jedis, Arrays.asList(key), entry.getKey(), new
HashSet<NotifyListener>(entry.getValue()));
+ for (Map.Entry<URL, Set<NotifyListener>> entry : new
HashMap<>(getSubscribed()).entrySet()) {
+ doNotify(jedis, Arrays.asList(key), entry.getKey(), new
HashSet<>(entry.getValue()));
}
}
@@ -411,7 +411,7 @@ public class RedisRegistry extends FailbackRegistry {
return;
}
long now = System.currentTimeMillis();
- List<URL> result = new ArrayList<URL>();
+ List<URL> result = new ArrayList<>();
List<String> categories =
Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0]));
String consumerService = url.getServiceInterface();
for (String key : keys) {
@@ -425,7 +425,7 @@ public class RedisRegistry extends FailbackRegistry {
if (!categories.contains(Constants.ANY_VALUE) &&
!categories.contains(category)) {
continue;
}
- List<URL> urls = new ArrayList<URL>();
+ List<URL> urls = new ArrayList<>();
Map<String, String> values = jedis.hgetAll(key);
if (values != null && values.size() > 0) {
for (Map.Entry<String, String> 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 d7a8ee0..37f1f21 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<String> anyServices = new ConcurrentHashSet<String>();
+ private final Set<String> anyServices = new ConcurrentHashSet<>();
private final ConcurrentMap<URL, ConcurrentMap<NotifyListener,
ChildListener>> zkListeners = new ConcurrentHashMap<URL,
ConcurrentMap<NotifyListener, ChildListener>>();
@@ -67,15 +67,12 @@ public class ZookeeperRegistry extends FailbackRegistry {
}
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 class ZookeeperRegistry extends FailbackRegistry {
String root = toRootPath();
ConcurrentMap<NotifyListener, ChildListener> listeners =
zkListeners.get(url);
if (listeners == null) {
- zkListeners.putIfAbsent(url, new
ConcurrentHashMap<NotifyListener, ChildListener>());
+ 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<String> 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 class ZookeeperRegistry extends FailbackRegistry {
}
}
} else {
- List<URL> urls = new ArrayList<URL>();
+ List<URL> urls = new ArrayList<>();
for (String path : toCategoriesPath(url)) {
ConcurrentMap<NotifyListener, ChildListener> listeners =
zkListeners.get(url);
if (listeners == null) {
- zkListeners.putIfAbsent(url, new
ConcurrentHashMap<NotifyListener, ChildListener>());
+ 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<String> 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 class ZookeeperRegistry extends FailbackRegistry {
throw new IllegalArgumentException("lookup url == null");
}
try {
- List<String> providers = new ArrayList<String>();
+ List<String> providers = new ArrayList<>();
for (String path : toCategoriesPath(url)) {
List<String> children = zkClient.getChildren(path);
if (children != null) {
@@ -274,7 +263,7 @@ public class ZookeeperRegistry extends FailbackRegistry {
}
private List<URL> toUrlsWithoutEmpty(URL consumer, List<String> providers)
{
- List<URL> urls = new ArrayList<URL>();
+ List<URL> 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 6fd3343..d702d01 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;
}