Skip to content

Commit

Permalink
use 1.0.0 version of apollo and some minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Aug 17, 2018
1 parent 83076fd commit 687f2ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions dubbo-config/dubbo-config-dynamic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.10.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
Expand Down Expand Up @@ -71,4 +71,4 @@
<version>1.8</version>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import java.util.HashSet;
import java.util.Set;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.dynamic.AbstractDynamicConfiguration;
import org.apache.dubbo.config.dynamic.ConfigChangeType;
import org.apache.dubbo.config.dynamic.ConfigType;
Expand All @@ -34,14 +37,12 @@
*/
public class ApolloDynamicConfiguration extends AbstractDynamicConfiguration {
private static final String APOLLO_ENV_KEY = "env";
// FIXME the key?
private static final String APOLLO_ADDR_KEY = "";
private static final String APOLLO_ADDR_KEY = "apollo.meta";
private static final String APOLLO_CLUSTER_KEY = "apollo.cluster";
/**
* support two namespaces: application -> dubbo
*/
private static final String APOLLO_DEFAULT_NAMESPACE = "application";

// support one namespace, shall we support more than one namespace?
private Config dubboConfig;
private Config appConfig;

public ApolloDynamicConfiguration() {

Expand All @@ -57,6 +58,7 @@ public void init() {
String configEnv = url.getParameter(Constants.CONFIG_ENV_KEY);
String configAddr = url.getParameter(Constants.CONFIG_ADDRESS_KEY);
String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY);
String configNamespace = url.getParameter(Constants.CONFIG_NAMESPACE_KEY);
if (configEnv != null) {
System.setProperty(APOLLO_ENV_KEY, configEnv);
}
Expand All @@ -66,32 +68,28 @@ public void init() {
if (configCluster != null) {
System.setProperty(APOLLO_CLUSTER_KEY, configCluster);
}
if (StringUtils.isEmpty(configNamespace)) {
configNamespace = APOLLO_DEFAULT_NAMESPACE;
}

dubboConfig = ConfigService.getConfig("dubbo");
appConfig = ConfigService.getAppConfig();
dubboConfig = ConfigService.getConfig(configNamespace);
}

@Override
public void addListener(String key, ConfigurationListener listener) {
this.appConfig.addChangeListener(new ApolloListener(listener));
this.dubboConfig.addChangeListener(new ApolloListener(listener));
ApolloListener apolloListener = new ApolloListener(listener);
this.dubboConfig.addChangeListener(apolloListener, apolloListener.getInterestedKeys());
}

@Override
public String getConfig(String key, String group, ConfigurationListener listener) {
this.appConfig.addChangeListener(new ApolloListener(listener));
this.dubboConfig.addChangeListener(new ApolloListener(listener));
addListener(key, listener);
return getInternalProperty(key, group, 0L);
}

@Override
protected String getInternalProperty(String key, String group, long timeout) {
String value = appConfig.getProperty(key, null);
if (value == null) {
value = dubboConfig.getProperty(key, null);
}

return value;
return dubboConfig.getProperty(key, null);
}

public ConfigChangeType getChangeType(PropertyChangeType changeType) {
Expand All @@ -102,8 +100,9 @@ public ConfigChangeType getChangeType(PropertyChangeType changeType) {
}

private class ApolloListener implements ConfigChangeListener {
private ConfigurationListener listener;
private URL url;
private final ConfigurationListener listener;
private final URL url;
private final Set<String> interestedKeys;

public ApolloListener(ConfigurationListener listener) {
this(listener.getUrl(), listener);
Expand All @@ -112,6 +111,16 @@ public ApolloListener(ConfigurationListener listener) {
public ApolloListener(URL url, ConfigurationListener listener) {
this.url = url;
this.listener = listener;
this.interestedKeys = initializeInterestedKeys(url);
}

private Set<String> initializeInterestedKeys(URL url) {
Set<String> interestedKeys = new HashSet<>();
interestedKeys.add(url.getServiceKey() + Constants.CONFIGURATORS_SUFFIX);
interestedKeys.add(url.getParameter(Constants.APPLICATION_KEY) + Constants.CONFIGURATORS_SUFFIX);
interestedKeys.add(url.getServiceKey() + Constants.ROUTERS_SUFFIX);

return interestedKeys;
}

@Override
Expand All @@ -129,6 +138,10 @@ public void onChange(ConfigChangeEvent changeEvent) {
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}

Set<String> getInterestedKeys() {
return interestedKeys;
}
}

}

0 comments on commit 687f2ce

Please sign in to comment.