This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 3e24ad182d9c76b8ac6d5f7faba637acdd0504c4 Author: liubao <[email protected]> AuthorDate: Wed Sep 27 11:26:01 2023 +0800 [SCB-2008]remove archaius dependency: nacos adapter --- .../rest/filter/inner/RestServerCodecFilter.java | 2 +- .../core/ConfigurationSpringInitializer.java | 192 ++------------------- .../core/ServiceCombCoreConfiguration.java | 8 +- .../core/TestConfigurationSpringInitializer.java | 92 +--------- .../nacos/{client => }/ConfigurationAction.java | 2 +- .../config/nacos/{client => }/NacosClient.java | 38 ++-- .../config/nacos/{client => }/NacosConfig.java | 27 ++- .../config/nacos/NacosConfigConfiguration.java} | 26 ++- ...Impl.java => NacosDynamicPropertiesSource.java} | 100 +++-------- ...rk.boot.autoconfigure.AutoConfiguration.imports | 18 ++ .../config/nacos/{client => }/NacosClientTest.java | 13 +- .../sources/NacosConfigurationSourceImplTest.java | 74 -------- .../ConfigurationSpringBootInitializer.java | 2 +- 13 files changed, 116 insertions(+), 478 deletions(-) diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java index fd6344676..614e4789f 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java @@ -116,7 +116,7 @@ public class RestServerCodecFilter implements ProviderFilter { public static CompletableFuture<Response> encodeResponse(Response response, boolean download, ProduceProcessor produceProcessor, HttpServletResponseEx responseEx) { responseEx.setStatus(response.getStatusCode()); - copyHeadersToHttpResponse(response.getHeaders(), responseEx); + copyHeadersToHttpResponse(response.getHeaders(), responseEx); if (download) { return CompletableFuture.completedFuture(response); diff --git a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java index b1fe6adc3..dc602b47d 100644 --- a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java +++ b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java @@ -19,24 +19,16 @@ package org.apache.servicecomb.core; import java.io.IOException; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -import org.apache.commons.configuration.AbstractConfiguration; -import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.config.ConfigMapping; -import org.apache.servicecomb.config.ConfigUtil; +import org.apache.servicecomb.config.DynamicPropertiesSource; import org.apache.servicecomb.config.YAMLUtil; import org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader; -import org.apache.servicecomb.config.event.DynamicConfigurationChangedEvent; -import org.apache.servicecomb.config.event.RefreshGovernanceConfigurationEvent; -import org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource; import org.apache.servicecomb.foundation.bootstrap.BootStrapService; -import org.apache.servicecomb.foundation.common.event.EventManager; import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,38 +36,18 @@ import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Conditional; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.Ordered; -import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; -import org.springframework.core.env.PropertySource; - -import com.google.common.eventbus.Subscribe; -import com.netflix.config.ConfigurationManager; -import com.netflix.config.WatchedUpdateResult; /** - * Adapt spring PropertySource and Archaius Configuration - * spring vs archaius - * (add) | dynamic(configcenter) - * system property | system property - * environment | environment - * *properties/*.yml | (add) - * (add) | microservice.yaml - * - * add dynamic configuration, microserive.yaml to spring, add *properties/*.yml to archaius - * - * NOTICE: we are not duplicate spring system property and environment property source, this will cause some problem - * related to precedence of a KEY-VAlUE. That is cse.test in dynamic config may not override servicecomb.test in yml. - * Users need to use the same key as what is in config file to override. + * Add dynamic configuration, microserive.yaml to spring */ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderConfigurer implements EnvironmentAware { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationSpringInitializer.class); - public static final String EXTRA_CONFIG_SOURCE_PREFIX = "extraConfig-"; - public static final String MICROSERVICE_PROPERTY_SOURCE_NAME = "microservice.yaml"; public static final String MAPPING_PROPERTY_SOURCE_NAME = "mapping.yaml"; @@ -92,13 +64,12 @@ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderCo private final List<BootStrapService> bootStrapServices = SPIServiceUtils.getSortedService(BootStrapService.class); - private final Map<String, Object> dynamicData = new ConcurrentHashMap<>(); + private final List<DynamicPropertiesSource<?>> dynamicPropertiesSources; - private ConfigCenterConfigurationSource configCenterConfigurationSource; - - public ConfigurationSpringInitializer() { + public ConfigurationSpringInitializer(List<DynamicPropertiesSource<?>> dynamicPropertiesSources) { setOrder(Ordered.LOWEST_PRECEDENCE / 2); setIgnoreUnresolvablePlaceholders(true); + this.dynamicPropertiesSources = dynamicPropertiesSources; } /** @@ -112,49 +83,14 @@ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderCo return; } - syncFromSpring(environment); - syncToSpring(environment); + addMicroserviceDefinitions(environment); startupBootStrapService(environment); - // watch configuration changes - EventManager.register(this); - configCenterConfigurationSource = ConfigUtil.installDynamicConfig(environment); - addDynamicConfigurationToSpring(environment, configCenterConfigurationSource); - } - - @Subscribe - public void onConfigurationDataChanged(DynamicConfigurationChangedEvent event) { - try { - WatchedUpdateResult data = event.getEvent(); - if (data.getDeleted() != null) { - data.getDeleted().forEach((k, v) -> dynamicData.remove(k)); - } - if (data.getAdded() != null) { - dynamicData.putAll(data.getAdded()); - } - if (data.getChanged() != null) { - dynamicData.putAll(data.getChanged()); - } - } catch (Exception e) { - LOGGER.error("", e); - } - EventManager.post(new RefreshGovernanceConfigurationEvent(event.getEvent())); - } - - private void syncFromSpring(Environment environment) { - String environmentName = generateNameForEnvironment(environment); - LOGGER.info("Environment received, will get configurations from [{}].", environmentName); - - Map<String, Object> extraConfig = getAllProperties(environment); - ConfigUtil.addExtraConfig(EXTRA_CONFIG_SOURCE_PREFIX + environmentName, extraConfig); + addDynamicConfigurationToSpring(environment); } - public static void syncToSpring(Environment environment) { - if (isExternalInit()) { - return; - } - + public static void addMicroserviceDefinitions(Environment environment) { addMicroserviceYAMLToSpring(environment); addMappingToSpring(environment); } @@ -229,17 +165,15 @@ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderCo propertySources.addFirst(new MapPropertySource(MAPPING_PROPERTY_SOURCE_NAME, mappings)); } - private void addDynamicConfigurationToSpring(Environment environment, - ConfigCenterConfigurationSource configCenterConfigurationSource) { + private void addDynamicConfigurationToSpring(Environment environment) { if (!(environment instanceof ConfigurableEnvironment)) { return; } - ConfigurableEnvironment ce = (ConfigurableEnvironment) environment; - if (configCenterConfigurationSource == null) { - return; - } try { - ce.getPropertySources().addFirst(new MapPropertySource("dynamic-source", dynamicData)); + for (DynamicPropertiesSource<?> dynamicPropertiesSource : dynamicPropertiesSources) { + ((ConfigurableEnvironment) environment).getPropertySources() + .addFirst(dynamicPropertiesSource.create(environment)); + } } catch (Exception e) { if (environment.getProperty(CoreConst.PRINT_SENSITIVE_ERROR_MESSAGE, boolean.class, false)) { @@ -252,104 +186,6 @@ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderCo @Override protected Properties mergeProperties() throws IOException { - Properties properties = super.mergeProperties(); - if (isExternalInit()) { - return properties; - } - - AbstractConfiguration config = ConfigurationManager.getConfigInstance(); - Iterator<String> iterator = config.getKeys(); - while (iterator.hasNext()) { - String key = iterator.next(); - Object value = config.getProperty(key); - properties.put(key, value); - } - return properties; - } - - /** - * Try to get a name for identifying the environment. - * @param environment the target that the name is generated for. - * @return The generated name for the environment. - */ - private String generateNameForEnvironment(Environment environment) { - String environmentName = environment.getProperty("spring.config.name"); - if (!StringUtils.isEmpty(environmentName)) { - return environmentName; - } - - environmentName = environment.getProperty("spring.application.name"); - if (!StringUtils.isEmpty(environmentName)) { - return environmentName; - } - - return environment.getClass().getName() + "@" + environment.hashCode(); - } - - /** - * Traversal all {@link PropertySource} of {@link ConfigurableEnvironment}, and try to get all properties. - */ - private Map<String, Object> getAllProperties(Environment environment) { - Map<String, Object> configFromSpringBoot = new HashMap<>(); - - if (!(environment instanceof ConfigurableEnvironment)) { - return configFromSpringBoot; - } - - ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; - - if (ignoreResolveFailure()) { - configurableEnvironment.setIgnoreUnresolvableNestedPlaceholders(true); - } - - for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) { - if (MICROSERVICE_PROPERTY_SOURCE_NAME.equals(propertySource.getName()) - || MAPPING_PROPERTY_SOURCE_NAME.equals(propertySource.getName())) { - continue; - } - getProperties(configurableEnvironment, propertySource, configFromSpringBoot); - } - return configFromSpringBoot; - } - - /** - * Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)} - */ - private void getProperties(ConfigurableEnvironment environment, PropertySource<?> propertySource, - Map<String, Object> configFromSpringBoot) { - if (propertySource instanceof CompositePropertySource) { - // recursively get EnumerablePropertySource - CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource; - compositePropertySource.getPropertySources().forEach(ps -> getProperties(environment, ps, configFromSpringBoot)); - return; - } - if (propertySource instanceof EnumerablePropertySource) { - EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource; - for (String propertyName : enumerablePropertySource.getPropertyNames()) { - try { - Object propertyValue = environment.getProperty(propertyName, Object.class); - if (propertyValue == null) { - LOGGER.error( - "The value of a configuration item is null, please check whether there is any impact, config item key: {}", - propertyName); - continue; - } - configFromSpringBoot.put(propertyName, propertyValue); - } catch (Exception e) { - throw new RuntimeException( - "set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.", - e); - } - } - return; - } - - LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]", propertySource.getName()); - } - - private boolean ignoreResolveFailure() { - return ConfigUtil - .createLocalConfig() - .getBoolean("servicecomb.config.ignoreResolveFailure", false); + return super.mergeProperties(); } } diff --git a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java index 55338ea25..74b8f075f 100644 --- a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java +++ b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java @@ -16,6 +16,9 @@ */ package org.apache.servicecomb.core; +import java.util.List; + +import org.apache.servicecomb.config.DynamicPropertiesSource; import org.apache.servicecomb.config.MicroserviceProperties; import org.apache.servicecomb.core.bootup.FilterChainCollector; import org.apache.servicecomb.core.bootup.ServiceInformationCollector; @@ -33,8 +36,9 @@ import org.springframework.core.env.Environment; @SuppressWarnings("unused") public class ServiceCombCoreConfiguration { @Bean - public ConfigurationSpringInitializer configurationSpringInitializer() { - return new ConfigurationSpringInitializer(); + public ConfigurationSpringInitializer configurationSpringInitializer( + List<DynamicPropertiesSource<?>> dynamicPropertiesSources) { + return new ConfigurationSpringInitializer(dynamicPropertiesSources); } @Bean diff --git a/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java b/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java index e663bf21f..e6c448657 100644 --- a/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java +++ b/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java @@ -16,9 +16,9 @@ */ package org.apache.servicecomb.core; -import com.netflix.config.ConfigurationManager; -import mockit.Deencapsulation; -import org.apache.commons.configuration.Configuration; +import java.util.HashMap; +import java.util.Map; + import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; import org.apache.servicecomb.config.ConfigUtil; @@ -34,16 +34,8 @@ import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.StandardEnvironment; -import org.springframework.core.env.SystemEnvironmentPropertySource; import org.springframework.jndi.JndiPropertySource; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - public class TestConfigurationSpringInitializer { @BeforeEach public void beforeTest() { @@ -61,23 +53,6 @@ public class TestConfigurationSpringInitializer { ArchaiusUtils.resetConfig(); } - @Test - public void testAll() { - ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer(); - ConfigUtil.installDynamicConfig(); - - Object o = ConfigUtil.getProperty("zq"); - @SuppressWarnings("unchecked") - List<Map<String, Object>> listO = (List<Map<String, Object>>) o; - Assertions.assertEquals(3, listO.size()); - Assertions.assertNull(ConfigUtil.getProperty("notExist")); - - Configuration instance = ConfigurationManager.getConfigInstance(); - ConfigUtil.installDynamicConfig(); - // must not reinstall - Assertions.assertEquals(instance, ConfigurationManager.getConfigInstance()); - } - @Test public void testSetEnvironment() { ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); @@ -146,18 +121,6 @@ public class TestConfigurationSpringInitializer { } return value; }).when(environment).getProperty(ArgumentMatchers.anyString(), ArgumentMatchers.eq(Object.class)); - - new ConfigurationSpringInitializer().setEnvironment(environment); - - Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil(); - Assertions.assertFalse(extraLocalConfig.isEmpty()); - Map<String, Object> extraProperties = extraLocalConfig - .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName() + "@" - + environment.hashCode()); - Assertions.assertNotNull(extraLocalConfig); - for (Entry<String, String> entry : propertyMap.entrySet()) { - Assertions.assertEquals(entry.getValue(), extraProperties.get(entry.getKey())); - } } @Test @@ -191,54 +154,5 @@ public class TestConfigurationSpringInitializer { propertySources2.addFirst(new MapPropertySource("mapPropertySource2", map2)); Mockito.when(environment2.getProperty("key2", Object.class)).thenReturn("value2"); Mockito.when(environment2.getProperty("key2")).thenReturn("value2"); - - ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer(); - configurationSpringInitializer.setEnvironment(environment0); - configurationSpringInitializer.setEnvironment(environment1); - configurationSpringInitializer.setEnvironment(environment2); - - Map<String, Map<String, Object>> extraConfig = getExtraConfigMapFromConfigUtil(); - Assertions.assertEquals(3, extraConfig.size()); - - Map<String, Object> extraProperties = extraConfig - .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "application"); - Assertions.assertEquals(1, extraProperties.size()); - Assertions.assertEquals("application", extraProperties.get("spring.config.name")); - - extraProperties = extraConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "bootstrap"); - Assertions.assertEquals(1, extraProperties.size()); - Assertions.assertEquals("bootstrap", extraProperties.get("spring.application.name")); - - extraProperties = extraConfig.get( - ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment2.getClass().getName() + "@" - + environment2.hashCode()); - Assertions.assertEquals(1, extraProperties.size()); - Assertions.assertEquals("value2", extraProperties.get("key2")); - } - - @Test - public void should_throw_exception_when_given_ignoreResolveFailure_false() { - Assertions.assertThrows(RuntimeException.class, () -> { - StandardEnvironment environment = newStandardEnvironment(); - - ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer(); - configurationSpringInitializer.setEnvironment(environment); - }); - } - - private Map<String, Map<String, Object>> getExtraConfigMapFromConfigUtil() { - return Deencapsulation - .getField(ConfigUtil.class, "EXTRA_CONFIG_MAP"); - } - - private StandardEnvironment newStandardEnvironment() { - Map<String, Object> envProperties = new HashMap<>(); - envProperties.put("IFS-X", "${IFS-X}"); - PropertySource<Map<String, Object>> systemEnvironmentPropertySource = new SystemEnvironmentPropertySource("system-env", envProperties); - - StandardEnvironment environment = new StandardEnvironment(); - environment.getPropertySources() - .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, systemEnvironmentPropertySource); - return environment; } } diff --git a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/ConfigurationAction.java b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java similarity index 94% rename from dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/ConfigurationAction.java rename to dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java index 3defe34f0..a4dc8f53c 100644 --- a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/ConfigurationAction.java +++ b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.servicecomb.config.nacos.client; +package org.apache.servicecomb.config.nacos; public enum ConfigurationAction { CREATE, diff --git a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosClient.java b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java similarity index 77% rename from dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosClient.java rename to dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java index 6e02f492c..7d79c52b7 100644 --- a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosClient.java +++ b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.servicecomb.config.nacos.client; +package org.apache.servicecomb.config.nacos; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.CREATE; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.DELETE; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.SET; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.CREATE; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.DELETE; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.SET; import java.util.HashMap; import java.util.Map; @@ -28,10 +28,11 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; import org.apache.commons.lang3.StringUtils; -import org.apache.servicecomb.config.nacos.archaius.sources.NacosConfigurationSourceImpl.UpdateHandler; +import org.apache.servicecomb.config.nacos.NacosDynamicPropertiesSource.UpdateHandler; import org.apache.servicecomb.config.parser.Parser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; @@ -45,19 +46,26 @@ public class NacosClient { private final UpdateHandler updateHandler; + private NacosConfig nacosConfig; + public NacosClient(UpdateHandler updateHandler) { this.updateHandler = updateHandler; } + @Autowired + public void setNacosConfig(NacosConfig nacosConfig) { + this.nacosConfig = nacosConfig; + } + public void refreshNacosConfig() { new ConfigRefresh().refreshConfig(); } class ConfigRefresh { - Parser contentParser = Parser.findParser(NacosConfig.INSTANCE.getContentType()); + Parser contentParser = Parser.findParser(nacosConfig.getContentType()); - String keyPrefix = NacosConfig.INSTANCE.getGroup() + "." + - NacosConfig.INSTANCE.getDataId(); + String keyPrefix = nacosConfig.getGroup() + "." + + nacosConfig.getDataId(); ConfigRefresh() { } @@ -65,16 +73,16 @@ public class NacosClient { @SuppressWarnings("unchecked") void refreshConfig() { Properties properties = new Properties(); - properties.put("serverAddr", NacosConfig.INSTANCE.getServerAddr()); - properties.put("namespace", NacosConfig.INSTANCE.getNameSpace()); + properties.put("serverAddr", nacosConfig.getServerAddr()); + properties.put("namespace", nacosConfig.getNameSpace()); try { ConfigService configService = NacosFactory.createConfigService(properties); - String content = configService.getConfig(NacosConfig.INSTANCE.getDataId(), - NacosConfig.INSTANCE.getGroup(), 5000); + String content = configService.getConfig(nacosConfig.getDataId(), + nacosConfig.getGroup(), 5000); processContent(content); - configService.addListener(NacosConfig.INSTANCE.getDataId(), - NacosConfig.INSTANCE.getGroup(), new Listener() { + configService.addListener(nacosConfig.getDataId(), + nacosConfig.getGroup(), new Listener() { @Override public void receiveConfigInfo(String configInfo) { LOGGER.info("receive from nacos:" + configInfo); @@ -96,7 +104,7 @@ public class NacosClient { return; } - refreshConfigItems(contentParser.parse(content, keyPrefix, NacosConfig.INSTANCE.getAddPrefix())); + refreshConfigItems(contentParser.parse(content, keyPrefix, nacosConfig.getAddPrefix())); } private void refreshConfigItems(Map<String, Object> map) { diff --git a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosConfig.java b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java similarity index 69% rename from dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosConfig.java rename to dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java index 4fb31111c..3e6a40e55 100644 --- a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/client/NacosConfig.java +++ b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java @@ -15,15 +15,11 @@ * limitations under the License. */ -package org.apache.servicecomb.config.nacos.client; +package org.apache.servicecomb.config.nacos; -import org.apache.commons.configuration.Configuration; +import org.springframework.core.env.Environment; public class NacosConfig { - public static final NacosConfig INSTANCE = new NacosConfig(); - - private static Configuration finalConfig; - public static final String DATA_ID = "servicecomb.nacos.dataId"; public static final String SERVER_ADDR = "servicecomb.nacos.serverAddr"; @@ -36,34 +32,33 @@ public class NacosConfig { public static final String CONTENT_TYPE = "servicecomb.nacos.contentType"; - private NacosConfig() { - } + private final Environment environment; - public static void setConcurrentCompositeConfiguration(Configuration config) { - finalConfig = config; + public NacosConfig(Environment environment) { + this.environment = environment; } public String getServerAddr() { - return finalConfig.getString(SERVER_ADDR); + return environment.getProperty(SERVER_ADDR); } public String getDataId() { - return finalConfig.getString(DATA_ID); + return environment.getProperty(DATA_ID); } public String getGroup() { - return finalConfig.getString(GROUP); + return environment.getProperty(GROUP); } public String getNameSpace() { - return finalConfig.getString(NAME_SPACE, "public"); + return environment.getProperty(NAME_SPACE, "public"); } public String getContentType() { - return finalConfig.getString(CONTENT_TYPE, "yaml"); + return environment.getProperty(CONTENT_TYPE, "yaml"); } public boolean getAddPrefix() { - return finalConfig.getBoolean(ADD_PREFIX, true); + return environment.getProperty(ADD_PREFIX, boolean.class, true); } } diff --git a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosConfigTest.java b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java similarity index 54% rename from dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosConfigTest.java rename to dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java index 8b82d0d67..bad19c3dc 100644 --- a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosConfigTest.java +++ b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java @@ -14,25 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.servicecomb.config.nacos; -package org.apache.servicecomb.config.nacos.client; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; -import org.apache.servicecomb.config.ConfigUtil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -public class NacosConfigTest { - @BeforeAll - public static void setUpClass() { - NacosConfig.setConcurrentCompositeConfiguration(ConfigUtil.createLocalConfig()); +public class NacosConfigConfiguration { + @Bean + public NacosConfig nacosConfig(Environment environment) { + return new NacosConfig(environment); } - @Test - public void getServiceName() { - NacosConfig instance = NacosConfig.INSTANCE; - Assertions.assertEquals("DEFAULT_GROUP", instance.getGroup()); - Assertions.assertEquals("127.0.0.1:8848", instance.getServerAddr()); - Assertions.assertEquals("example", instance.getDataId()); + @Bean + public NacosDynamicPropertiesSource nacosDynamicPropertiesSource() { + return new NacosDynamicPropertiesSource(); } } diff --git a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImpl.java b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java similarity index 51% rename from dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImpl.java rename to dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java index 528553c1e..134a4e035 100644 --- a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImpl.java +++ b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java @@ -14,41 +14,39 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.servicecomb.config.nacos; -package org.apache.servicecomb.config.nacos.archaius.sources; - -import static com.netflix.config.WatchedUpdateResult.createIncremental; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.CREATE; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.DELETE; -import static org.apache.servicecomb.config.nacos.client.ConfigurationAction.SET; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.CREATE; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.DELETE; +import static org.apache.servicecomb.config.nacos.ConfigurationAction.SET; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -import com.google.common.annotations.VisibleForTesting; -import org.apache.commons.configuration.Configuration; import org.apache.servicecomb.config.ConfigMapping; -import org.apache.servicecomb.config.nacos.client.ConfigurationAction; -import org.apache.servicecomb.config.nacos.client.NacosClient; -import org.apache.servicecomb.config.nacos.client.NacosConfig; +import org.apache.servicecomb.config.DynamicPropertiesSource; import org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; -import com.google.common.collect.ImmutableMap; +import com.google.common.annotations.VisibleForTesting; import com.netflix.config.WatchedUpdateListener; -import com.netflix.config.WatchedUpdateResult; -public class NacosConfigurationSourceImpl implements ConfigCenterConfigurationSource { +public class NacosDynamicPropertiesSource implements DynamicPropertiesSource<Map<String, Object>> { + public static final String SOURCE_NAME = "kie"; + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCenterConfigurationSource.class); private final Map<String, Object> valueCache = new ConcurrentHashMap<>(); private final List<WatchedUpdateListener> listeners = new CopyOnWriteArrayList<>(); - public NacosConfigurationSourceImpl() { + public NacosDynamicPropertiesSource() { } private final UpdateHandler updateHandler = new UpdateHandler(); @@ -58,60 +56,12 @@ public class NacosConfigurationSourceImpl implements ConfigCenterConfigurationSo return updateHandler; } - @Override - public int getOrder() { - return ORDER_BASE * 4; - } - @Override - public boolean isValidSource(Configuration localConfiguration) { - if (localConfiguration.getProperty(NacosConfig.SERVER_ADDR) == null) { - LOGGER.warn("Nacos configuration source is not configured!"); - return false; - } - return true; - } - - @Override - public void init(Configuration localConfiguration) { - NacosConfig.setConcurrentCompositeConfiguration(localConfiguration); - init(); - } - - private void init() { + private void init(Environment environment) { NacosClient nacosClient = new NacosClient(updateHandler); nacosClient.refreshNacosConfig(); } - @Override - public void addUpdateListener(WatchedUpdateListener watchedUpdateListener) { - listeners.add(watchedUpdateListener); - } - - @Override - public void removeUpdateListener(WatchedUpdateListener watchedUpdateListener) { - listeners.remove(watchedUpdateListener); - } - - private void updateConfiguration(WatchedUpdateResult result) { - for (WatchedUpdateListener l : listeners) { - try { - l.updateConfiguration(result); - } catch (Throwable ex) { - LOGGER.error("Error in invoking WatchedUpdateListener", ex); - } - } - } - - @Override - public Map<String, Object> getCurrentData() throws Exception { - return valueCache; - } - - public List<WatchedUpdateListener> getCurrentListeners() { - return listeners; - } - public class UpdateHandler { public void handle(ConfigurationAction action, Map<String, Object> config) { if (config == null || config.isEmpty()) { @@ -120,21 +70,10 @@ public class NacosConfigurationSourceImpl implements ConfigCenterConfigurationSo Map<String, Object> configuration = ConfigMapping.getConvertedMap(config); if (CREATE.equals(action)) { valueCache.putAll(configuration); - - updateConfiguration(createIncremental(ImmutableMap.copyOf(configuration), - null, - null)); } else if (SET.equals(action)) { valueCache.putAll(configuration); - - updateConfiguration(createIncremental(null, - ImmutableMap.copyOf(configuration), - null)); } else if (DELETE.equals(action)) { configuration.keySet().forEach(valueCache::remove); - updateConfiguration(createIncremental(null, - null, - ImmutableMap.copyOf(configuration))); } else { LOGGER.error("action: {} is invalid.", action.name()); return; @@ -142,4 +81,15 @@ public class NacosConfigurationSourceImpl implements ConfigCenterConfigurationSo LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet()); } } + + @Override + public EnumerablePropertySource<Map<String, Object>> create(Environment environment) { + init(environment); + return new MapPropertySource(SOURCE_NAME, valueCache); + } + + @Override + public int getOrder() { + return 0; + } } diff --git a/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 000000000..3f7419678 --- /dev/null +++ b/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You under the Apache License, Version 2.0 +## (the "License"); you may not use this file except in compliance with +## the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## --------------------------------------------------------------------------- + +org.apache.servicecomb.config.nacos.NacosConfigConfiguration diff --git a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosClientTest.java b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java similarity index 78% rename from dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosClientTest.java rename to dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java index 3a49bbc47..125d80a30 100644 --- a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/client/NacosClientTest.java +++ b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java @@ -15,23 +15,16 @@ * limitations under the License. */ -package org.apache.servicecomb.config.nacos.client; +package org.apache.servicecomb.config.nacos; import java.util.HashMap; import java.util.Map; -import org.apache.servicecomb.config.ConfigUtil; -import org.apache.servicecomb.config.nacos.archaius.sources.NacosConfigurationSourceImpl; -import org.apache.servicecomb.config.nacos.archaius.sources.NacosConfigurationSourceImpl.UpdateHandler; +import org.apache.servicecomb.config.nacos.NacosDynamicPropertiesSource.UpdateHandler; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class NacosClientTest { - @BeforeAll - public static void setUpClass() { - NacosConfig.setConcurrentCompositeConfiguration(ConfigUtil.createLocalConfig()); - } @Test public void testCompareChangedConfig() { @@ -39,7 +32,7 @@ public class NacosClientTest { Map<String, Object> before = new HashMap<>(); Map<String, Object> after = new HashMap<>(); - NacosConfigurationSourceImpl impl = new NacosConfigurationSourceImpl(); + NacosDynamicPropertiesSource impl = new NacosDynamicPropertiesSource(); UpdateHandler updateHandler = impl.new UpdateHandler(); NacosClient nacosClient = new NacosClient(updateHandler); diff --git a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImplTest.java b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImplTest.java deleted file mode 100644 index aa51be7d8..000000000 --- a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/archaius/sources/NacosConfigurationSourceImplTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.servicecomb.config.nacos.archaius.sources; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.servicecomb.config.nacos.archaius.sources.NacosConfigurationSourceImpl.UpdateHandler; -import org.apache.servicecomb.config.nacos.client.ConfigurationAction; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -import com.netflix.config.WatchedUpdateListener; - -public class NacosConfigurationSourceImplTest { - @Test - public void testCreate() throws Exception { - NacosConfigurationSourceImpl nacosConfigurationSource = new NacosConfigurationSourceImpl(); - nacosConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getAdded().isEmpty())); - UpdateHandler updateHandler = nacosConfigurationSource.getUpdateHandler(); - Map<String, Object> createItems = new HashMap<>(); - createItems.put("nacosTestKey", "testValue"); - updateHandler.handle(ConfigurationAction.CREATE, createItems); - } - - @Test - public void testUpdate() throws Exception { - NacosConfigurationSourceImpl nacosConfigurationSource = new NacosConfigurationSourceImpl(); - nacosConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getChanged().isEmpty())); - UpdateHandler updateHandler = nacosConfigurationSource.getUpdateHandler(); - Map<String, Object> updateItems = new HashMap<>(); - updateItems.put("nacosTestKey", "testValue"); - updateHandler.handle(ConfigurationAction.SET, updateItems); - - } - - @Test - public void testDelete() throws Exception { - NacosConfigurationSourceImpl nacosConfigurationSource = new NacosConfigurationSourceImpl(); - nacosConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getDeleted().isEmpty())); - UpdateHandler updateHandler = nacosConfigurationSource.getUpdateHandler(); - Map<String, Object> deleteItems = new HashMap<>(); - deleteItems.put("nacosTestKey", "testValue"); - - nacosConfigurationSource.getCurrentData().put("nacosTestKey", "testValue"); - updateHandler.handle(ConfigurationAction.DELETE, deleteItems); - Assertions.assertTrue(nacosConfigurationSource.getCurrentData().isEmpty()); - } - - @Test - public void testRemoveUpdateListener() { - NacosConfigurationSourceImpl nacosConfigurationSource = new NacosConfigurationSourceImpl(); - WatchedUpdateListener watchedUpdateListener = Mockito.mock(WatchedUpdateListener.class); - nacosConfigurationSource.addUpdateListener(watchedUpdateListener); - nacosConfigurationSource.removeUpdateListener(watchedUpdateListener); - Assertions.assertTrue(nacosConfigurationSource.getCurrentListeners().isEmpty()); - } -} diff --git a/spring-boot/spring-boot-starters/java-chassis-spring-boot-starter/src/main/java/org/apache/servicecomb/springboot/starter/ConfigurationSpringBootInitializer.java b/spring-boot/spring-boot-starters/java-chassis-spring-boot-starter/src/main/java/org/apache/servicecomb/springboot/starter/ConfigurationSpringBootInitializer.java index fd36ab455..115fc686f 100644 --- a/spring-boot/spring-boot-starters/java-chassis-spring-boot-starter/src/main/java/org/apache/servicecomb/springboot/starter/ConfigurationSpringBootInitializer.java +++ b/spring-boot/spring-boot-starters/java-chassis-spring-boot-starter/src/main/java/org/apache/servicecomb/springboot/starter/ConfigurationSpringBootInitializer.java @@ -29,6 +29,6 @@ import org.springframework.core.env.ConfigurableEnvironment; public class ConfigurationSpringBootInitializer implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - ConfigurationSpringInitializer.syncToSpring(environment); + ConfigurationSpringInitializer.addMicroserviceDefinitions(environment); } }
