You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/06/08 12:18:16 UTC
[GitHub] jeho0815 closed pull request #745: [SCB-636] change config mapping
machanism
jeho0815 closed pull request #745: [SCB-636] change config mapping machanism
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/745
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
index 3bbf5e6c3..353051ce1 100644
--- a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
+++ b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
@@ -45,7 +45,6 @@ public void tearDown() {
}
@Test
- @SuppressWarnings(value = "unchecked")
public void testConfigurations(@Mocked RoutingContext context
, @Mocked HttpServerRequest requst
, @Mocked EdgeInvocation invocation) {
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
index 22e4709cc..a8bda9094 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
@@ -61,7 +61,7 @@
String CONFIG_DEFAULT_REGISTER_BY = "SDK";
- String SERVICECOMB_ENV = "SERVICECOMB_ENV";
+ String SERVICECOMB_ENV = "service_description.environment";
String DEFAULT_SERVICECOMB_ENV = "";
}
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigMapping.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigMapping.java
index 634f3ea45..f29e9afb7 100644
--- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigMapping.java
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigMapping.java
@@ -17,20 +17,46 @@
package org.apache.servicecomb.config;
+import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
+import org.apache.commons.configuration.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Created by on 2017/1/5.
*/
public final class ConfigMapping {
private static Map<String, Object> configMap = null;
+ private static final Logger LOGGER = LoggerFactory.getLogger(ConfigMapping.class);
+
static {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
- InputStream is = loader.getResourceAsStream("mapping.yaml");
- configMap = YAMLUtil.yaml2Properties(is);
+ List<URL> urlList = new ArrayList<>();
+ configMap = new HashMap<String, Object>();
+ Enumeration<URL> urls;
+ try {
+ urls = loader.getResources("mapping.yaml");
+ while (urls.hasMoreElements()) {
+ urlList.add(urls.nextElement());
+ }
+ for (URL url : urlList) {
+ try (InputStream in = url.openStream()) {
+ configMap.putAll(YAMLUtil.yaml2Properties(in));
+ }
+ }
+ } catch (IOException e) {
+ LOGGER.error("get config mapping file error!", e);
+ }
}
private ConfigMapping() {
@@ -54,9 +80,40 @@ public static String map(String key) {
String key = entry.getKey();
Object configValue = oldMap.get(key);
if (configValue != null) {
- String newKey = (String) entry.getValue();
- retMap.put(newKey, configValue);
- retMap.remove(key);
+ if (entry.getValue() instanceof List) {
+ @SuppressWarnings("unchecked")
+ List<String> newKeys = (List<String>) entry.getValue();
+ for (String newKey : newKeys) {
+ retMap.put(newKey, configValue);
+ }
+ } else {
+ String newKey = (String) entry.getValue();
+ retMap.put(newKey, configValue);
+ }
+ }
+ }
+ return retMap;
+ }
+
+ public static Map<String, Object> getConvertedMap(Configuration config) {
+ if (configMap == null) {
+ return new LinkedHashMap<>();
+ }
+ Map<String, Object> retMap = new LinkedHashMap<>();
+ for (Map.Entry<String, Object> entry : configMap.entrySet()) {
+ String key = entry.getKey();
+ Object configValue = config.getProperty(key);
+ if (configValue != null) {
+ if (entry.getValue() instanceof List) {
+ @SuppressWarnings("unchecked")
+ List<String> newKeys = (List<String>) entry.getValue();
+ for (String newKey : newKeys) {
+ retMap.put(newKey, configValue);
+ }
+ } else {
+ String newKey = (String) entry.getValue();
+ retMap.put(newKey, configValue);
+ }
}
}
return retMap;
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 27f216101..f8b52717a 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
@@ -131,17 +131,19 @@ public static ConcurrentCompositeConfiguration createLocalConfig(List<ConfigMode
convertEnvVariable(new ConcurrentMapConfiguration(new EnvironmentConfiguration())),
"configFromEnvironment");
// If there is extra configurations, add it into config.
- EXTRA_CONFIG_MAP.entrySet().stream()
+ EXTRA_CONFIG_MAP.entrySet()
+ .stream()
.filter(mapEntry -> !mapEntry.getValue().isEmpty())
- .forEachOrdered(configMapEntry ->
- duplicateServiceCombConfigToCse(config,
- new ConcurrentMapConfiguration(configMapEntry.getValue()),
- configMapEntry.getKey()));
+ .forEachOrdered(configMapEntry -> duplicateServiceCombConfigToCse(config,
+ new ConcurrentMapConfiguration(configMapEntry.getValue()),
+ configMapEntry.getKey()));
duplicateServiceCombConfigToCse(config,
new DynamicConfiguration(
new MicroserviceConfigurationSource(configModelList), new NeverStartPollingScheduler()),
"configFromYamlFile");
-
+ duplicateServiceCombConfigToCseAtFront(config,
+ new ConcurrentMapConfiguration(ConfigMapping.getConvertedMap(config)),
+ "configFromMapping");
return config;
}
@@ -181,6 +183,14 @@ private static void duplicateServiceCombConfigToCse(ConcurrentCompositeConfigura
compositeConfiguration.addConfiguration(source, sourceName);
}
+ private static void duplicateServiceCombConfigToCseAtFront(ConcurrentCompositeConfiguration compositeConfiguration,
+ AbstractConfiguration source,
+ String sourceName) {
+ duplicateServiceCombConfigToCse(source);
+
+ compositeConfiguration.addConfigurationAtFront(source, sourceName);
+ }
+
private static ConfigCenterConfigurationSource createConfigCenterConfigurationSource(
Configuration localConfiguration) {
ConfigCenterConfigurationSource configCenterConfigurationSource =
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/YAMLUtil.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/YAMLUtil.java
index 7a6d10ee3..116d6ce96 100644
--- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/YAMLUtil.java
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/YAMLUtil.java
@@ -30,6 +30,15 @@
private YAMLUtil() {
}
+ /**
+ * load a input {@link InputStream} to be a map {@link Map}, you have to close the inputStream by yourself, such as:<br>
+ * <p>try (InputStream in = url.openStream()) {<br>
+ * configMap.putAll(YAMLUtil.yaml2Properties(in));<br>
+ * }<br>
+ * </p>
+ * @param input the stream to be loaded
+ * @return a config map
+ */
@SuppressWarnings("unchecked")
public static Map<String, Object> yaml2Properties(InputStream input) {
Map<String, Object> configurations = new LinkedHashMap<>();
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/archaius/sources/MicroserviceConfigurationSource.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/archaius/sources/MicroserviceConfigurationSource.java
index 4e44d0d1e..e39c987a7 100644
--- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/archaius/sources/MicroserviceConfigurationSource.java
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/archaius/sources/MicroserviceConfigurationSource.java
@@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;
-import org.apache.servicecomb.config.ConfigMapping;
import org.apache.servicecomb.config.YAMLUtil;
import com.netflix.config.PollResult;
@@ -45,7 +44,6 @@ public PollResult poll(boolean b, Object o) throws Exception {
configurations.putAll(YAMLUtil.retrieveItems("", configModel.getConfig()));
}
- configurations = ConfigMapping.getConvertedMap(configurations);
return PollResult.createFull(configurations);
}
}
diff --git a/foundations/foundation-config/src/main/resources/mapping.yaml b/foundations/foundation-config/src/main/resources/mapping.yaml
index 0346b782e..8bccf5894 100644
--- a/foundations/foundation-config/src/main/resources/mapping.yaml
+++ b/foundations/foundation-config/src/main/resources/mapping.yaml
@@ -15,10 +15,4 @@
## limitations under the License.
## ---------------------------------------------------------------------------
-eureka:
- client:
- serviceUrl:
- defaultZone: registry.client.serviceUrl.defaultZone
-
-service_description:
- environment: SERVICECOMB_ENV
\ No newline at end of file
+SERVICECOMB_ENV: service_description.environment
diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigMapping.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigMapping.java
index b78bd40cb..834beab1d 100644
--- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigMapping.java
+++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigMapping.java
@@ -30,18 +30,18 @@
public class TestConfigMapping {
@Test
public void testMapping() {
- String value = ConfigMapping.map("eureka.client.serviceUrl.defaultZone");
+ String value = ConfigMapping.map("SERVICECOMB_ENV");
Map<String, Object> m = ConfigMapping.getMapping();
- assertEquals(value, "registry.client.serviceUrl.defaultZone");
+ assertEquals("service_description.environment", value);
assertNotNull(m);
}
@Test
public void testConvertedMap() {
- String value = ConfigMapping.map("eureka.client.serviceUrl.defaultZone");
+ String value = ConfigMapping.map("CSE_ENV_MAPPING");
Map<String, Object> m = ConfigMapping.getMapping();
Map<String, Object> m1 = ConfigMapping.getConvertedMap(m);
- assertEquals(value, "registry.client.serviceUrl.defaultZone");
+ assertEquals("servicecomb.testmapping.key", value);
assertNotNull(m1);
}
}
diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigUtil.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigUtil.java
index c56bb7deb..f5773832a 100644
--- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigUtil.java
+++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigUtil.java
@@ -71,6 +71,7 @@ public static void beforeTest() {
System.setProperty(systemPropertyName, systemExpected);
try {
setEnv(environmentPropertyName, environmentExpected);
+ setEnv("MY_SERVICES_ENDPOINT", "https://myhost:8888");
} catch (Exception e) {
throw new IllegalStateException(e);
}
@@ -133,45 +134,45 @@ public void propertiesFromFileIsDuplicatedToCse() {
String expected = "value";
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty("servicecomb.cse.servicecomb.file", null)
- .get(),
+ .getInstance()
+ .getStringProperty("servicecomb.cse.servicecomb.file", null)
+ .get(),
equalTo(expected));
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty("cse.cse.servicecomb.file", null)
- .get(),
+ .getInstance()
+ .getStringProperty("cse.cse.servicecomb.file", null)
+ .get(),
equalTo(expected));
}
@Test
public void propertiesFromSystemIsDuplicatedToCse() {
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty(systemPropertyName, null)
- .get(),
+ .getInstance()
+ .getStringProperty(systemPropertyName, null)
+ .get(),
equalTo(systemExpected));
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty("servicecomb.cse.servicecomb.system.setting", null)
- .get(),
+ .getInstance()
+ .getStringProperty("servicecomb.cse.servicecomb.system.setting", null)
+ .get(),
equalTo(systemExpected));
}
@Test
public void propertiesFromEnvironmentIsDuplicatedToCse() {
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty(environmentPropertyName, null)
- .get(),
+ .getInstance()
+ .getStringProperty(environmentPropertyName, null)
+ .get(),
equalTo(environmentExpected));
assertThat(DynamicPropertyFactory
- .getInstance()
- .getStringProperty("servicecomb.cse.servicecomb.environment.setting", null)
- .get(),
+ .getInstance()
+ .getStringProperty("servicecomb.cse.servicecomb.environment.setting", null)
+ .get(),
equalTo(environmentExpected));
}
@@ -322,6 +323,8 @@ public void testCreateLocalConfigWithExtraConfig() {
String overriddenConfigKey = "servicecomb.cse.servicecomb.file";
extraConfig.put(extraConfigKey, extraConfigValue);
final String propertyHigherPriority = "higher_priority";
+ String mapedKey1 = "servicecomb.service.mapping.address";
+ String mapedKey2 = "servicecomb.service1.mapping.address";
extraConfig.put(overriddenConfigKey, propertyHigherPriority);
ConfigUtil.addExtraConfig("testExtraConfig", extraConfig);
@@ -330,6 +333,9 @@ public void testCreateLocalConfigWithExtraConfig() {
Assert.assertEquals(extraConfigValue, localConfiguration.getProperty(extraConfigKey));
Assert.assertEquals(propertyHigherPriority, localConfiguration.getString(overriddenConfigKey));
+ // Test mapping key/value from self mappfing.xml
+ Assert.assertEquals("https://myhost:8888", localConfiguration.getString(mapedKey1));
+ Assert.assertEquals("https://myhost:8888", localConfiguration.getString(mapedKey2));
}
@SuppressWarnings("unchecked")
diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/archaius/sources/TestYAMLConfigurationSource.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/archaius/sources/TestYAMLConfigurationSource.java
index 358317c91..02251e807 100644
--- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/archaius/sources/TestYAMLConfigurationSource.java
+++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/archaius/sources/TestYAMLConfigurationSource.java
@@ -20,7 +20,6 @@
import static org.apache.servicecomb.config.archaius.sources.ConfigSourceMaker.yamlConfigSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
import java.net.URL;
import java.util.List;
@@ -50,8 +49,6 @@ public void testPullFromClassPath() throws Exception {
assertEquals(20, configMap.size());
assertNotNull(configMap.get("trace.handler.sampler.percent"));
assertEquals(0.5, configMap.get("trace.handler.sampler.percent"));
- assertEquals("http://10.120.169.202:9980/", configMap.get("registry.client.serviceUrl.defaultZone"));
- assertNull(configMap.get("eureka.client.serviceUrl.defaultZone"));
}
@Test
diff --git a/foundations/foundation-config/src/test/resources/mapping.yaml b/foundations/foundation-config/src/test/resources/mapping.yaml
new file mode 100644
index 000000000..0603585cc
--- /dev/null
+++ b/foundations/foundation-config/src/test/resources/mapping.yaml
@@ -0,0 +1,21 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+CSE_ENV_MAPPING: servicecomb.testmapping.key
+MY_SERVICES_ENDPOINT:
+ - servicecomb.service.mapping.address
+ - servicecomb.service1.mapping.address
diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
index 596256881..d0d7c81c4 100644
--- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
+++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
@@ -179,7 +179,6 @@ public void correctResponsesHavePaths() {
@Test(expected = ServiceCombException.class)
public void testInvalidate() throws Exception {
URL resource = TestSwaggerUtils.class.getResource("/swagger1.yaml");
- byte[] buffer = new byte[2048];
Swagger swagger = SwaggerUtils.parseSwagger(resource);
SwaggerUtils.validateSwagger(swagger);
}
@@ -187,7 +186,6 @@ public void testInvalidate() throws Exception {
@Test
public void testInvalidateValid() throws Exception {
URL resource = TestSwaggerUtils.class.getResource("/swagger2.yaml");
- byte[] buffer = new byte[2048];
Swagger swagger = SwaggerUtils.parseSwagger(resource);
SwaggerUtils.validateSwagger(swagger);
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services