Skip to content

Commit d4b76f8

Browse files
add multiple appId,pull other appId config into config. (#70)
* 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]>
1 parent 67eb866 commit d4b76f8

File tree

81 files changed

+1284
-548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1284
-548
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Apollo Java 2.4.0
1010
* [Feature openapi query namespace support not fill item](https://github.com/apolloconfig/apollo-java/pull/83)
1111
* [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74)
1212
* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)
13+
* [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70)
1314

1415

1516
------------------

apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/internals/PureApolloConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public PureApolloConfig(String namespace,
3939
super(namespace, configRepository);
4040
}
4141

42+
/**
43+
* Constructor.
44+
*
45+
* @param appId the appId of this config instance
46+
* @param namespace the namespace of this config instance
47+
* @param configRepository the config repository for this config instance
48+
*/
49+
public PureApolloConfig(String appId, String namespace,
50+
ConfigRepository configRepository) {
51+
super(appId, namespace, configRepository);
52+
}
53+
4254
@Override
4355
public String getProperty(String key, String defaultValue) {
4456
// step 1: check local cached properties file

apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/internals/PureApolloConfigFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class PureApolloConfigFactory extends DefaultConfigFactory implements ConfigFactory {
2828

2929
@Override
30-
protected Config createRepositoryConfig(String namespace, ConfigRepository configRepository) {
31-
return new PureApolloConfig(namespace, configRepository);
30+
protected Config createRepositoryConfig(String appId, String namespace, ConfigRepository configRepository) {
31+
return new PureApolloConfig(appId, namespace, configRepository);
3232
}
3333
}

apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFile.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public interface ConfigFile {
3535
*/
3636
boolean hasContent();
3737

38+
/**
39+
* Get the appId of this config file instance
40+
* @return the appId
41+
*
42+
* @since 2.4.0
43+
*/
44+
default String getAppId(){
45+
return null;
46+
}
47+
3848
/**
3949
* Get the namespace of this config file instance
4050
* @return the namespace

apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import com.ctrip.framework.apollo.build.ApolloInjector;
2020
import com.ctrip.framework.apollo.core.ConfigConsts;
2121
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
22+
import com.ctrip.framework.apollo.core.utils.StringUtils;
2223
import com.ctrip.framework.apollo.internals.ConfigManager;
2324
import com.ctrip.framework.apollo.internals.ConfigMonitorInitializer;
2425
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
2526
import com.ctrip.framework.apollo.spi.ConfigFactory;
2627
import com.ctrip.framework.apollo.spi.ConfigRegistry;
28+
import com.ctrip.framework.apollo.util.ConfigUtil;
2729

2830
/**
2931
* Entry point for client config use
@@ -91,6 +93,10 @@ public static Config getConfig(String namespace) {
9193
return s_instance.getManager().getConfig(namespace);
9294
}
9395

96+
public static Config getConfig(String appId, String namespace) {
97+
return s_instance.getManager().getConfig(appId, namespace);
98+
}
99+
94100
public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
95101
return s_instance.getManager().getConfigFile(namespace, configFileFormat);
96102
}
@@ -111,16 +117,32 @@ static void setConfig(Config config) {
111117
*/
112118
static void setConfig(String namespace, final Config config) {
113119
s_instance.getRegistry().register(namespace, new ConfigFactory() {
120+
121+
private final ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
122+
114123
@Override
115124
public Config create(String namespace) {
116125
return config;
117126
}
118127

128+
@Override
129+
public Config create(String appId, String namespace) {
130+
if(!StringUtils.equals(appId, m_configUtil.getAppId())){
131+
throw new IllegalArgumentException("Provided appId '" + appId + "' does not match the default appId '" + m_configUtil.getAppId() + "'");
132+
}
133+
return config;
134+
}
135+
119136
@Override
120137
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
121138
return null;
122139
}
123140

141+
@Override
142+
public ConfigFile createConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat) {
143+
return null;
144+
}
145+
124146
});
125147
}
126148

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,15 @@ protected void clearConfigCache() {
454454
/**
455455
* @param changes map's key is config property's key
456456
*/
457-
protected void fireConfigChange(String namespace, Map<String, ConfigChange> changes) {
457+
protected void fireConfigChange(String appId, String namespace, Map<String, ConfigChange> changes) {
458458
final Set<String> changedKeys = changes.keySet();
459459
final List<ConfigChangeListener> listeners = this.findMatchedConfigChangeListeners(changedKeys);
460460

461461
// notify those listeners
462462
for (ConfigChangeListener listener : listeners) {
463463
Set<String> interestedChangedKeys = resolveInterestedChangedKeys(listener, changedKeys);
464464
InterestedConfigChangeEvent interestedConfigChangeEvent = new InterestedConfigChangeEvent(
465-
namespace, changes, interestedChangedKeys);
465+
appId, namespace, changes, interestedChangedKeys);
466466
this.notifyAsync(listener, interestedConfigChangeEvent);
467467
}
468468
}
@@ -567,7 +567,7 @@ private Set<String> resolveInterestedChangedKeys(ConfigChangeListener configChan
567567
return Collections.unmodifiableSet(interestedChangedKeys);
568568
}
569569

570-
List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
570+
List<ConfigChange> calcPropertyChanges(String appId, String namespace, Properties previous,
571571
Properties current) {
572572
if (previous == null) {
573573
previous = propertiesFactory.getPropertiesInstance();
@@ -587,12 +587,12 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
587587
List<ConfigChange> changes = Lists.newArrayList();
588588

589589
for (String newKey : newKeys) {
590-
changes.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
590+
changes.add(new ConfigChange(appId, namespace, newKey, null, current.getProperty(newKey),
591591
PropertyChangeType.ADDED));
592592
}
593593

594594
for (String removedKey : removedKeys) {
595-
changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
595+
changes.add(new ConfigChange(appId, namespace, removedKey, previous.getProperty(removedKey), null,
596596
PropertyChangeType.DELETED));
597597
}
598598

@@ -602,7 +602,7 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
602602
if (Objects.equal(previousValue, currentValue)) {
603603
continue;
604604
}
605-
changes.add(new ConfigChange(namespace, commonKey, previousValue, currentValue,
605+
changes.add(new ConfigChange(appId, namespace, commonKey, previousValue, currentValue,
606606
PropertyChangeType.MODIFIED));
607607
}
608608

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
4747
private static final Logger logger = DeferredLoggerFactory.getLogger(AbstractConfigFile.class);
4848
protected static ExecutorService m_executorService;
4949
protected final ConfigRepository m_configRepository;
50+
protected final String m_appId;
5051
protected final String m_namespace;
5152
protected final AtomicReference<Properties> m_configProperties;
5253
private final List<ConfigFileChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
@@ -59,8 +60,9 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
5960
.create("ConfigFile", true));
6061
}
6162

62-
public AbstractConfigFile(String namespace, ConfigRepository configRepository) {
63+
public AbstractConfigFile(String appId, String namespace, ConfigRepository configRepository) {
6364
m_configRepository = configRepository;
65+
m_appId = appId;
6466
m_namespace = namespace;
6567
m_configProperties = new AtomicReference<>();
6668
propertiesFactory = ApolloInjector.getInstance(PropertiesFactory.class);
@@ -82,6 +84,11 @@ private void initialize() {
8284
}
8385
}
8486

87+
@Override
88+
public String getAppId() {
89+
return m_appId;
90+
}
91+
8592
@Override
8693
public String getNamespace() {
8794
return m_namespace;
@@ -91,6 +98,11 @@ public String getNamespace() {
9198

9299
@Override
93100
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
101+
this.onRepositoryChange(m_appId, m_namespace, newProperties);
102+
}
103+
104+
@Override
105+
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
94106
if (newProperties.equals(m_configProperties.get())) {
95107
return;
96108
}
@@ -112,7 +124,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
112124
changeType = PropertyChangeType.DELETED;
113125
}
114126

115-
this.fireConfigChange(new ConfigFileChangeEvent(m_namespace, oldValue, newValue, changeType));
127+
this.fireConfigChange(new ConfigFileChangeEvent(m_appId, m_namespace, oldValue, newValue, changeType));
116128

117129
Tracer.logEvent(APOLLO_CLIENT_CONFIGCHANGES, m_namespace);
118130
}

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public void removeChangeListener(RepositoryChangeListener listener) {
6464
m_listeners.remove(listener);
6565
}
6666

67-
protected void fireRepositoryChange(String namespace, Properties newProperties) {
67+
protected void fireRepositoryChange(String appId, String namespace, Properties newProperties) {
6868
for (RepositoryChangeListener listener : m_listeners) {
6969
try {
70-
listener.onRepositoryChange(namespace, newProperties);
70+
listener.onRepositoryChange(appId, namespace, newProperties);
7171
} catch (Throwable ex) {
7272
Tracer.logError(ex);
7373
logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex);

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,28 @@ public interface ConfigManager {
3131
*/
3232
Config getConfig(String namespace);
3333

34+
/**
35+
* Get the config instance for the namespace and appId specified.
36+
* @param appId the appId
37+
* @param namespace the namespace
38+
* @return the config instance for the appId and namespace
39+
*/
40+
Config getConfig(String appId,String namespace);
41+
3442
/**
3543
* Get the config file instance for the namespace specified.
3644
* @param namespace the namespace
3745
* @param configFileFormat the config file format
3846
* @return the config file instance for the namespace
3947
*/
4048
ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat);
49+
50+
/**
51+
* Get the config file instance for the namespace specified.
52+
* @param appId the appId
53+
* @param namespace the namespace
54+
* @param configFileFormat the config file format
55+
* @return the config file instance for the appId and namespace
56+
*/
57+
ConfigFile getConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat);
4158
}

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
*/
1717
package com.ctrip.framework.apollo.internals;
1818

19+
import com.ctrip.framework.apollo.build.ApolloInjector;
1920
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.*;
2021
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
22+
import com.ctrip.framework.apollo.core.utils.StringUtils;
2123
import com.ctrip.framework.apollo.enums.ConfigSourceType;
24+
import com.ctrip.framework.apollo.util.ConfigUtil;
2225
import com.google.common.collect.Maps;
2326
import com.google.common.collect.Sets;
2427
import java.io.IOException;
@@ -47,6 +50,7 @@
4750
public class DefaultConfig extends AbstractConfig implements RepositoryChangeListener {
4851

4952
private static final Logger logger = DeferredLoggerFactory.getLogger(DefaultConfig.class);
53+
private final String m_appId;
5054
private final String m_namespace;
5155
private final Properties m_resourceProperties;
5256
private final AtomicReference<Properties> m_configProperties;
@@ -62,8 +66,23 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
6266
* @param configRepository the config repository for this config instance
6367
*/
6468
public DefaultConfig(String namespace, ConfigRepository configRepository) {
69+
this(null, namespace , configRepository);
70+
}
71+
72+
/**
73+
* Constructor.
74+
*
75+
* @param appId the appId of this config instance
76+
* @param namespace the namespace of this config instance
77+
* @param configRepository the config repository for this config instance
78+
*/
79+
public DefaultConfig(String appId, String namespace, ConfigRepository configRepository) {
80+
if (appId == null) {
81+
appId = ApolloInjector.getInstance(ConfigUtil.class).getAppId();
82+
}
83+
m_appId = appId;
6584
m_namespace = namespace;
66-
m_resourceProperties = loadFromResource(m_namespace);
85+
m_resourceProperties = loadFromResource(m_appId, m_namespace);
6786
m_configRepository = configRepository;
6887
m_configProperties = new AtomicReference<>();
6988
m_warnLogRateLimiter = RateLimiter.create(0.017); // 1 warning log output per minute
@@ -219,6 +238,11 @@ private Set<String> stringPropertyNames(Properties properties) {
219238

220239
@Override
221240
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
241+
this.onRepositoryChange(m_appId, m_namespace, newProperties);
242+
}
243+
244+
@Override
245+
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
222246
if (newProperties.equals(m_configProperties.get())) {
223247
return;
224248
}
@@ -235,7 +259,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
235259
return;
236260
}
237261

238-
this.fireConfigChange(m_namespace, actualChanges);
262+
this.fireConfigChange(m_appId, m_namespace, actualChanges);
239263

240264
Tracer.logEvent(APOLLO_CLIENT_CONFIGCHANGES, m_namespace);
241265
}
@@ -248,7 +272,7 @@ private void updateConfig(Properties newConfigProperties, ConfigSourceType sourc
248272
private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfigProperties,
249273
ConfigSourceType sourceType) {
250274
List<ConfigChange> configChanges =
251-
calcPropertyChanges(m_namespace, m_configProperties.get(), newConfigProperties);
275+
calcPropertyChanges(m_appId, m_namespace, m_configProperties.get(), newConfigProperties);
252276

253277
ImmutableMap.Builder<String, ConfigChange> actualChanges =
254278
new ImmutableMap.Builder<>();
@@ -299,19 +323,25 @@ private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfi
299323
return actualChanges.build();
300324
}
301325

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

331+
if (StringUtils.equals(ApolloInjector.getInstance(ConfigUtil.class).getAppId(), appId)
332+
&& in == null) {
333+
name = String.format("META-INF/config/%s.properties", namespace);
334+
in = ClassLoaderUtil.getLoader().getResourceAsStream(name);
335+
}
336+
307337
if (in != null) {
308338
properties = propertiesFactory.getPropertiesInstance();
309339

310340
try {
311341
properties.load(in);
312342
} catch (IOException ex) {
313343
Tracer.logError(ex);
314-
logger.error("Load resource config for namespace {} failed", namespace, ex);
344+
logger.error("Load resource config for appId {} namespace {} failed", appId, namespace, ex);
315345
} finally {
316346
try {
317347
in.close();

0 commit comments

Comments
 (0)