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..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 @@ -33,10 +33,13 @@ import com.ctrip.framework.apollo.enums.PropertyChangeType; 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; import java.util.concurrent.CopyOnWriteArraySet; +import java.util.stream.Collectors; /** * Apollo implementation, https://github.com/ctripcorp/apollo @@ -46,6 +49,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 +59,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 +86,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. @@ -90,7 +109,7 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { 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 @@ -112,11 +131,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); }