Commit 792f2539 authored by liaozan's avatar liaozan 🏀

Replace CompositePropertySource with single OrderedMapPropertySource

parent 6db1a712
...@@ -15,7 +15,7 @@ import org.springframework.boot.logging.DeferredLogFactory; ...@@ -15,7 +15,7 @@ import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster; import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
...@@ -30,18 +30,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact ...@@ -30,18 +30,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact
*/ */
class ConfigurablePropertiesLoader { class ConfigurablePropertiesLoader {
/**
* the name of properties propertySource
*/
public static final String PROPERTIES_PROPERTY_SOURCE = "ConfigurablePropertiesPropertySource";
private final Log log; private final Log log;
private final DeferredLogFactory deferredLogFactory; private final DeferredLogFactory deferredLogFactory;
ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) { ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) {
this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class);
this.deferredLogFactory = deferredLogFactory; this.deferredLogFactory = deferredLogFactory;
this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class);
} }
void load(ConfigurableEnvironment environment, SpringApplication application) { void load(ConfigurableEnvironment environment, SpringApplication application) {
...@@ -52,32 +47,34 @@ class ConfigurablePropertiesLoader { ...@@ -52,32 +47,34 @@ class ConfigurablePropertiesLoader {
} }
ApplicationEventMulticaster eventMulticaster = createEventMulticaster(application.getListeners()); ApplicationEventMulticaster eventMulticaster = createEventMulticaster(application.getListeners());
boolean remoteFirst = ApolloProperties.get(environment).isRemoteFirst();
ApolloProperties apolloProperties = ApolloProperties.get(environment); configurableProperties.parallelStream().forEach(properties -> {
OrderedMapPropertySource propertySource = loadFromRemote(environment, remoteFirst, properties.getNamespace());
// MUST NOT use CachedCompositePropertySource, because propertyNames will be cached when property loading if (propertySource == null) {
CompositePropertySource compositePropertySource = new CompositePropertySource(PROPERTIES_PROPERTY_SOURCE); return;
if (apolloProperties.isRemoteFirst()) { }
environment.getPropertySources().addFirst(compositePropertySource); // multicast event
} else { ConfigLoadedEvent event = createEvent(environment, application, propertySource, properties);
environment.getPropertySources().addLast(compositePropertySource); eventMulticaster.multicastEvent(event, ResolvableType.forClass(event.getClass()));
});
} }
configurableProperties.forEach(properties -> { private OrderedMapPropertySource loadFromRemote(ConfigurableEnvironment environment, boolean remoteFirst, String namespace) {
String namespace = properties.getNamespace();
Config config = ConfigService.getConfig(namespace); Config config = ConfigService.getConfig(namespace);
OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config);
if (propertySource == null) { if (propertySource == null) {
log.warn("No configuration properties loaded under namespace: " + namespace); log.warn("No configuration properties loaded under namespace: " + namespace);
return; return null;
}
if (remoteFirst) {
environment.getPropertySources().addFirst(propertySource);
} else {
environment.getPropertySources().addLast(propertySource);
} }
// early add to environment to support properties bind
compositePropertySource.addPropertySource(propertySource);
// resolve any placeHolders // resolve any placeHolders
ConfigUtils.resolvePlaceHolders(environment, propertySource); ConfigUtils.resolvePlaceHolders(environment, propertySource);
// multicast event return propertySource;
eventMulticaster.multicastEvent(createEvent(environment, application, propertySource, properties));
});
} }
private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application, private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment