You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by vi...@apache.org on 2019/07/26 02:26:17 UTC
[dubbo] 03/05: refactor
This is an automated email from the ASF dual-hosted git repository.
victory pushed a commit to branch cloud-native
in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit efd44e19470843139d89fa5bd510b401d90e721b
Author: cvictory <sh...@gmail.com>
AuthorDate: Thu Jul 18 10:10:46 2019 +0800
refactor
---
dubbo-bootstrap/pom.xml | 35 ++++++++++++--
.../bootstrap/DubboServiceProviderBootstrap.java | 4 +-
.../dubbo/registry/etcd/EtcdServiceDiscovery.java | 53 ++++++++++++++--------
.../registry/etcd/EtcdServiceDiscoveryFactory.java | 2 +-
...e.dubbo.registry.client.ServiceDiscoveryFactory | 1 +
5 files changed, 72 insertions(+), 23 deletions(-)
diff --git a/dubbo-bootstrap/pom.xml b/dubbo-bootstrap/pom.xml
index 17df134..9426264 100644
--- a/dubbo-bootstrap/pom.xml
+++ b/dubbo-bootstrap/pom.xml
@@ -26,7 +26,14 @@
<!-- Test dependencies -->
- <!-- Zookeeper dependencies for testing -->
+
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-configcenter-zookeeper</artifactId>
+ <version>${project.parent.version}</version>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
@@ -41,13 +48,29 @@
<scope>test</scope>
</dependency>
+ <!-- Zookeeper dependencies for testing -->
<dependency>
<groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-configcenter-zookeeper</artifactId>
+ <artifactId>dubbo-registry-etcd3</artifactId>
+ <version>${project.parent.version}</version>
+<!-- <scope>test</scope>-->
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-metadata-report-etcd</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-configcenter-etcd</artifactId>
+ <version>${project.parent.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+
<!-- Nacos dependencies for testing -->
<dependency>
<groupId>org.apache.dubbo</groupId>
@@ -90,6 +113,12 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>20.0</version>
+ </dependency>
+
</dependencies>
-</project>
\ No newline at end of file
+</project>
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
index 7918bff..c68fbd0 100644
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
@@ -35,9 +35,11 @@ public class DubboServiceProviderBootstrap {
new DubboBootstrap()
.application(ApplicationBuilder.newBuilder().name("dubbo-provider-demo").metadata("remote").build())
+// .metadataReport(MetadataReportBuilder.newBuilder().address("zookeeper://127.0.0.1:2181").build())
.metadataReport(MetadataReportBuilder.newBuilder().address("zookeeper://127.0.0.1:2181").build())
// .application(ApplicationBuilder.newBuilder().name("dubbo-provider-demo").build())
- .registry(RegistryBuilder.newBuilder().address("zookeeper://127.0.0.1:2181?registry-type=service").build())
+// .registry(RegistryBuilder.newBuilder().address("zookeeper://127.0.0.1:2181?registry-type=service").build())
+ .registry(RegistryBuilder.newBuilder().address("etcd3://127.0.0.1:2379?registry-type=service").build())
.protocol(ProtocolBuilder.newBuilder().port(-1).name("dubbo").build())
.service(ServiceBuilder.newBuilder().id("test").interfaceClass(EchoService.class).ref(new EchoServiceImpl()).build())
.start()
diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java
index e5a1a62..9fcc5d4 100644
--- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
+import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.event.EventListener;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.client.ServiceDiscovery;
@@ -37,6 +38,9 @@ import com.google.gson.Gson;
import java.io.File;
import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -44,7 +48,7 @@ import java.util.concurrent.ConcurrentMap;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
/**
- * @author cvictory ON 2019-07-08
+ * 2019-07-08
*/
public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<ServiceInstancesChangedEvent> {
@@ -53,9 +57,11 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
private final String root = "/services";
private final Set<String> services = new ConcurrentHashSet<>();
+ private final Map<String, ChildListener> childListenerMap = new ConcurrentHashMap<>();
private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, ChildListener>> etcdListeners = new ConcurrentHashMap<>();
private final EtcdClient etcdClient;
+ private final EventDispatcher dispatcher;
public EtcdServiceDiscovery(URL url, EtcdTransporter etcdTransporter) {
if (url.isAnyHost()) {
@@ -72,11 +78,14 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
}
}
});
+
+ this.dispatcher = EventDispatcher.getDefaultExtension();
+ this.dispatcher.addEventListener(this);
}
@Override
public void onEvent(ServiceInstancesChangedEvent event) {
-
+ registerServiceWatcher(event.getServiceName());
}
@Override
@@ -104,7 +113,8 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
}
String toPath(ServiceInstance serviceInstance) {
- return root + File.separator + serviceInstance.getServiceName() + File.separator + serviceInstance.getId();
+ return root + File.separator + serviceInstance.getServiceName() + File.separator + serviceInstance.getHost()
+ + ":" + serviceInstance.getPort();
}
@Override
@@ -139,23 +149,30 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
@Override
public void addServiceInstancesChangedListener(String serviceName, ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException {
-
+ registerServiceWatcher(serviceName);
+ dispatcher.addEventListener(listener);
}
protected void registerServiceWatcher(String serviceName) {
- String path = buildServicePath(serviceName);
- CuratorWatcher watcher = watcherCaches.computeIfAbsent(path, key ->
- new ZookeeperServiceDiscoveryChangeWatcher(this, serviceName, dispatcher));
- try {
- etcdClient.
- curatorFramework.getChildren().usingWatcher(watcher).forPath(path);
- } catch (KeeperException.NoNodeException e) {
- // ignored
- if (logger.isErrorEnabled()) {
- logger.error(e.getMessage());
- }
- } catch (Exception e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
+ String path = root + File.separator + serviceName;
+ /*
+ * if we have no category watcher listener,
+ * we find out the current listener or create one for the current category, put or get only once.
+ */
+ ChildListener childListener =
+ Optional.ofNullable(childListenerMap.get(serviceName))
+ .orElseGet(() -> {
+ ChildListener watchListener, prev;
+ prev = childListenerMap.putIfAbsent(serviceName, watchListener = (parentPath, currentChildren) ->
+ dispatcher.dispatch(new ServiceInstancesChangedEvent(serviceName, getInstances(serviceName))));
+ return prev != null ? prev : watchListener;
+ });
+
+ etcdClient.create(path);
+ /*
+ * at the first time, we want to pull already category and then watch their direct children,
+ * eg: /dubbo/interface/providers, /dubbo/interface/consumers and so on.
+ */
+ List<String> children = etcdClient.addChildListener(path, childListener);
}
}
diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java
index fced9ba..8d1e3b5 100644
--- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java
+++ b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java
@@ -22,7 +22,7 @@ import org.apache.dubbo.registry.client.ServiceDiscoveryFactory;
import org.apache.dubbo.remoting.etcd.EtcdTransporter;
/**
- * @author cvictory ON 2019-07-08
+ * 2019-07-08
*/
public class EtcdServiceDiscoveryFactory implements ServiceDiscoveryFactory {
diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory
new file mode 100644
index 0000000..ad5937a
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory
@@ -0,0 +1 @@
+etcd3=org.apache.dubbo.registry.etcd.EtcdServiceDiscoveryFactory