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 55f827169e15eeaa7f597bd11074ce23ff127fc9 Author: liubao <[email protected]> AuthorDate: Mon Sep 25 17:24:12 2023 +0800 [SCB-2008]remove archaius dependency: apollo adapter --- .../core/ConfigurationSpringInitializer.java | 2 +- .../config/{client => apollo}/ApolloClient.java | 10 +- .../config/{client => apollo}/ApolloConfig.java | 2 +- .../apollo/ApolloDynamicPropertiesSource.java | 79 +++++++++++ .../{client => apollo}/ConfigurationAction.java | 2 +- .../sources/ApolloConfigurationSourceImpl.java | 147 --------------------- ...comb.config.spi.ConfigCenterConfigurationSource | 18 --- .../{client => apollo}/ApolloClientTest.java | 6 +- .../{client => apollo}/ApolloConfigTest.java | 3 +- .../sources/ApolloConfigurationSourceImplTest.java | 78 ----------- .../servicecomb/config/BootStrapProperties.java | 142 ++++++-------------- .../org/apache/servicecomb/config/ConfigUtil.java | 5 +- .../config/DynamicPropertiesSource.java | 11 +- .../config/BootStrapPropertiesTest.java | 12 +- .../tracing/zipkin/TracingConfiguration.java | 5 +- .../servicecomb/registry/lightweight/Self.java | 17 ++- .../model/AbstractPropertiesLoader.java | 20 +-- .../model/InstancePropertiesLoader.java | 9 +- .../lightweight/model/MicroserviceFactory.java | 23 ++-- .../lightweight/model/MicroserviceInstance.java | 23 ++-- .../config/TestAbstractPropertiesLoader.java | 14 +- .../servicecomb/localregistry/RegistryBean.java | 2 +- 22 files changed, 205 insertions(+), 425 deletions(-) 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 f814f1209..b1fe6adc3 100644 --- a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java +++ b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java @@ -119,7 +119,7 @@ public class ConfigurationSpringInitializer extends PropertySourcesPlaceholderCo // watch configuration changes EventManager.register(this); - configCenterConfigurationSource = ConfigUtil.installDynamicConfig(); + configCenterConfigurationSource = ConfigUtil.installDynamicConfig(environment); addDynamicConfigurationToSpring(environment, configCenterConfigurationSource); } diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloClient.java b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloClient.java similarity index 94% rename from dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloClient.java rename to dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloClient.java index 80820abdb..acd4a4dff 100644 --- a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloClient.java +++ b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloClient.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.servicecomb.config.client; +package org.apache.servicecomb.config.apollo; -import static org.apache.servicecomb.config.client.ConfigurationAction.CREATE; -import static org.apache.servicecomb.config.client.ConfigurationAction.DELETE; -import static org.apache.servicecomb.config.client.ConfigurationAction.SET; +import static org.apache.servicecomb.config.apollo.ConfigurationAction.CREATE; +import static org.apache.servicecomb.config.apollo.ConfigurationAction.DELETE; +import static org.apache.servicecomb.config.apollo.ConfigurationAction.SET; import java.io.IOException; import java.util.HashMap; @@ -30,7 +30,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import com.google.common.annotations.VisibleForTesting; -import org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl.UpdateHandler; +import org.apache.servicecomb.config.apollo.ApolloDynamicPropertiesSource.UpdateHandler; import org.apache.servicecomb.foundation.common.utils.JsonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloConfig.java b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloConfig.java similarity index 98% rename from dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloConfig.java rename to dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloConfig.java index b8de462e3..605912e36 100644 --- a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ApolloConfig.java +++ b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloConfig.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.servicecomb.config.client; +package org.apache.servicecomb.config.apollo; import org.apache.commons.configuration.Configuration; diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloDynamicPropertiesSource.java b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloDynamicPropertiesSource.java new file mode 100644 index 000000000..01dc1a434 --- /dev/null +++ b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ApolloDynamicPropertiesSource.java @@ -0,0 +1,79 @@ +/* + * 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.apollo; + +import static org.apache.servicecomb.config.apollo.ConfigurationAction.CREATE; +import static org.apache.servicecomb.config.apollo.ConfigurationAction.DELETE; +import static org.apache.servicecomb.config.apollo.ConfigurationAction.SET; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.servicecomb.config.ConfigMapping; +import org.apache.servicecomb.config.DynamicPropertiesSource; +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; + +public class ApolloDynamicPropertiesSource implements DynamicPropertiesSource<Map<String, Object>> { + public static final String SOURCE_NAME = "apollo"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ApolloDynamicPropertiesSource.class); + + private final Map<String, Object> valueCache = new ConcurrentHashMap<>(); + + private final ApolloDynamicPropertiesSource.UpdateHandler updateHandler = + new ApolloDynamicPropertiesSource.UpdateHandler(); + + @Override + public int getOrder() { + return 300; + } + + private void init() { + ApolloClient apolloClient = new ApolloClient(updateHandler); + apolloClient.refreshApolloConfig(); + } + + public class UpdateHandler { + public void handle(ConfigurationAction action, Map<String, Object> config) { + if (config == null || config.isEmpty()) { + return; + } + Map<String, Object> configuration = ConfigMapping.getConvertedMap(config); + if (CREATE.equals(action)) { + valueCache.putAll(configuration); + } else if (SET.equals(action)) { + valueCache.putAll(configuration); + } else if (DELETE.equals(action)) { + configuration.keySet().forEach(valueCache::remove); + } else { + LOGGER.error("action: {} is invalid.", action.name()); + return; + } + LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet()); + } + } + + @Override + public EnumerablePropertySource<Map<String, Object>> create(Environment environment) { + init(); + return new MapPropertySource(SOURCE_NAME, valueCache); + } +} diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ConfigurationAction.java similarity index 94% copy from dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java copy to dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ConfigurationAction.java index 5daf1972c..1a0047152 100644 --- a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java +++ b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/apollo/ConfigurationAction.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.servicecomb.config.client; +package org.apache.servicecomb.config.apollo; public enum ConfigurationAction { CREATE, diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java b/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java deleted file mode 100644 index 3b261249f..000000000 --- a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java +++ /dev/null @@ -1,147 +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.archaius.sources; - -import static com.netflix.config.WatchedUpdateResult.createIncremental; -import static org.apache.servicecomb.config.client.ConfigurationAction.CREATE; -import static org.apache.servicecomb.config.client.ConfigurationAction.DELETE; -import static org.apache.servicecomb.config.client.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.client.ApolloClient; -import org.apache.servicecomb.config.client.ApolloConfig; -import org.apache.servicecomb.config.client.ConfigurationAction; -import org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.ImmutableMap; -import com.netflix.config.WatchedUpdateListener; -import com.netflix.config.WatchedUpdateResult; - -public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationSource { - private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCenterConfigurationSource.class); - - private final Map<String, Object> valueCache = new ConcurrentHashMap<>(); - - private final List<WatchedUpdateListener> listeners = new CopyOnWriteArrayList<>(); - - private static final String APOLLO_CONFIG_URL_KEY = "apollo.config.serverUri"; - - public ApolloConfigurationSourceImpl() { - } - - private final UpdateHandler updateHandler = new UpdateHandler(); - - @VisibleForTesting - UpdateHandler getUpdateHandler() { - return updateHandler; - } - - @Override - public int getOrder() { - return ORDER_BASE * 3; - } - - @Override - public boolean isValidSource(Configuration localConfiguration) { - if (localConfiguration.getProperty(APOLLO_CONFIG_URL_KEY) == null) { - LOGGER.warn("Apollo configuration source is not configured!"); - return false; - } - return true; - } - - @Override - public void init(Configuration localConfiguration) { - ApolloConfig.setConcurrentCompositeConfiguration(localConfiguration); - init(); - } - - private void init() { - ApolloClient apolloClient = new ApolloClient(updateHandler); - apolloClient.refreshApolloConfig(); - } - - @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()) { - return; - } - 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; - } - LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet()); - } - } -} diff --git a/dynamic-config/config-apollo/src/main/resources/META-INF/services/org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource b/dynamic-config/config-apollo/src/main/resources/META-INF/services/org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource deleted file mode 100644 index fa6f9b758..000000000 --- a/dynamic-config/config-apollo/src/main/resources/META-INF/services/org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource +++ /dev/null @@ -1,18 +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. -# - -org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl diff --git a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloClientTest.java b/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloClientTest.java similarity index 94% rename from dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloClientTest.java rename to dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloClientTest.java index 66ae6c18f..a531a2ea3 100644 --- a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloClientTest.java +++ b/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloClientTest.java @@ -15,15 +15,17 @@ * limitations under the License. */ -package org.apache.servicecomb.config.client; +package org.apache.servicecomb.config.apollo; import java.util.HashMap; import java.util.Map; import org.apache.servicecomb.config.ConfigUtil; +import org.apache.servicecomb.config.apollo.ApolloClient; +import org.apache.servicecomb.config.apollo.ApolloConfig; import org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl; import org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl.UpdateHandler; -import org.apache.servicecomb.config.client.ApolloClient.ConfigRefresh; +import org.apache.servicecomb.config.apollo.ApolloClient.ConfigRefresh; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloConfigTest.java b/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloConfigTest.java similarity index 94% rename from dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloConfigTest.java rename to dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloConfigTest.java index dfcdbbeb9..01a550609 100644 --- a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/client/ApolloConfigTest.java +++ b/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/apollo/ApolloConfigTest.java @@ -15,9 +15,10 @@ * limitations under the License. */ -package org.apache.servicecomb.config.client; +package org.apache.servicecomb.config.apollo; import org.apache.servicecomb.config.ConfigUtil; +import org.apache.servicecomb.config.apollo.ApolloConfig; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java b/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java deleted file mode 100644 index b01640c9b..000000000 --- a/dynamic-config/config-apollo/src/test/java/org/apache/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java +++ /dev/null @@ -1,78 +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.archaius.sources; - -import static org.apache.servicecomb.config.client.ConfigurationAction.CREATE; -import static org.apache.servicecomb.config.client.ConfigurationAction.DELETE; -import static org.apache.servicecomb.config.client.ConfigurationAction.SET; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl.UpdateHandler; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -import com.netflix.config.WatchedUpdateListener; - -public class ApolloConfigurationSourceImplTest { - @Test - public void testCreate() throws Exception { - - ApolloConfigurationSourceImpl apolloConfigurationSource = new ApolloConfigurationSourceImpl(); - apolloConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getAdded().isEmpty())); - UpdateHandler updateHandler = apolloConfigurationSource.getUpdateHandler(); - Map<String, Object> createItems = new HashMap<>(); - createItems.put("testKey", "testValue"); - updateHandler.handle(CREATE, createItems); - } - - @Test - public void testUpdate() throws Exception { - - ApolloConfigurationSourceImpl apolloConfigurationSource = new ApolloConfigurationSourceImpl(); - apolloConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getChanged().isEmpty())); - UpdateHandler updateHandler = apolloConfigurationSource.getUpdateHandler(); - Map<String, Object> updateItems = new HashMap<>(); - updateItems.put("testKey", "testValue"); - updateHandler.handle(SET, updateItems); - } - - @Test - public void testDelete() throws Exception { - ApolloConfigurationSourceImpl apolloConfigurationSource = new ApolloConfigurationSourceImpl(); - apolloConfigurationSource.addUpdateListener(result -> Assertions.assertFalse(result.getDeleted().isEmpty())); - UpdateHandler updateHandler = apolloConfigurationSource.getUpdateHandler(); - Map<String, Object> deleteItems = new HashMap<>(); - deleteItems.put("testKey", "testValue"); - - apolloConfigurationSource.getCurrentData().put("testKey", "testValue"); - updateHandler.handle(DELETE, deleteItems); - Assertions.assertTrue(apolloConfigurationSource.getCurrentData().isEmpty()); - } - - @Test - public void testRemoveUpdateListener() { - ApolloConfigurationSourceImpl apolloConfigurationSource = new ApolloConfigurationSourceImpl(); - WatchedUpdateListener watchedUpdateListener = Mockito.mock(WatchedUpdateListener.class); - apolloConfigurationSource.addUpdateListener(watchedUpdateListener); - apolloConfigurationSource.removeUpdateListener(watchedUpdateListener); - Assertions.assertTrue(apolloConfigurationSource.getCurrentListeners().isEmpty()); - } -} diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java index 7c18f5811..81ed8ed00 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java @@ -17,16 +17,14 @@ package org.apache.servicecomb.config; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; +import java.util.Set; -import org.apache.commons.configuration.AbstractConfiguration; import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.PropertyConverter; -import org.apache.commons.configuration.SubsetConfiguration; import org.apache.commons.lang3.StringUtils; +import org.springframework.core.env.Environment; /** @@ -108,49 +106,31 @@ public class BootStrapProperties { public static final String DEFAULT_MICROSERVICE_INSTANCE_INITIAL_STATUS = "UP"; - private static final Configuration configuration = ConfigUtil.createLocalConfig(); - - public static String readApplication(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_APPLICATION, + public static String readApplication(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_APPLICATION, OLD_CONFIG_SERVICE_APPLICATION, DEFAULT_APPLICATION); } - public static String readApplication() { - return readApplication(BootStrapProperties.configuration); - } - - public static String readServiceName(Configuration configuration) { - String result = readStringValue(configuration, CONFIG_SERVICE_NAME, OLD_CONFIG_SERVICE_NAME, + public static String readServiceName(Environment environment) { + String result = readStringValue(environment, CONFIG_SERVICE_NAME, OLD_CONFIG_SERVICE_NAME, DEFAULT_MICROSERVICE_NAME); checkMicroserviceName(result); return result; } - public static String readServiceName() { - return readServiceName(BootStrapProperties.configuration); - } - - public static String readServiceVersion(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_VERSION, OLD_CONFIG_SERVICE_VERSION, + public static String readServiceVersion(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_VERSION, OLD_CONFIG_SERVICE_VERSION, DEFAULT_MICROSERVICE_VERSION); } - public static String readServiceVersion() { - return readServiceVersion(BootStrapProperties.configuration); - } - - public static String readServiceRole(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_ROLE, OLD_CONFIG_SERVICE_ROLE, DEFAULT_MICROSERVICE_ROLE); - } - - public static String readServiceRole() { - return readServiceRole(BootStrapProperties.configuration); + public static String readServiceRole(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_ROLE, OLD_CONFIG_SERVICE_ROLE, DEFAULT_MICROSERVICE_ROLE); } - public static String readServiceDescription(Configuration configuration) { - String[] descriptionArray = configuration.getStringArray(CONFIG_SERVICE_DESCRIPTION); + public static String readServiceDescription(Environment environment) { + String[] descriptionArray = environment.getProperty(CONFIG_SERVICE_DESCRIPTION, String[].class); if (null == descriptionArray || descriptionArray.length < 1) { - descriptionArray = configuration.getStringArray(OLD_CONFIG_SERVICE_DESCRIPTION); + descriptionArray = environment.getProperty(OLD_CONFIG_SERVICE_DESCRIPTION, String[].class); } if (null == descriptionArray || descriptionArray.length < 1) { @@ -165,34 +145,18 @@ public class BootStrapProperties { return rawDescriptionBuilder.substring(0, rawDescriptionBuilder.length() - 1); } - public static String readServiceDescription() { - return readServiceDescription(BootStrapProperties.configuration); - } - - public static String readServiceEnvironment(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_ENVIRONMENT, OLD_CONFIG_SERVICE_ENVIRONMENT, + public static String readServiceEnvironment(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_ENVIRONMENT, OLD_CONFIG_SERVICE_ENVIRONMENT, DEFAULT_MICROSERVICE_ENVIRONMENT); } - public static String readServiceEnvironment() { - return readServiceEnvironment(BootStrapProperties.configuration); - } - - public static String readServiceExtendedClass(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_EXTENDED_CLASS, OLD_CONFIG_SERVICE_EXTENDED_CLASS, + public static String readServiceExtendedClass(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_EXTENDED_CLASS, OLD_CONFIG_SERVICE_EXTENDED_CLASS, null); } - public static String readServiceExtendedClass() { - return readServiceExtendedClass(BootStrapProperties.configuration); - } - - public static Map<String, String> readServiceProperties(Configuration configuration) { - return readProperties(configuration, CONFIG_SERVICE_PROPERTIES, OLD_CONFIG_SERVICE_PROPERTIES); - } - - public static Map<String, String> readServiceProperties() { - return readServiceProperties(BootStrapProperties.configuration); + public static Map<String, String> readServiceProperties(Environment environment) { + return readProperties(environment, CONFIG_SERVICE_PROPERTIES, OLD_CONFIG_SERVICE_PROPERTIES); } public static List<Object> readServicePaths(Configuration configuration) { @@ -203,73 +167,41 @@ public class BootStrapProperties { return result; } - public static List<Object> readServicePaths() { - return readServicePaths(BootStrapProperties.configuration); - } - - public static Map<String, String> readServiceInstanceProperties(Configuration configuration) { - return readProperties(configuration, CONFIG_SERVICE_INSTANCE_PROPERTIES, OLD_CONFIG_SERVICE_INSTANCE_PROPERTIES); - } - - public static Map<String, String> readServiceInstanceProperties() { - return readServiceInstanceProperties(BootStrapProperties.configuration); + public static Map<String, String> readServiceInstanceProperties(Environment environment) { + return readProperties(environment, CONFIG_SERVICE_INSTANCE_PROPERTIES, OLD_CONFIG_SERVICE_INSTANCE_PROPERTIES); } - public static String readServiceInstanceExtendedClass(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_INSTANCE_EXTENDED_CLASS, + public static String readServiceInstanceExtendedClass(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_INSTANCE_EXTENDED_CLASS, OLD_CONFIG_SERVICE_INSTANCE_EXTENDED_CLASS, null); } - public static String readServiceInstanceExtendedClass() { - return readServiceInstanceExtendedClass(BootStrapProperties.configuration); - } - - public static String readServiceInstanceInitialStatus(Configuration configuration) { - return readStringValue(configuration, CONFIG_SERVICE_INSTANCE_INITIAL_STATUS, + public static String readServiceInstanceInitialStatus(Environment environment) { + return readStringValue(environment, CONFIG_SERVICE_INSTANCE_INITIAL_STATUS, OLD_CONFIG_SERVICE_INSTANCE_INITIAL_STATUS, DEFAULT_MICROSERVICE_INSTANCE_INITIAL_STATUS); } - public static String readServiceInstanceInitialStatus() { - return readServiceInstanceInitialStatus(BootStrapProperties.configuration); - } - - private static String readStringValue(Configuration configuration, String newKey, String oldKey, + private static String readStringValue(Environment environment, String newKey, String oldKey, String defaultValue) { - String result = configuration.getString(newKey, null); + String result = environment.getProperty(newKey); if (result == null) { - return configuration.getString(oldKey, defaultValue); + return environment.getProperty(oldKey, defaultValue); } return result; } - private static Map<String, String> readProperties(Configuration configuration, String newKey, String oldKey) { - AbstractConfiguration subset = (AbstractConfiguration) configuration.subset(newKey); - if (subset.isEmpty()) { - subset = (AbstractConfiguration) configuration.subset(oldKey); + private static Map<String, String> readProperties(Environment environment, String newKey, String oldKey) { + Set<String> keys = ConfigUtil.propertiesWithPrefix(environment, newKey); + if (keys.isEmpty()) { + keys = ConfigUtil.propertiesWithPrefix(environment, oldKey); } - return toStringMap(subset); - } - - private static Map<String, String> toStringMap(AbstractConfiguration configuration) { - AbstractConfiguration root = findRoot(configuration); - Map<String, String> map = new LinkedHashMap<>(); - configuration.getKeys().forEachRemaining(key -> { - Object value = configuration.getProperty(key); - // support placeholder - value = PropertyConverter.interpolate(value, root); - map.put(key, Objects.toString(value, null)); - }); - return map; - } - - private static AbstractConfiguration findRoot(AbstractConfiguration configuration) { - if (configuration instanceof SubsetConfiguration) { - return findRoot((AbstractConfiguration) ((SubsetConfiguration) configuration).getParent()); + Map<String, String> result = new HashMap<>(keys.size()); + for (String key : keys) { + result.put(key, environment.getProperty(key)); } - - return configuration; + return result; } private static void checkMicroserviceName(String name) { diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java index c9c3908fd..11c002c3e 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigUtil.java @@ -53,6 +53,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import com.netflix.config.ConcurrentCompositeConfiguration; @@ -289,9 +290,9 @@ public final class ConfigUtil { } } - public static Set<String> propertiesWithPrefix(ConfigurableEnvironment environment, String prefix) { + public static Set<String> propertiesWithPrefix(Environment environment, String prefix) { Set<String> result = new HashSet<>(); - for (PropertySource<?> propertySource : environment.getPropertySources()) { + for (PropertySource<?> propertySource : ((ConfigurableEnvironment) environment).getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { for (String key : ((EnumerablePropertySource<?>) propertySource).getPropertyNames()) { if (key.startsWith(prefix)) { diff --git a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesSource.java similarity index 71% rename from dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java rename to foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesSource.java index 5daf1972c..8833d26af 100644 --- a/dynamic-config/config-apollo/src/main/java/org/apache/servicecomb/config/client/ConfigurationAction.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesSource.java @@ -14,11 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.servicecomb.config; -package org.apache.servicecomb.config.client; +import org.springframework.core.Ordered; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; -public enum ConfigurationAction { - CREATE, - SET, - DELETE +public interface DynamicPropertiesSource<T> extends Ordered { + EnumerablePropertySource<T> create(Environment environment); } diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java index 7187bb8e3..1513ae6ad 100644 --- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java +++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java @@ -21,16 +21,20 @@ import static org.apache.servicecomb.foundation.test.scaffolding.AssertUtils.ass import static org.assertj.core.api.Assertions.assertThat; import java.util.Map; +import java.util.Map.Entry; -import org.apache.commons.configuration.MapConfiguration; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.core.env.ConfigurableEnvironment; class BootStrapPropertiesTest { private Map<String, String> readInstanceProperties(String yaml) { Map<String, Object> properties = YAMLUtil.yaml2Properties(yaml); - MapConfiguration configuration = new MapConfiguration(properties); - - return BootStrapProperties.readServiceInstanceProperties(configuration); + ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); + for (Entry<String, Object> entry : properties.entrySet()) { + Mockito.when(environment.getProperty(entry.getKey())).thenReturn(entry.getValue().toString()); + } + return BootStrapProperties.readServiceInstanceProperties(environment); } @Test diff --git a/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/TracingConfiguration.java b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/TracingConfiguration.java index 2ab531b80..5d972825b 100644 --- a/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/TracingConfiguration.java +++ b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/TracingConfiguration.java @@ -31,6 +31,7 @@ import org.apache.servicecomb.config.DynamicProperties; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; import brave.Tracing; import brave.context.slf4j.MDCScopeDecorator; @@ -86,9 +87,9 @@ public class TracingConfiguration { @Bean Tracing tracing(Sender sender, DynamicProperties dynamicProperties, - CurrentTraceContext currentTraceContext) { + CurrentTraceContext currentTraceContext, Environment environment) { return Tracing.newBuilder() - .localServiceName(BootStrapProperties.readServiceName()) + .localServiceName(BootStrapProperties.readServiceName(environment)) .currentTraceContext(currentTraceContext) // puts trace IDs into logs .addSpanHandler(AsyncZipkinSpanHandler.create(sender)) .build(); diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java index 76cc82588..c9b863eae 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java @@ -19,15 +19,15 @@ package org.apache.servicecomb.registry.lightweight; import java.util.UUID; -import org.apache.commons.configuration.AbstractConfiguration; import org.apache.servicecomb.core.BootListener; import org.apache.servicecomb.registry.lightweight.model.Microservice; import org.apache.servicecomb.registry.lightweight.model.MicroserviceFactory; import org.apache.servicecomb.registry.lightweight.model.MicroserviceInstance; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import com.google.common.annotations.VisibleForTesting; -import com.netflix.config.ConfigurationManager; public class Self implements InitializingBean, BootListener { private Microservice microservice; @@ -39,14 +39,21 @@ public class Self implements InitializingBean, BootListener { private final MicroserviceInfo microserviceInfo = new MicroserviceInfo(); + private Environment environment; + + @Autowired + public void setEnvironment(Environment environment) { + this.environment = environment; + } + @Override public void afterPropertiesSet() { - init(ConfigurationManager.getConfigInstance()); + init(environment); } @VisibleForTesting - public Self init(AbstractConfiguration configuration) { - microservice = new MicroserviceFactory().create(configuration); + public Self init(Environment environment) { + microservice = new MicroserviceFactory().create(environment); microservice.serviceId(String.format("%s/%s/%s/%s", microservice.getEnvironment(), microservice.getAppId(), diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/AbstractPropertiesLoader.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/AbstractPropertiesLoader.java index ce93adda9..5153e09a6 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/AbstractPropertiesLoader.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/AbstractPropertiesLoader.java @@ -20,11 +20,11 @@ package org.apache.servicecomb.registry.lightweight.model; import java.util.HashMap; import java.util.Map; -import org.apache.commons.configuration.Configuration; import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.registry.api.PropertyExtended; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; /** * Loading microservice properties @@ -32,24 +32,24 @@ import org.slf4j.LoggerFactory; public abstract class AbstractPropertiesLoader { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPropertiesLoader.class); - public Map<String, String> loadProperties(Configuration configuration) { + public Map<String, String> loadProperties(Environment environment) { Map<String, String> propertiesMap = new HashMap<>(); - loadPropertiesFromConfigMap(configuration, propertiesMap); - loadPropertiesFromExtendedClass(configuration, propertiesMap); + loadPropertiesFromConfigMap(environment, propertiesMap); + loadPropertiesFromExtendedClass(environment, propertiesMap); return propertiesMap; } - protected abstract Map<String, String> readProperties(Configuration configuration); + protected abstract Map<String, String> readProperties(Environment environment); - protected abstract String readPropertiesExtendedClass(Configuration configuration); + protected abstract String readPropertiesExtendedClass(Environment environment); - private void loadPropertiesFromConfigMap(Configuration configuration, Map<String, String> propertiesMap) { - propertiesMap.putAll(readProperties(configuration)); + private void loadPropertiesFromConfigMap(Environment environment, Map<String, String> propertiesMap) { + propertiesMap.putAll(readProperties(environment)); } - private void loadPropertiesFromExtendedClass(Configuration configuration, Map<String, String> propertiesMap) { - String extendedPropertyClass = readPropertiesExtendedClass(configuration); + private void loadPropertiesFromExtendedClass(Environment environment, Map<String, String> propertiesMap) { + String extendedPropertyClass = readPropertiesExtendedClass(environment); if (StringUtils.isEmpty(extendedPropertyClass)) { return; diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/InstancePropertiesLoader.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/InstancePropertiesLoader.java index a483549e2..33bfff41c 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/InstancePropertiesLoader.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/InstancePropertiesLoader.java @@ -21,6 +21,7 @@ import java.util.Map; import org.apache.commons.configuration.Configuration; import org.apache.servicecomb.config.BootStrapProperties; +import org.springframework.core.env.Environment; public final class InstancePropertiesLoader extends AbstractPropertiesLoader { @@ -31,12 +32,12 @@ public final class InstancePropertiesLoader extends AbstractPropertiesLoader { @Override - protected Map<String, String> readProperties(Configuration configuration) { - return BootStrapProperties.readServiceInstanceProperties(configuration); + protected Map<String, String> readProperties(Environment environment) { + return BootStrapProperties.readServiceInstanceProperties(environment); } @Override - protected String readPropertiesExtendedClass(Configuration configuration) { - return BootStrapProperties.readServiceInstanceExtendedClass(configuration); + protected String readPropertiesExtendedClass(Environment environment) { + return BootStrapProperties.readServiceInstanceExtendedClass(environment); } } diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceFactory.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceFactory.java index a542ced1b..b4612b2ac 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceFactory.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceFactory.java @@ -28,15 +28,16 @@ import org.apache.commons.configuration.EnvironmentConfiguration; import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.foundation.common.Version; +import org.springframework.core.env.Environment; public class MicroserviceFactory { - public Microservice create(Configuration configuration) { - Microservice microservice = createMicroserviceFromConfiguration(configuration); - microservice.setInstance(MicroserviceInstance.createFromDefinition(configuration)); + public Microservice create(Environment environment) { + Microservice microservice = createMicroserviceFromConfiguration(environment); + microservice.setInstance(MicroserviceInstance.createFromDefinition(environment)); return microservice; } - private Microservice createMicroserviceFromConfiguration(Configuration configuration) { + private Microservice createMicroserviceFromConfiguration(Environment environment) { Microservice microservice = new Microservice(); EnvironmentConfiguration envConfig = new EnvironmentConfiguration(); @@ -44,30 +45,30 @@ public class MicroserviceFactory { !StringUtils.isEmpty(envConfig.getString(envConfig.getString(APP_MAPPING)))) { microservice.setAppId(envConfig.getString(envConfig.getString(APP_MAPPING))); } else { - microservice.setAppId(BootStrapProperties.readApplication(configuration)); + microservice.setAppId(BootStrapProperties.readApplication(environment)); } if (!StringUtils.isEmpty(envConfig.getString(SERVICE_MAPPING)) && !StringUtils.isEmpty(envConfig.getString(envConfig.getString(SERVICE_MAPPING)))) { microservice.setServiceName(envConfig.getString(envConfig.getString(SERVICE_MAPPING))); } else { - microservice.setServiceName(BootStrapProperties.readServiceName(configuration)); + microservice.setServiceName(BootStrapProperties.readServiceName(environment)); } String version; if (!StringUtils.isEmpty(envConfig.getString(VERSION_MAPPING)) && !StringUtils.isEmpty(envConfig.getString(envConfig.getString(VERSION_MAPPING)))) { version = envConfig.getString(envConfig.getString(VERSION_MAPPING)); } else { - version = BootStrapProperties.readServiceVersion(configuration); + version = BootStrapProperties.readServiceVersion(environment); } // just check version format new Version(version); microservice.setVersion(version); - microservice.setDescription(BootStrapProperties.readServiceDescription(configuration)); - microservice.setLevel(BootStrapProperties.readServiceRole(configuration)); - Map<String, String> propertiesMap = MicroservicePropertiesLoader.INSTANCE.loadProperties(configuration); + microservice.setDescription(BootStrapProperties.readServiceDescription(environment)); + microservice.setLevel(BootStrapProperties.readServiceRole(environment)); + Map<String, String> propertiesMap = MicroservicePropertiesLoader.INSTANCE.loadProperties(environment); microservice.setProperties(propertiesMap); - microservice.setEnvironment(BootStrapProperties.readServiceEnvironment(configuration)); + microservice.setEnvironment(BootStrapProperties.readServiceEnvironment(environment)); // set alias name when allow cross app if (microservice.allowCrossApp()) { diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceInstance.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceInstance.java index 740ddd619..2d9192817 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceInstance.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroserviceInstance.java @@ -22,15 +22,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.commons.configuration.Configuration; import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.registry.api.DataCenterInfo; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.definition.DefinitionConst; +import org.springframework.core.env.Environment; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.netflix.config.DynamicPropertyFactory; /** * Created by on 2016/12/5. @@ -177,36 +176,32 @@ public class MicroserviceInstance { } // Some properties of microservice instance are dynamic changed, not cover them all now. - public static MicroserviceInstance createFromDefinition(Configuration configuration) { + public static MicroserviceInstance createFromDefinition(Environment environment) { MicroserviceInstance microserviceInstance = new MicroserviceInstance(); // default hard coded values microserviceInstance.setStage(DefinitionConst.DEFAULT_STAGE); microserviceInstance.setStatus(MicroserviceInstanceStatus - .valueOf(BootStrapProperties.readServiceInstanceInitialStatus())); + .valueOf(BootStrapProperties.readServiceInstanceInitialStatus(environment))); HealthCheck healthCheck = new HealthCheck(); healthCheck.setMode(HealthCheckMode.HEARTBEAT); microserviceInstance.setHealthCheck(healthCheck); // load properties - Map<String, String> propertiesMap = InstancePropertiesLoader.INSTANCE.loadProperties(configuration); + Map<String, String> propertiesMap = InstancePropertiesLoader.INSTANCE.loadProperties(environment); microserviceInstance.setProperties(propertiesMap); // load data center information - loadDataCenterInfo(microserviceInstance); + loadDataCenterInfo(environment, microserviceInstance); return microserviceInstance; } - private static void loadDataCenterInfo(MicroserviceInstance microserviceInstance) { - String dataCenterName = DynamicPropertyFactory.getInstance() - .getStringProperty("servicecomb.datacenter.name", null) - .get(); + private static void loadDataCenterInfo(Environment environment, MicroserviceInstance microserviceInstance) { + String dataCenterName = environment.getProperty("servicecomb.datacenter.name"); if (StringUtils.isEmpty(dataCenterName)) { return; } - String region = DynamicPropertyFactory.getInstance(). - getStringProperty("servicecomb.datacenter.region", null).get(); - String availableZone = DynamicPropertyFactory.getInstance(). - getStringProperty("servicecomb.datacenter.availableZone", null).get(); + String region = environment.getProperty("servicecomb.datacenter.region"); + String availableZone = environment.getProperty("servicecomb.datacenter.availableZone"); DataCenterInfo dataCenterInfo = new DataCenterInfo(); dataCenterInfo.setName(dataCenterName); dataCenterInfo.setRegion(region); diff --git a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java index 6a85fb26b..9f7692e87 100644 --- a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java +++ b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java @@ -17,25 +17,23 @@ package org.apache.servicecomb.registry.config; -import org.apache.commons.configuration.Configuration; import org.apache.servicecomb.config.BootStrapProperties; - -import com.netflix.config.DynamicConfiguration; - import org.apache.servicecomb.registry.lightweight.model.AbstractPropertiesLoader; import org.apache.servicecomb.registry.lightweight.model.MicroservicePropertiesLoader; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.core.env.Environment; public class TestAbstractPropertiesLoader { @Test public void testExtendedClassCompatible() { - Configuration configuration = new DynamicConfiguration(); - configuration.setProperty(BootStrapProperties.CONFIG_SERVICE_EXTENDED_CLASS, "invalidClass"); - + Environment environment = Mockito.mock(Environment.class); + Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_EXTENDED_CLASS)) + .thenReturn("invalidClass"); AbstractPropertiesLoader loader = MicroservicePropertiesLoader.INSTANCE; try { - loader.loadProperties(configuration); + loader.loadProperties(environment); Assertions.fail("Must throw exception"); } catch (Error e) { Assertions.assertEquals(ClassNotFoundException.class, e.getCause().getClass()); diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/RegistryBean.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/RegistryBean.java index 3e224c9df..3a3ac85d4 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/RegistryBean.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/RegistryBean.java @@ -120,7 +120,7 @@ public class RegistryBean { if (!StringUtils.isEmpty(configAppId)) { return configAppId; } - return BootStrapProperties.readApplication(); + return BootStrapProperties.DEFAULT_APPLICATION; } public String getId() {
