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:14 UTC

[dubbo] branch cloud-native updated (2691784 -> 758b49f)

This is an automated email from the ASF dual-hosted git repository.

victory pushed a change to branch cloud-native
in repository https://gitbox.apache.org/repos/asf/dubbo.git.


    from 2691784  Dubbo cloud native (#4666)
     add 6edcc36  new etcd serviceDiscovery
     add f314218  Merge branch 'cloud-native' of github.com:apache/incubator-dubbo into cloud-native
     add b718bb0  fix compile issue
     new d71e5e7  Merge branch 'cloud-native' of github.com:apache/incubator-dubbo into cloud-native
     new cfbe5af  store temperily
     new efd44e1  refactor
     new 1aae695  temporily commit
     new 758b49f  merge and reinit

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 dubbo-bootstrap/pom.xml                            |  34 ++++-
 .../bootstrap/DubboServiceConsumerBootstrap.java   |   1 +
 .../bootstrap/DubboServiceProviderBootstrap.java   |   1 +
 .../dubbo/registry/etcd/EtcdServiceDiscovery.java  | 138 ++++++++++++++++-----
 .../registry/etcd/EtcdServiceDiscoveryFactory.java |   2 +-
 ...e.dubbo.registry.client.ServiceDiscoveryFactory |   1 +
 .../zookeeper/ZookeeperServiceDiscovery.java       |   2 +-
 .../org/apache/dubbo/remoting/etcd/EtcdClient.java |   8 ++
 .../dubbo/remoting/etcd/jetcd/JEtcdClient.java     |   5 +
 .../remoting/etcd/jetcd/JEtcdClientWrapper.java    |  20 +++
 10 files changed, 179 insertions(+), 33 deletions(-)
 create mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory


[dubbo] 01/05: Merge branch 'cloud-native' of github.com:apache/incubator-dubbo into cloud-native

Posted by vi...@apache.org.
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 d71e5e7636f614190942d1bb054d35560417a20b
Merge: b718bb0 fe562c3
Author: cvictory <sh...@gmail.com>
AuthorDate: Thu Jul 11 15:01:57 2019 +0800

    Merge branch 'cloud-native' of github.com:apache/incubator-dubbo into cloud-native



[dubbo] 03/05: refactor

Posted by vi...@apache.org.
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


[dubbo] 04/05: temporily commit

Posted by vi...@apache.org.
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 1aae6956dcca2b9d4982d8ec107dbf4ae2481fda
Author: cvictory <sh...@gmail.com>
AuthorDate: Mon Jul 22 16:08:33 2019 +0800

    temporily commit
---
 .../bootstrap/DubboServiceProviderBootstrap.java     |  4 ++--
 .../dubbo/registry/etcd/EtcdServiceDiscovery.java    | 19 +++++++++++++++++--
 .../org/apache/dubbo/remoting/etcd/EtcdClient.java   |  8 ++++++++
 .../dubbo/remoting/etcd/jetcd/JEtcdClient.java       |  5 +++++
 .../remoting/etcd/jetcd/JEtcdClientWrapper.java      | 20 ++++++++++++++++++++
 5 files changed, 52 insertions(+), 4 deletions(-)

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 c68fbd0..4e46a67 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
@@ -38,8 +38,8 @@ public class DubboServiceProviderBootstrap {
 //                .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("etcd3://127.0.0.1:2379?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 9fcc5d4..4cce645 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
@@ -38,6 +38,8 @@ import com.google.gson.Gson;
 
 import java.io.File;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -62,6 +64,7 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
     private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, ChildListener>> etcdListeners = new ConcurrentHashMap<>();
     private final EtcdClient etcdClient;
     private final EventDispatcher dispatcher;
+    private ServiceInstance serviceInstance;
 
     public EtcdServiceDiscovery(URL url, EtcdTransporter etcdTransporter) {
         if (url.isAnyHost()) {
@@ -72,7 +75,7 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
         etcdClient.addStateListener(state -> {
             if (state == StateListener.CONNECTED) {
                 try {
-//                    recover();
+                    recover();
                 } catch (Exception e) {
                     logger.error(e.getMessage(), e);
                 }
@@ -101,8 +104,9 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
     @Override
     public void register(ServiceInstance serviceInstance) throws RuntimeException {
         try {
+            this.serviceInstance = serviceInstance;
             String path = toPath(serviceInstance);
-            etcdClient.put(path, new Gson().toJson(serviceInstance));
+            etcdClient.putEphemeral(path, new Gson().toJson(serviceInstance));
             services.add(serviceInstance.getServiceName());
         } catch (Throwable e) {
             throw new RpcException("Failed to register " + serviceInstance + " to etcd " + etcdClient.getUrl()
@@ -137,6 +141,7 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
             String path = toPath(serviceInstance);
             etcdClient.delete(path);
             services.remove(serviceInstance.getServiceName());
+            this.serviceInstance = null;
         } catch (Throwable e) {
             throw new RpcException("Failed to unregister " + serviceInstance + " to etcd " + etcdClient.getUrl() + ", cause: " + e.getMessage(), e);
         }
@@ -175,4 +180,14 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
          */
         List<String> children = etcdClient.addChildListener(path, childListener);
     }
+
+    private void recover() throws Exception {
+        // register
+        if (serviceInstance != null) {
+            if (logger.isInfoEnabled()) {
+                logger.info("Recover application register: " + serviceInstance);
+            }
+            register(serviceInstance);
+        }
+    }
 }
diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java
index 286be93..e23b870 100644
--- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java
+++ b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java
@@ -180,4 +180,12 @@ public interface EtcdClient {
      */
     boolean put(String key, String value);
 
+    /**
+     * Put the key value pair to etcd (Ephemeral)
+     * @param key the specified key
+     * @param value the paired value
+     * @return true if put success
+     */
+    boolean putEphemeral(String key, String value);
+
 }
diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java
index 4c055b4..01a6025 100644
--- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java
+++ b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java
@@ -203,6 +203,11 @@ public class JEtcdClient extends AbstractEtcdClient<JEtcdClient.EtcdWatcher> {
         return clientWrapper.put(key, value);
     }
 
+    @Override
+    public boolean putEphemeral(String key, String value) {
+        return clientWrapper.put(key, value);
+    }
+
     public ManagedChannel getChannel() {
         return clientWrapper.getChannel();
     }
diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java
index 7ef6777..f635005 100644
--- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java
+++ b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java
@@ -667,6 +667,26 @@ public class JEtcdClientWrapper {
         return false;
     }
 
+    public boolean putEphemeral(final String key, String value) {
+        try {
+            return RetryLoops.invokeWithRetry(
+                    () -> {
+                        requiredNotNull(client, failed);
+                        // recovery an retry
+                        keepAlive();
+                        final long leaseId = globalLeaseId;
+                        client.getKVClient()
+                                .put(ByteSequence.from(key, UTF_8)
+                                        , ByteSequence.from(String.valueOf(value), UTF_8)
+                                        , PutOption.newBuilder().withLeaseId(leaseId).build())
+                                .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
+                        return true;
+                    }, retryPolicy);
+        } catch (Exception e) {
+            throw new IllegalStateException(e.getMessage(), e);
+        }
+    }
+
     private void retry() {
         if (!failedRegistered.isEmpty()) {
             Set<String> failed = new HashSet<String>(failedRegistered);


[dubbo] 05/05: merge and reinit

Posted by vi...@apache.org.
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 758b49f1e83cc71c732cb683b623153f57ce1112
Merge: 1aae695 2691784
Author: cvictory <sh...@gmail.com>
AuthorDate: Fri Jul 26 10:25:45 2019 +0800

    merge and reinit

 dubbo-bootstrap/pom.xml                            |  11 +-
 .../dubbo/bootstrap/ApplicationSettings.java       | 127 -----
 .../org/apache/dubbo/bootstrap/DubboBootstrap.java | 394 +++++++------
 .../apache/dubbo/bootstrap/ProtocolSettings.java   | 215 -------
 .../apache/dubbo/bootstrap/ReferenceSettings.java  | 334 -----------
 .../apache/dubbo/bootstrap/RegistrySettings.java   | 164 ------
 .../apache/dubbo/bootstrap/ServiceSettings.java    | 384 -------------
 .../apache/dubbo/bootstrap/DubboBootstrapTest.java |  14 -
 .../bootstrap/DubboServiceConsumerBootstrap.java   |  24 +-
 .../bootstrap/DubboServiceProviderBootstrap.java   |  25 +-
 dubbo-common/pom.xml                               |   4 +
 .../config/configcenter/ConfigChangeEvent.java     |   6 +-
 .../config/configcenter/ConfigurationListener.java |   4 +-
 .../config/configcenter/DynamicConfiguration.java  | 105 +++-
 .../configcenter/DynamicConfigurationFactory.java  |  20 +-
 .../file/FileSystemDynamicConfiguration.java       | 628 +++++++++++++++++++++
 .../FileSystemDynamicConfigurationFactory.java     |  21 +-
 .../configcenter/nop/NopDynamicConfiguration.java  |   1 +
 .../nop/NopDynamicConfigurationFactory.java        |   1 +
 .../wrapper/CompositeDynamicConfiguration.java     |   3 -
 .../apache/dubbo/common/utils/ReflectUtils.java    |  30 +-
 ...config.configcenter.DynamicConfigurationFactory |   3 +-
 .../DynamicConfigurationFactoryTest.java           |  25 +-
 .../file/FileSystemDynamicConfigurationTest.java   | 169 ++++++
 dubbo-common/src/test/resources/log4j.xml          |   4 +-
 .../dubbo/config/AbstractInterfaceConfig.java      |  23 +-
 .../org/apache/dubbo/config/RegistryConfig.java    |  12 +-
 .../org/apache/dubbo/config/ServiceConfig.java     |  17 +-
 .../dubbo/config/builders/AbstractBuilder.java     |   2 +-
 .../apache/dubbo/config/context/ConfigManager.java | 433 +++++++-------
 .../metadata/ServiceInstancePortCustomizer.java    |   1 -
 .../dubbo/config/context/ConfigManagerTest.java    |  71 +++
 .../ConfigurableMetadataServiceExporterTest.java   |   4 +-
 .../resources/META-INF/spring/dubbo-provider.xml   |  16 +-
 .../support/nacos/NacosDynamicConfiguration.java   |  13 +-
 dubbo-dependencies-bom/pom.xml                     |  11 +-
 .../client/FileSystemServiceDiscovery.java         | 114 ++++
 37 files changed, 1701 insertions(+), 1732 deletions(-)

diff --cc dubbo-bootstrap/pom.xml
index 9426264,e38e05d..77af3e2
--- a/dubbo-bootstrap/pom.xml
+++ b/dubbo-bootstrap/pom.xml
@@@ -113,10 -90,10 +113,15 @@@
              <scope>test</scope>
          </dependency>
  
++<!--        <dependency>-->
++<!--            <groupId>com.google.guava</groupId>-->
++<!--            <artifactId>guava</artifactId>-->
++<!--            <version>20.0</version>-->
++<!--        </dependency>-->
          <dependency>
-             <groupId>com.google.guava</groupId>
-             <artifactId>guava</artifactId>
-             <version>20.0</version>
+             <groupId>org.apache.curator</groupId>
+             <artifactId>curator-test</artifactId>
+             <scope>test</scope>
          </dependency>
  
      </dependencies>
diff --cc dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
index 48ac788,191b233..d88dcc3
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
@@@ -17,11 -17,8 +17,9 @@@
  package org.apache.dubbo.bootstrap;
  
  import org.apache.dubbo.config.ReferenceConfig;
- import org.apache.dubbo.config.builders.ApplicationBuilder;
- import org.apache.dubbo.config.builders.ReferenceBuilder;
- import org.apache.dubbo.config.builders.RegistryBuilder;
- import org.apache.dubbo.config.utils.ReferenceConfigCache;
+ import org.apache.dubbo.config.context.ConfigManager;
+ 
 +
  /**
   * Dubbo Provider Bootstrap
   *
diff --cc dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
index 4e46a67,1ed573f..019a61a
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
@@@ -31,17 -23,15 +23,16 @@@ package org.apache.dubbo.bootstrap
   */
  public class DubboServiceProviderBootstrap {
  
-     public static void main(String[] args) throws IOException {
- 
+     public static void main(String[] args) {
          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())
+                 .application("dubbo-provider-demo")
+                 // Zookeeper in service registry type
+                 .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
+                 // Nacos
+                 .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry-type=service"))
 +//                .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())
+                 .protocol(builder -> builder.port(-1).name("dubbo"))
+                 .service(builder -> builder.id("test").interfaceClass(EchoService.class).ref(new EchoServiceImpl()))
                  .start()
                  .await();
      }


[dubbo] 02/05: store temperily

Posted by vi...@apache.org.
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 cfbe5af378381681661bafaf1d11dc54754ccba7
Author: cvictory <sh...@gmail.com>
AuthorDate: Thu Jul 11 19:19:17 2019 +0800

    store temperily
---
 .../bootstrap/DubboServiceConsumerBootstrap.java   |  16 ++--
 .../dubbo/registry/etcd/EtcdServiceDiscovery.java  | 100 +++++++++++++++------
 .../zookeeper/ZookeeperServiceDiscovery.java       |   2 +-
 3 files changed, 84 insertions(+), 34 deletions(-)

diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
index 4e03c1b..48ac788 100644
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
@@ -16,9 +16,11 @@
  */
 package org.apache.dubbo.bootstrap;
 
+import org.apache.dubbo.config.ReferenceConfig;
 import org.apache.dubbo.config.builders.ApplicationBuilder;
 import org.apache.dubbo.config.builders.ReferenceBuilder;
 import org.apache.dubbo.config.builders.RegistryBuilder;
+import org.apache.dubbo.config.utils.ReferenceConfigCache;
 
 /**
  * Dubbo Provider Bootstrap
@@ -38,14 +40,12 @@ public class DubboServiceConsumerBootstrap {
                 .await();
 
         // TODO,
-//        ReferenceConfig<EchoService> referenceConfig = ReferenceConfigCache.getCache().get(EchoService.class.getName(), EchoService.class);
-//
-//        EchoService echoService = referenceConfig.get();
-//
-//        for (int i = 0; i < 500; i++) {
-//            Thread.sleep(2000L);
-//            System.out.println(echoService.echo("Hello,World"));
-//        }
+        EchoService echoService = ReferenceConfigCache.getCache().get(EchoService.class.getName(), EchoService.class);
+
+        for (int i = 0; i < 500; i++) {
+            Thread.sleep(2000L);
+            System.out.println(echoService.echo("Hello,World"));
+        }
 
     }
 }
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 60075d4..e5a1a62 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
@@ -27,12 +27,22 @@ import org.apache.dubbo.registry.client.ServiceInstance;
 import org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent;
 import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
 import org.apache.dubbo.remoting.etcd.ChildListener;
+import org.apache.dubbo.remoting.etcd.EtcdClient;
 import org.apache.dubbo.remoting.etcd.EtcdTransporter;
+import org.apache.dubbo.remoting.etcd.StateListener;
+import org.apache.dubbo.remoting.etcd.option.OptionUtil;
+import org.apache.dubbo.rpc.RpcException;
 
+import com.google.gson.Gson;
+
+import java.io.File;
+import java.util.Collections;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
+
 /**
  * @author cvictory ON 2019-07-08
  */
@@ -40,33 +50,28 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
 
     private final static Logger logger = LoggerFactory.getLogger(EtcdServiceDiscovery.class);
 
-//    private final String root;
+    private final String root = "/services";
 
-    private final Set<String> anyServices = new ConcurrentHashSet<>();
+    private final Set<String> services = new ConcurrentHashSet<>();
 
     private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, ChildListener>> etcdListeners = new ConcurrentHashMap<>();
-//    private final EtcdClient etcdClient;
+    private final EtcdClient etcdClient;
 
     public EtcdServiceDiscovery(URL url, EtcdTransporter etcdTransporter) {
-//        if (url.isAnyHost()) {
-//            throw new IllegalStateException("Service discovery address is invalid, actual: '" + url.getHost() + "'");
-//        }
-////        String group = url.getParameter(GROUP_KEY, DEFAULT_ROOT);
-//        if (!group.startsWith(PATH_SEPARATOR)) {
-//            group = PATH_SEPARATOR + group;
-//        }
-//        this.root = group;
-//        etcdClient = etcdTransporter.connect(url);
-//
-//        etcdClient.addStateListener(state -> {
-//            if (state == StateListener.CONNECTED) {
-//                try {
-////                    recover();
-//                } catch (Exception e) {
-//                    logger.error(e.getMessage(), e);
-//                }
-//            }
-//        });
+        if (url.isAnyHost()) {
+            throw new IllegalStateException("Service discovery address is invalid, actual: '" + url.getHost() + "'");
+        }
+        etcdClient = etcdTransporter.connect(url);
+
+        etcdClient.addStateListener(state -> {
+            if (state == StateListener.CONNECTED) {
+                try {
+//                    recover();
+                } catch (Exception e) {
+                    logger.error(e.getMessage(), e);
+                }
+            }
+        });
     }
 
     @Override
@@ -86,26 +91,71 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener<Ser
 
     @Override
     public void register(ServiceInstance serviceInstance) throws RuntimeException {
+        try {
+            String path = toPath(serviceInstance);
+            etcdClient.put(path, new Gson().toJson(serviceInstance));
+            services.add(serviceInstance.getServiceName());
+        } catch (Throwable e) {
+            throw new RpcException("Failed to register " + serviceInstance + " to etcd " + etcdClient.getUrl()
+                    + ", cause: " + (OptionUtil.isProtocolError(e)
+                    ? "etcd3 registry may not be supported yet or etcd3 registry is not available."
+                    : e.getMessage()), e);
+        }
+    }
 
+    String toPath(ServiceInstance serviceInstance) {
+        return root + File.separator + serviceInstance.getServiceName() + File.separator + serviceInstance.getId();
     }
 
     @Override
     public void update(ServiceInstance serviceInstance) throws RuntimeException {
-
+        try {
+            String path = toPath(serviceInstance);
+            etcdClient.put(path, new Gson().toJson(serviceInstance));
+            services.add(serviceInstance.getServiceName());
+        } catch (Throwable e) {
+            throw new RpcException("Failed to register " + serviceInstance + " to etcd " + etcdClient.getUrl()
+                    + ", cause: " + (OptionUtil.isProtocolError(e)
+                    ? "etcd3 registry may not be supported yet or etcd3 registry is not available."
+                    : e.getMessage()), e);
+        }
     }
 
     @Override
     public void unregister(ServiceInstance serviceInstance) throws RuntimeException {
-
+        try {
+            String path = toPath(serviceInstance);
+            etcdClient.delete(path);
+            services.remove(serviceInstance.getServiceName());
+        } catch (Throwable e) {
+            throw new RpcException("Failed to unregister " + serviceInstance + " to etcd " + etcdClient.getUrl() + ", cause: " + e.getMessage(), e);
+        }
     }
 
     @Override
     public Set<String> getServices() {
-        return null;
+        return Collections.unmodifiableSet(services);
     }
 
     @Override
     public void addServiceInstancesChangedListener(String serviceName, ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException {
 
     }
+
+    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);
+        }
+    }
 }
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
index 6776a74..888972a 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
@@ -156,4 +156,4 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery, EventListene
         // re-register again
         registerServiceWatcher(serviceName);
     }
-}
\ No newline at end of file
+}