You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ts...@apache.org on 2020/07/27 16:50:40 UTC

[dubbo] branch master updated: fix issue 6504, and polish some code (#6505)

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

tswstarplanet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 47ed754  fix issue 6504, and polish some code (#6505)
47ed754 is described below

commit 47ed75489206c4c944dd0eb187157eaa0ffc0527
Author: tswstarplanet <ts...@apache.org>
AuthorDate: Tue Jul 28 00:50:12 2020 +0800

    fix issue 6504, and polish some code (#6505)
---
 .../consul/ConsulDynamicConfiguration.java         | 24 +++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java
index 5bf8abe..c1e5a44 100644
--- a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java
+++ b/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java
@@ -32,6 +32,7 @@ import com.orbitz.consul.Consul;
 import com.orbitz.consul.KeyValueClient;
 import com.orbitz.consul.cache.KVCache;
 import com.orbitz.consul.model.kv.Value;
+import org.apache.dubbo.common.utils.StringUtils;
 
 import java.util.Collection;
 import java.util.LinkedHashSet;
@@ -55,17 +56,26 @@ public class ConsulDynamicConfiguration extends TreePathDynamicConfiguration {
     private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000;
     private static final String WATCH_TIMEOUT = "consul-watch-timeout";
 
-    private Consul client;
+    private final Consul client;
 
-    private KeyValueClient kvClient;
+    private final KeyValueClient kvClient;
 
-    private ConcurrentMap<String, ConsulListener> watchers = new ConcurrentHashMap<>();
+    private final int watchTimeout;
+
+    private final ConcurrentMap<String, ConsulListener> watchers = new ConcurrentHashMap<>();
 
     public ConsulDynamicConfiguration(URL url) {
         super(url);
+        watchTimeout = url.getParameter(WATCH_TIMEOUT, DEFAULT_WATCH_TIMEOUT);
         String host = url.getHost();
         int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT;
-        client = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build();
+        Consul.Builder builder = Consul.builder()
+                .withHostAndPort(HostAndPort.fromParts(host, port));
+        String token = url.getParameter("token", (String) null);
+        if (StringUtils.isNotEmpty(token)) {
+            builder.withAclToken(token);
+        }
+        client = builder.build();
         this.kvClient = client.keyValueClient();
     }
 
@@ -128,8 +138,8 @@ public class ConsulDynamicConfiguration extends TreePathDynamicConfiguration {
     private class ConsulListener implements KVCache.Listener<String, Value> {
 
         private KVCache kvCache;
-        private Set<ConfigurationListener> listeners = new LinkedHashSet<>();
-        private String normalizedKey;
+        private final Set<ConfigurationListener> listeners = new LinkedHashSet<>();
+        private final String normalizedKey;
 
         public ConsulListener(String normalizedKey) {
             this.normalizedKey = normalizedKey;
@@ -137,7 +147,7 @@ public class ConsulDynamicConfiguration extends TreePathDynamicConfiguration {
         }
 
         private void initKVCache() {
-            this.kvCache = KVCache.newCache(kvClient, normalizedKey);
+            this.kvCache = KVCache.newCache(kvClient, normalizedKey, watchTimeout);
             kvCache.addListener(this);
             kvCache.start();
         }