Commit 34d85888 authored by liaozan's avatar liaozan 🏀

Refactor dubbo properties load

parent ecb37261
...@@ -21,4 +21,8 @@ public class OrderedMapPropertySource extends MapPropertySource { ...@@ -21,4 +21,8 @@ public class OrderedMapPropertySource extends MapPropertySource {
getSource().putAll(properties); getSource().putAll(properties);
} }
public void addProperty(String propertyName, String propertyValue) {
getSource().put(propertyName, propertyValue);
}
} }
\ No newline at end of file
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
<properties> <properties>
<jenkins.version>2.397</jenkins.version> <jenkins.version>2.397</jenkins.version>
<maven.install.skip>true</maven.install.skip>
<maven.deploy.skip>true</maven.deploy.skip> <maven.deploy.skip>true</maven.deploy.skip>
<spotbugs.skip>true</spotbugs.skip> <spotbugs.skip>true</spotbugs.skip>
<enforcer.skip>true</enforcer.skip> <enforcer.skip>true</enforcer.skip>
......
...@@ -34,8 +34,11 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg ...@@ -34,8 +34,11 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg
private static Map<String, Object> INIT_PROPERTIES = new LinkedHashMap<>(); private static Map<String, Object> INIT_PROPERTIES = new LinkedHashMap<>();
private final ConfigurablePropertiesLoader configurablePropertiesLoader;
public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) {
super(deferredLogFactory, bootstrapContext); super(deferredLogFactory, bootstrapContext);
this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory);
} }
@Override @Override
...@@ -45,7 +48,7 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg ...@@ -45,7 +48,7 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg
return; return;
} }
setRequiredProperty(environment); setRequiredProperty(environment);
new ConfigurablePropertiesLoader(deferredLogFactory, environment, application).load(); configurablePropertiesLoader.load(environment, application);
} }
@Override @Override
......
...@@ -38,18 +38,12 @@ class ConfigurablePropertiesLoader { ...@@ -38,18 +38,12 @@ class ConfigurablePropertiesLoader {
private final DeferredLogFactory deferredLogFactory; private final DeferredLogFactory deferredLogFactory;
private final ConfigurableEnvironment environment; ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) {
private final SpringApplication application;
ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory, ConfigurableEnvironment environment, SpringApplication application) {
this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class); this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class);
this.deferredLogFactory = deferredLogFactory; this.deferredLogFactory = deferredLogFactory;
this.environment = environment;
this.application = application;
} }
void load() { void load(ConfigurableEnvironment environment, SpringApplication application) {
List<ConfigurableProperties> configurableProperties = loadFactories(ConfigurableProperties.class, getClass().getClassLoader()); List<ConfigurableProperties> configurableProperties = loadFactories(ConfigurableProperties.class, getClass().getClassLoader());
if (CollectionUtils.isEmpty(configurableProperties)) { if (CollectionUtils.isEmpty(configurableProperties)) {
log.warn("There is no configuration properties found"); log.warn("There is no configuration properties found");
...@@ -81,11 +75,12 @@ class ConfigurablePropertiesLoader { ...@@ -81,11 +75,12 @@ class ConfigurablePropertiesLoader {
// resolve any placeHolders // resolve any placeHolders
ConfigUtils.resolvePlaceHolders(environment, propertySource); ConfigUtils.resolvePlaceHolders(environment, propertySource);
// multicast event // multicast event
eventMulticaster.multicastEvent(createEvent(propertySource, properties)); eventMulticaster.multicastEvent(createEvent(environment, application, propertySource, properties));
}); });
} }
private PropertiesPreparedEvent createEvent(OrderedMapPropertySource propertySource, ConfigurableProperties properties) { private PropertiesPreparedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application,
OrderedMapPropertySource propertySource, ConfigurableProperties properties) {
ConfigurableProperties boundProperties = properties.bind(environment); ConfigurableProperties boundProperties = properties.bind(environment);
return new PropertiesPreparedEvent(environment, deferredLogFactory, propertySource, boundProperties, application); return new PropertiesPreparedEvent(environment, deferredLogFactory, propertySource, boundProperties, application);
} }
......
...@@ -23,18 +23,18 @@ public class PropertiesPreparedEvent extends ApplicationEvent { ...@@ -23,18 +23,18 @@ public class PropertiesPreparedEvent extends ApplicationEvent {
private final OrderedMapPropertySource propertySource; private final OrderedMapPropertySource propertySource;
private final SpringApplication application; private final SpringApplication springApplication;
public PropertiesPreparedEvent(ConfigurableEnvironment environment, public PropertiesPreparedEvent(ConfigurableEnvironment environment,
DeferredLogFactory deferredLogFactory, DeferredLogFactory deferredLogFactory,
OrderedMapPropertySource propertySource, OrderedMapPropertySource propertySource,
ConfigurableProperties properties, ConfigurableProperties properties,
SpringApplication application) { SpringApplication springApplication) {
super(properties); super(properties);
this.environment = environment; this.environment = environment;
this.propertySource = propertySource; this.propertySource = propertySource;
this.deferredLogFactory = deferredLogFactory; this.deferredLogFactory = deferredLogFactory;
this.application = application; this.springApplication = springApplication;
} }
public ConfigurableProperties getConfigurableProperties() { public ConfigurableProperties getConfigurableProperties() {
......
package com.schbrain.framework.autoconfigure.dubbo.listener;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.spring.context.event.DubboConfigInitEvent;
import org.apache.dubbo.config.spring.util.DubboBeanUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.PriorityOrdered;
import java.util.Map;
import static org.apache.dubbo.config.spring.util.EnvironmentUtils.filterDubboProperties;
/**
* @author liaozan
* @since 2023-05-08
*/
class DubboConfigInitEventListener implements ApplicationListener<DubboConfigInitEvent>, PriorityOrdered {
private final ConfigurableApplicationContext applicationContext;
DubboConfigInitEventListener(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void onApplicationEvent(DubboConfigInitEvent event) {
if (event.getApplicationContext() == applicationContext) {
ApplicationModel applicationModel = DubboBeanUtils.getApplicationModel(applicationContext);
ConfigManager configManager = applicationModel.getApplicationConfigManager();
configManager.addConfigCenter(buildConfigCenterConfig());
}
}
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
private ConfigCenterConfig buildConfigCenterConfig() {
Map<String, String> externalConfiguration = filterDubboProperties(applicationContext.getEnvironment());
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
configCenterConfig.setExternalConfig(externalConfiguration);
return configCenterConfig;
}
}
\ No newline at end of file
package com.schbrain.framework.autoconfigure.dubbo.listener; package com.schbrain.framework.autoconfigure.dubbo.listener;
import com.alibaba.fastjson2.JSONFactory; import com.alibaba.fastjson2.JSONFactory;
import com.google.common.collect.Maps;
import com.schbrain.common.util.ApplicationName; import com.schbrain.common.util.ApplicationName;
import com.schbrain.common.util.properties.OrderedMapPropertySource;
import com.schbrain.framework.autoconfigure.apollo.event.PropertiesPreparedEvent; import com.schbrain.framework.autoconfigure.apollo.event.PropertiesPreparedEvent;
import com.schbrain.framework.autoconfigure.apollo.event.listener.GenericPropertiesPreparedEventListener; import com.schbrain.framework.autoconfigure.apollo.event.listener.GenericPropertiesPreparedEventListener;
import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties; import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.spring.ConfigCenterBean;
import org.apache.dubbo.config.spring.util.EnvironmentUtils;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Map;
import java.util.SortedMap;
import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES;
/** /**
* @author liaozan * @author liaozan
* @since 2023-04-28 * @since 2023-04-28
*/ */
public class DubboPropertiesPreparedEventListener extends GenericPropertiesPreparedEventListener<DubboProperties> { public class DubboPropertiesPreparedEventListener extends GenericPropertiesPreparedEventListener<DubboProperties> implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name"; public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name";
...@@ -32,26 +28,24 @@ public class DubboPropertiesPreparedEventListener extends GenericPropertiesPrepa ...@@ -32,26 +28,24 @@ public class DubboPropertiesPreparedEventListener extends GenericPropertiesPrepa
} }
@Override @Override
protected void onPropertiesPrepared(PropertiesPreparedEvent event, DubboProperties properties) { public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = event.getEnvironment(); applicationContext.addApplicationListener(new DubboConfigInitEventListener(applicationContext));
Map<String, String> requiredProperties = collectRequiredProperties(environment, event.getApplication());
event.getPropertySource().addProperties(requiredProperties);
injectDubboProperties(environment);
} }
private void injectDubboProperties(ConfigurableEnvironment environment) { @Override
protected void onPropertiesPrepared(PropertiesPreparedEvent event, DubboProperties properties) {
event.getSpringApplication().addInitializers(this);
setRequiredProperties(event.getEnvironment(), event.getSpringApplication(), event.getPropertySource());
JSONFactory.setUseJacksonAnnotation(false); JSONFactory.setUseJacksonAnnotation(false);
SortedMap<String, String> dubboProperties = EnvironmentUtils.filterDubboProperties(environment);
ConfigCenterBean configCenterBean = new ConfigCenterBean();
configCenterBean.setExternalConfig(dubboProperties);
DubboBootstrap.getInstance().configCenter(configCenterBean);
} }
private Map<String, String> collectRequiredProperties(ConfigurableEnvironment environment, SpringApplication application) { private void setRequiredProperties(ConfigurableEnvironment environment, SpringApplication application, OrderedMapPropertySource propertySource) {
Map<String, String> dubboRequiredProperties = Maps.newLinkedHashMapWithExpectedSize(2); if (!propertySource.containsProperty(DUBBO_SCAN_BASE_PACKAGES)) {
dubboRequiredProperties.put(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application)); propertySource.addProperty(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application));
dubboRequiredProperties.put(DUBBO_APPLICATION_NAME, ApplicationName.get(environment)); }
return dubboRequiredProperties; if (!propertySource.containsProperty(DUBBO_APPLICATION_NAME)) {
propertySource.addProperty(DUBBO_APPLICATION_NAME, ApplicationName.get(environment));
}
} }
private String getBasePackage(SpringApplication application) { private String getBasePackage(SpringApplication application) {
......
...@@ -3,7 +3,13 @@ package com.schbrain.framework.autoconfigure.dubbo.properties; ...@@ -3,7 +3,13 @@ package com.schbrain.framework.autoconfigure.dubbo.properties;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.util.LinkedHashSet;
import java.util.Set;
/** /**
* used to fetch dubbo.* config from remote * used to fetch dubbo.* config from remote
...@@ -16,9 +22,64 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -16,9 +22,64 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "dubbo") @ConfigurationProperties(prefix = "dubbo")
public class DubboProperties extends ConfigurableProperties { public class DubboProperties extends ConfigurableProperties {
@NestedConfigurationProperty
private Scan scan = new Scan();
@NestedConfigurationProperty
private ApplicationConfig application = new ApplicationConfig();
@NestedConfigurationProperty
private ModuleConfig module = new ModuleConfig();
@NestedConfigurationProperty
private RegistryConfig registry = new RegistryConfig();
@NestedConfigurationProperty
private ProtocolConfig protocol = new ProtocolConfig();
@NestedConfigurationProperty
private MonitorConfig monitor = new MonitorConfig();
@NestedConfigurationProperty
private ProviderConfig provider = new ProviderConfig();
@NestedConfigurationProperty
private ConsumerConfig consumer = new ConsumerConfig();
@NestedConfigurationProperty
private ConfigCenterConfig configCenter = new ConfigCenterConfig();
@NestedConfigurationProperty
private MetadataReportConfig metadataReport = new MetadataReportConfig();
@NestedConfigurationProperty
private MetricsConfig metrics = new MetricsConfig();
@NestedConfigurationProperty
private TracingConfig tracing = new TracingConfig();
@Override @Override
public String getDefaultNamespace() { public String getDefaultNamespace() {
return "dubbo-common"; return "dubbo-common";
} }
static class Scan {
/**
* The basePackages to scan , the multiple-value is delimited by comma
*
* @see EnableDubbo#scanBasePackages()
*/
private Set<String> basePackages = new LinkedHashSet<>();
public Set<String> getBasePackages() {
return basePackages;
}
public void setBasePackages(Set<String> basePackages) {
this.basePackages = basePackages;
}
}
} }
\ No newline at end of file
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