From 1ffa1b6bde97fe0be03581156d472297cf25663d Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 17 Jan 2019 14:45:12 +0800 Subject: [PATCH 1/2] optimize apollo extension --- .../apollo/ApolloDynamicConfiguration.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java index c73dcd75f90..8b09b15db0d 100644 --- a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java +++ b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java @@ -33,10 +33,12 @@ import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.model.ConfigChange; +import java.util.Arrays; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; +import java.util.stream.Collectors; /** * Apollo implementation, https://github.com/ctripcorp/apollo @@ -46,6 +48,7 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { private static final String APOLLO_ENV_KEY = "env"; private static final String APOLLO_ADDR_KEY = "apollo.meta"; private static final String APOLLO_CLUSTER_KEY = "apollo.cluster"; + private static final String APOLLO_PROTOCOL_PREFIX = "http://"; private URL url; private Config dubboConfig; @@ -55,12 +58,12 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); - String configAddr = url.getBackupAddress(); + String configAddr = getAddressWithProtocolPrefix(url); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } - if (StringUtils.isEmpty(configEnv) && !Constants.ANYHOST_VALUE.equals(configAddr)) { + if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { @@ -82,6 +85,21 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { } } + private String getAddressWithProtocolPrefix (URL url) { + String address = url.getBackupAddress(); + if (StringUtils.isNotEmpty(address)) { + address = Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(address)) + .map(addr -> { + if (addr.startsWith(APOLLO_PROTOCOL_PREFIX)) { + return addr; + } + return APOLLO_PROTOCOL_PREFIX + addr; + }) + .collect(Collectors.joining(",")); + } + return address; + } + /** * Since all governance rules will lay under dubbo group, this method now always uses the default dubboConfig and * ignores the group parameter. @@ -112,11 +130,8 @@ public void removeListener(String key, String group, ConfigurationListener liste @Override public String getConfig(String key, String group, long timeout) throws IllegalStateException { if (StringUtils.isNotEmpty(group) && !url.getParameter(Constants.CONFIG_GROUP_KEY, DEFAULT_GROUP).equals(group)) { - Config config = ConfigService.getConfig(group); - if (config != null) { - return config.getProperty(key, null); - } - return null; + Config config = ConfigService.getAppConfig(); + return config.getProperty(key, null); } return dubboConfig.getProperty(key, null); } From 6c3fcc110cad1a0a9fd10cf325a844ec4e68aa5e Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 17 Jan 2019 15:33:36 +0800 Subject: [PATCH 2/2] optimize apollo extension: specify the key to watch --- .../support/apollo/ApolloDynamicConfiguration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java index 8b09b15db0d..f0ad75b82d7 100644 --- a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java +++ b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java @@ -34,6 +34,7 @@ import com.ctrip.framework.apollo.model.ConfigChange; import java.util.Arrays; +import java.util.Collections; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -108,7 +109,7 @@ private String getAddressWithProtocolPrefix (URL url) { public void addListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.computeIfAbsent(group + key, k -> createTargetListener(key, group)); apolloListener.addListener(listener); - dubboConfig.addChangeListener(apolloListener); + dubboConfig.addChangeListener(apolloListener, Collections.singleton(key)); } @Override