This is an automated email from the ASF dual-hosted git repository.
victory pushed a commit to branch 2.7.0-release
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/2.7.0-release by this push:
new 87a1bd7 optimize apollo extension (#3263)
87a1bd7 is described below
commit 87a1bd7dfe3f713a34d7daff89655b0d5275e6b8
Author: ken.lj <[email protected]>
AuthorDate: Thu Jan 17 16:35:09 2019 +0800
optimize apollo extension (#3263)
* optimize apollo extension
* optimize apollo extension: specify the key to watch
---
.../support/apollo/ApolloDynamicConfiguration.java | 32 ++++++++++++++++------
1 file changed, 24 insertions(+), 8 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 c73dcd7..f0ad75b 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.ConfigSourceType;
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 class ApolloDynamicConfiguration implements
DynamicConfiguration {
@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);
}