Skip to content

Commit

Permalink
add multiple appId,pull other appId config into config. (#70)
Browse files Browse the repository at this point in the history
* meger

* update some suggestion

* meger

* fix some test problem

* temp submit

* 0.update some suggestion.
1.MultipleConfig add order property
2.ApolloConfigChangeListener add appId property

* update coderabbitai suggestion

* update some suggestion

* update CHANGES.md. add feature description.

* Apply suggestions from code review

* Update apollo-client/src/main/java/com/ctrip/framework/apollo/spring/spi/DefaultApolloConfigRegistrarHelper.java

---------

Co-authored-by: Jason Song <[email protected]>
  • Loading branch information
TerryLam2010 and nobodyiam authored Nov 23, 2024
1 parent 67eb866 commit d4b76f8
Show file tree
Hide file tree
Showing 81 changed files with 1,284 additions and 548 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Apollo Java 2.4.0
* [Feature openapi query namespace support not fill item](https://github.com/apolloconfig/apollo-java/pull/83)
* [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74)
* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)
* [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70)


------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public PureApolloConfig(String namespace,
super(namespace, configRepository);
}

/**
* Constructor.
*
* @param appId the appId of this config instance
* @param namespace the namespace of this config instance
* @param configRepository the config repository for this config instance
*/
public PureApolloConfig(String appId, String namespace,
ConfigRepository configRepository) {
super(appId, namespace, configRepository);
}

@Override
public String getProperty(String key, String defaultValue) {
// step 1: check local cached properties file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class PureApolloConfigFactory extends DefaultConfigFactory implements ConfigFactory {

@Override
protected Config createRepositoryConfig(String namespace, ConfigRepository configRepository) {
return new PureApolloConfig(namespace, configRepository);
protected Config createRepositoryConfig(String appId, String namespace, ConfigRepository configRepository) {
return new PureApolloConfig(appId, namespace, configRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public interface ConfigFile {
*/
boolean hasContent();

/**
* Get the appId of this config file instance
* @return the appId
*
* @since 2.4.0
*/
default String getAppId(){
return null;
}

/**
* Get the namespace of this config file instance
* @return the namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.internals.ConfigMonitorInitializer;
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigRegistry;
import com.ctrip.framework.apollo.util.ConfigUtil;

/**
* Entry point for client config use
Expand Down Expand Up @@ -91,6 +93,10 @@ public static Config getConfig(String namespace) {
return s_instance.getManager().getConfig(namespace);
}

public static Config getConfig(String appId, String namespace) {
return s_instance.getManager().getConfig(appId, namespace);
}

public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return s_instance.getManager().getConfigFile(namespace, configFileFormat);
}
Expand All @@ -111,16 +117,32 @@ static void setConfig(Config config) {
*/
static void setConfig(String namespace, final Config config) {
s_instance.getRegistry().register(namespace, new ConfigFactory() {

private final ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

@Override
public Config create(String namespace) {
return config;
}

@Override
public Config create(String appId, String namespace) {
if(!StringUtils.equals(appId, m_configUtil.getAppId())){
throw new IllegalArgumentException("Provided appId '" + appId + "' does not match the default appId '" + m_configUtil.getAppId() + "'");
}
return config;
}

@Override
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return null;
}

@Override
public ConfigFile createConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat) {
return null;
}

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,15 @@ protected void clearConfigCache() {
/**
* @param changes map's key is config property's key
*/
protected void fireConfigChange(String namespace, Map<String, ConfigChange> changes) {
protected void fireConfigChange(String appId, String namespace, Map<String, ConfigChange> changes) {
final Set<String> changedKeys = changes.keySet();
final List<ConfigChangeListener> listeners = this.findMatchedConfigChangeListeners(changedKeys);

// notify those listeners
for (ConfigChangeListener listener : listeners) {
Set<String> interestedChangedKeys = resolveInterestedChangedKeys(listener, changedKeys);
InterestedConfigChangeEvent interestedConfigChangeEvent = new InterestedConfigChangeEvent(
namespace, changes, interestedChangedKeys);
appId, namespace, changes, interestedChangedKeys);
this.notifyAsync(listener, interestedConfigChangeEvent);
}
}
Expand Down Expand Up @@ -567,7 +567,7 @@ private Set<String> resolveInterestedChangedKeys(ConfigChangeListener configChan
return Collections.unmodifiableSet(interestedChangedKeys);
}

List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
List<ConfigChange> calcPropertyChanges(String appId, String namespace, Properties previous,
Properties current) {
if (previous == null) {
previous = propertiesFactory.getPropertiesInstance();
Expand All @@ -587,12 +587,12 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
List<ConfigChange> changes = Lists.newArrayList();

for (String newKey : newKeys) {
changes.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
changes.add(new ConfigChange(appId, namespace, newKey, null, current.getProperty(newKey),
PropertyChangeType.ADDED));
}

for (String removedKey : removedKeys) {
changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
changes.add(new ConfigChange(appId, namespace, removedKey, previous.getProperty(removedKey), null,
PropertyChangeType.DELETED));
}

Expand All @@ -602,7 +602,7 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
if (Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(new ConfigChange(namespace, commonKey, previousValue, currentValue,
changes.add(new ConfigChange(appId, namespace, commonKey, previousValue, currentValue,
PropertyChangeType.MODIFIED));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
private static final Logger logger = DeferredLoggerFactory.getLogger(AbstractConfigFile.class);
protected static ExecutorService m_executorService;
protected final ConfigRepository m_configRepository;
protected final String m_appId;
protected final String m_namespace;
protected final AtomicReference<Properties> m_configProperties;
private final List<ConfigFileChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
Expand All @@ -59,8 +60,9 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
.create("ConfigFile", true));
}

public AbstractConfigFile(String namespace, ConfigRepository configRepository) {
public AbstractConfigFile(String appId, String namespace, ConfigRepository configRepository) {
m_configRepository = configRepository;
m_appId = appId;
m_namespace = namespace;
m_configProperties = new AtomicReference<>();
propertiesFactory = ApolloInjector.getInstance(PropertiesFactory.class);
Expand All @@ -82,6 +84,11 @@ private void initialize() {
}
}

@Override
public String getAppId() {
return m_appId;
}

@Override
public String getNamespace() {
return m_namespace;
Expand All @@ -91,6 +98,11 @@ public String getNamespace() {

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
this.onRepositoryChange(m_appId, m_namespace, newProperties);
}

@Override
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
if (newProperties.equals(m_configProperties.get())) {
return;
}
Expand All @@ -112,7 +124,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
changeType = PropertyChangeType.DELETED;
}

this.fireConfigChange(new ConfigFileChangeEvent(m_namespace, oldValue, newValue, changeType));
this.fireConfigChange(new ConfigFileChangeEvent(m_appId, m_namespace, oldValue, newValue, changeType));

Tracer.logEvent(APOLLO_CLIENT_CONFIGCHANGES, m_namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public void removeChangeListener(RepositoryChangeListener listener) {
m_listeners.remove(listener);
}

protected void fireRepositoryChange(String namespace, Properties newProperties) {
protected void fireRepositoryChange(String appId, String namespace, Properties newProperties) {
for (RepositoryChangeListener listener : m_listeners) {
try {
listener.onRepositoryChange(namespace, newProperties);
listener.onRepositoryChange(appId, namespace, newProperties);
} catch (Throwable ex) {
Tracer.logError(ex);
logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ public interface ConfigManager {
*/
Config getConfig(String namespace);

/**
* Get the config instance for the namespace and appId specified.
* @param appId the appId
* @param namespace the namespace
* @return the config instance for the appId and namespace
*/
Config getConfig(String appId,String namespace);

/**
* Get the config file instance for the namespace specified.
* @param namespace the namespace
* @param configFileFormat the config file format
* @return the config file instance for the namespace
*/
ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat);

/**
* Get the config file instance for the namespace specified.
* @param appId the appId
* @param namespace the namespace
* @param configFileFormat the config file format
* @return the config file instance for the appId and namespace
*/
ConfigFile getConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package com.ctrip.framework.apollo.internals;

import com.ctrip.framework.apollo.build.ApolloInjector;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.*;
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.enums.ConfigSourceType;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.IOException;
Expand Down Expand Up @@ -47,6 +50,7 @@
public class DefaultConfig extends AbstractConfig implements RepositoryChangeListener {

private static final Logger logger = DeferredLoggerFactory.getLogger(DefaultConfig.class);
private final String m_appId;
private final String m_namespace;
private final Properties m_resourceProperties;
private final AtomicReference<Properties> m_configProperties;
Expand All @@ -62,8 +66,23 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
* @param configRepository the config repository for this config instance
*/
public DefaultConfig(String namespace, ConfigRepository configRepository) {
this(null, namespace , configRepository);
}

/**
* Constructor.
*
* @param appId the appId of this config instance
* @param namespace the namespace of this config instance
* @param configRepository the config repository for this config instance
*/
public DefaultConfig(String appId, String namespace, ConfigRepository configRepository) {
if (appId == null) {
appId = ApolloInjector.getInstance(ConfigUtil.class).getAppId();
}
m_appId = appId;
m_namespace = namespace;
m_resourceProperties = loadFromResource(m_namespace);
m_resourceProperties = loadFromResource(m_appId, m_namespace);
m_configRepository = configRepository;
m_configProperties = new AtomicReference<>();
m_warnLogRateLimiter = RateLimiter.create(0.017); // 1 warning log output per minute
Expand Down Expand Up @@ -219,6 +238,11 @@ private Set<String> stringPropertyNames(Properties properties) {

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
this.onRepositoryChange(m_appId, m_namespace, newProperties);
}

@Override
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
if (newProperties.equals(m_configProperties.get())) {
return;
}
Expand All @@ -235,7 +259,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
return;
}

this.fireConfigChange(m_namespace, actualChanges);
this.fireConfigChange(m_appId, m_namespace, actualChanges);

Tracer.logEvent(APOLLO_CLIENT_CONFIGCHANGES, m_namespace);
}
Expand All @@ -248,7 +272,7 @@ private void updateConfig(Properties newConfigProperties, ConfigSourceType sourc
private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfigProperties,
ConfigSourceType sourceType) {
List<ConfigChange> configChanges =
calcPropertyChanges(m_namespace, m_configProperties.get(), newConfigProperties);
calcPropertyChanges(m_appId, m_namespace, m_configProperties.get(), newConfigProperties);

ImmutableMap.Builder<String, ConfigChange> actualChanges =
new ImmutableMap.Builder<>();
Expand Down Expand Up @@ -299,19 +323,25 @@ private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfi
return actualChanges.build();
}

private Properties loadFromResource(String namespace) {
String name = String.format("META-INF/config/%s.properties", namespace);
private Properties loadFromResource(String appId, String namespace) {
String name = String.format("META-INF/config/%s+%s.properties", appId, namespace);
InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(name);
Properties properties = null;

if (StringUtils.equals(ApolloInjector.getInstance(ConfigUtil.class).getAppId(), appId)
&& in == null) {
name = String.format("META-INF/config/%s.properties", namespace);
in = ClassLoaderUtil.getLoader().getResourceAsStream(name);
}

if (in != null) {
properties = propertiesFactory.getPropertiesInstance();

try {
properties.load(in);
} catch (IOException ex) {
Tracer.logError(ex);
logger.error("Load resource config for namespace {} failed", namespace, ex);
logger.error("Load resource config for appId {} namespace {} failed", appId, namespace, ex);
} finally {
try {
in.close();
Expand Down
Loading

0 comments on commit d4b76f8

Please sign in to comment.