You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/11/06 09:26:36 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2115]support mapping servicestage environments to microservice definitions (#2042)

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


The following commit(s) were added to refs/heads/master by this push:
     new 0d33d00  [SCB-2115]support mapping servicestage environments to microservice definitions (#2042)
0d33d00 is described below

commit 0d33d00d40649b2abed8acb9dada3bfc98e4f9de
Author: bao liu <bi...@qq.com>
AuthorDate: Fri Nov 6 17:26:27 2020 +0800

    [SCB-2115]support mapping servicestage environments to microservice definitions (#2042)
---
 .../core/ConfigurationSpringInitializer.java       | 11 +++++++++++
 coverage-reports/pom.xml                           |  8 ++++++++
 .../demo/multiServiceCenterClient/ServerBTest.java |  3 +++
 .../ConfigurationEndpoint.java                     |  5 +++++
 .../src/main/resources/application.yml             |  1 +
 .../src/main/resources/mapping.yaml                | 20 +++++++++++++++++++
 .../src/main/resources/microservice.yaml           |  5 ++++-
 dependencies/bom/pom.xml                           | 10 +++++++---
 distribution/pom.xml                               |  9 ++++++---
 .../apache/servicecomb/config/ConfigMapping.java   | 13 +++++++++++-
 huawei-cloud/environment/README.md                 | 15 ++++++++++++++
 huawei-cloud/{ => environment}/pom.xml             | 23 ++++++++++++----------
 .../src/main/resources/mapping.yaml                | 21 ++++++--------------
 huawei-cloud/pom.xml                               |  1 +
 .../servicestage/src/main/resources/mapping.yaml   | 10 +---------
 15 files changed, 113 insertions(+), 42 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 3aae470..6317fad 100644
--- a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
+++ b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.Properties;
 
 import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.servicecomb.config.ConfigMapping;
 import org.apache.servicecomb.config.ConfigUtil;
 import org.apache.servicecomb.config.YAMLUtil;
 import org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader;
@@ -88,6 +89,8 @@ public class ConfigurationSpringInitializer extends PropertyPlaceholderConfigure
 
     addMicroserviceYAMLToSpring(environment);
 
+    addMappingToString(environment);
+
     startupBootStrapService(environment);
 
     ConfigUtil.addExtraConfig(EXTRA_CONFIG_SOURCE_PREFIX + environmentName, extraConfig);
@@ -145,6 +148,14 @@ public class ConfigurationSpringInitializer extends PropertyPlaceholderConfigure
     }
   }
 
+  private void addMappingToString(Environment environment) {
+    if (environment instanceof ConfigurableEnvironment) {
+      ConfigurableEnvironment ce = (ConfigurableEnvironment) environment;
+      Map<String, Object> mappings = ConfigMapping.getConvertedMap(environment);
+      ce.getPropertySources().addFirst(new MapPropertySource("mapping.yaml", mappings));
+    }
+  }
+
   private void addDynamicConfigurationToSpring(Environment environment) {
     if (environment instanceof ConfigurableEnvironment) {
       ConfigurableEnvironment ce = (ConfigurableEnvironment) environment;
diff --git a/coverage-reports/pom.xml b/coverage-reports/pom.xml
index e2a5763..2fe61bb 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -256,6 +256,14 @@
     <!-- ServiceComb ServiceStage extension -->
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
+      <artifactId>dtm</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>servicestage-environment</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
       <artifactId>servicestage</artifactId>
     </dependency>
 
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBTest.java b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBTest.java
index 3a1abd1..42c3c62 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBTest.java
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBTest.java
@@ -46,6 +46,9 @@ public class ServerBTest implements CategorizedTestCase {
     TestMgr.check("key4-boot", configurationEndpoint.getValue("demo.multi.service.center.serverB.key4", 1));
     TestMgr.check("key4-boot", configurationEndpoint.getValue("demo.multi.service.center.serverB.key4", 2));
     TestMgr.check("key4-boot", configurationEndpoint.getValue("demo.multi.service.center.serverB.key4", 3));
+    TestMgr.check("key5-high", configurationEndpoint.getValue("demo.multi.service.center.serverB.key5", 1));
+    TestMgr.check("key5-high", configurationEndpoint.getValue("demo.multi.service.center.serverB.key5", 2));
+    TestMgr.check("key5-high", configurationEndpoint.getValue("demo.multi.service.center.serverB.key5", 3));
   }
 
   @Override
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/java/org/apache/servicecomb/demo/multiServiceCenterServerB/ConfigurationEndpoint.java b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/java/org/apache/servicecomb/demo/multiServiceCenterServerB/ConfigurationEndpoint.java
index 9c4c2b5..19be127 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/java/org/apache/servicecomb/demo/multiServiceCenterServerB/ConfigurationEndpoint.java
+++ b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/java/org/apache/servicecomb/demo/multiServiceCenterServerB/ConfigurationEndpoint.java
@@ -52,6 +52,9 @@ public class ConfigurationEndpoint {
   @Value("${demo.multi.service.center.serverB.key4}")
   private String key4;
 
+  @Value("${demo.multi.service.center.serverB.key5}")
+  private String key5;
+
   @GetMapping(path = "/config")
   public String getValue(@RequestParam(name = "key") String key, @RequestParam(name = "type") int type) {
     if (type == 1) {
@@ -68,6 +71,8 @@ public class ConfigurationEndpoint {
           return key3;
         case "demo.multi.service.center.serverB.key4":
           return key4;
+        case "demo.multi.service.center.serverB.key5":
+          return key5;
         default:
           return null;
       }
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/application.yml b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/application.yml
index 663c6ad..8303ad5 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/application.yml
+++ b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/application.yml
@@ -34,4 +34,5 @@ servicecomb:
 demo.multi.service.center.serverB:
   key1: key1-boot
   key4: key4-boot
+  key5: key5
 
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/mapping.yaml b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/mapping.yaml
new file mode 100644
index 0000000..01642a3
--- /dev/null
+++ b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/mapping.yaml
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+
+demo.multi.service.center.serverB.actual.key5:
+  - demo.multi.service.center.serverB.key5
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/microservice.yaml b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/microservice.yaml
index 34d28d2..ff62f18 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/microservice.yaml
+++ b/demo/demo-multi-service-center/demo-multi-service-center-serverB/src/main/resources/microservice.yaml
@@ -19,4 +19,7 @@ servicecomb-config-order: 1
 
 demo.multi.service.center.serverB:
   key1: key1-override
-  key2: key2-override
\ No newline at end of file
+  key2: key2-override
+  key5: key5
+  actual:
+    key5: key5-high
diff --git a/dependencies/bom/pom.xml b/dependencies/bom/pom.xml
index addebe8..4779e5b 100644
--- a/dependencies/bom/pom.xml
+++ b/dependencies/bom/pom.xml
@@ -300,13 +300,17 @@
       <!-- ServiceComb ServiceStage extension -->
       <dependency>
         <groupId>org.apache.servicecomb</groupId>
-        <artifactId>servicestage</artifactId>
+        <artifactId>dtm</artifactId>
         <version>${project.version}</version>
       </dependency>
-      <!-- ServiceComb Dtm extension -->
       <dependency>
         <groupId>org.apache.servicecomb</groupId>
-        <artifactId>dtm</artifactId>
+        <artifactId>servicestage-environment</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.servicecomb</groupId>
+        <artifactId>servicestage</artifactId>
         <version>${project.version}</version>
       </dependency>
       <!-- ServiceComb: swagger -->
diff --git a/distribution/pom.xml b/distribution/pom.xml
index fa9d9a2..544a222 100644
--- a/distribution/pom.xml
+++ b/distribution/pom.xml
@@ -254,13 +254,16 @@
     <!-- ServiceComb ServiceStage extension -->
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
-      <artifactId>servicestage</artifactId>
+      <artifactId>dtm</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>servicestage-environment</artifactId>
     </dependency>
 
-    <!-- ServiceComb dtm extension -->
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
-      <artifactId>dtm</artifactId>
+      <artifactId>servicestage</artifactId>
     </dependency>
 
     <!-- swagger -->
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 b002fb4..5715ffd 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
@@ -31,6 +31,7 @@ import org.apache.commons.configuration.Configuration;
 import org.apache.servicecomb.foundation.common.utils.JvmUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
 
 /**
  * Created by   on 2017/1/5.
@@ -90,7 +91,17 @@ public final class ConfigMapping {
     return retMap;
   }
 
-  private static void putConfigsToRetMap(Map<String, Object> retMap, Map.Entry<String, Object> entry, Object configValue) {
+  public static Map<String, Object> getConvertedMap(Environment environment) {
+    if (configMap == null) {
+      return new LinkedHashMap<>();
+    }
+    Map<String, Object> retMap = new LinkedHashMap<>();
+    configMap.entrySet().forEach(entry -> putConfigsToRetMap(retMap, entry, environment.getProperty(entry.getKey())));
+    return retMap;
+  }
+
+  private static void putConfigsToRetMap(Map<String, Object> retMap, Map.Entry<String, Object> entry,
+      Object configValue) {
     if (configValue != null) {
       if (entry.getValue() instanceof List) {
         @SuppressWarnings("unchecked")
diff --git a/huawei-cloud/environment/README.md b/huawei-cloud/environment/README.md
new file mode 100644
index 0000000..3f17873
--- /dev/null
+++ b/huawei-cloud/environment/README.md
@@ -0,0 +1,15 @@
+这个模块主要提供 servicestage 部署环境变量和配置项的映射,通过这种方式可以简化配置。 
+
+servicestage 存在环境、应用、组件等概念,这些内容都是在部署的时候输入,可以将这些内容用于微服务的配置信息。 
+
+应用 - CAS_APPLICATION_NAME - servicecomb.service.application
+组件 - CAS_COMPONENT_NAME - servicecomb.service.name
+组件版本 - CAS_INSTANCE_VERSION - servicecomb.service.version
+
+部署的过程还会注入环境里面的资源信息。
+
+服务/配置中心地址(逻辑多租,APIG场景) - PAAS_CSE_ENDPOINT - servicecomb.service.registry.address/servicecomb.config.client.serverUri
+服务中心地址 - PAAS_CSE_SC_ENDPOINT - servicecomb.service.registry.address
+配置中心地址 - PAAS_CSE_CC_ENDPOINT - servicecomb.config.client.serverUri
+项目(区域) - PAAS_PROJECT_NAME - servicecomb.credentials.project
+
diff --git a/huawei-cloud/pom.xml b/huawei-cloud/environment/pom.xml
similarity index 72%
copy from huawei-cloud/pom.xml
copy to huawei-cloud/environment/pom.xml
index 9fe822f..aeacd32 100644
--- a/huawei-cloud/pom.xml
+++ b/huawei-cloud/environment/pom.xml
@@ -19,20 +19,23 @@
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <parent>
+    <artifactId>huawei-cloud</artifactId>
     <groupId>org.apache.servicecomb</groupId>
-    <artifactId>java-chassis-parent</artifactId>
     <version>2.1.3-SNAPSHOT</version>
-    <relativePath>../parents/default</relativePath>
   </parent>
-
-  <artifactId>huawei-cloud</artifactId>
-  <name>Java Chassis::Huawei Cloud</name>
-  <packaging>pom</packaging>
   <modelVersion>4.0.0</modelVersion>
+  <artifactId>servicestage-environment</artifactId>
+  <name>Java Chassis::Huawei Cloud::ServiceStage</name>
 
-  <modules>
-    <module>servicestage</module>
-    <module>dtm</module>
-  </modules>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>java-chassis-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>registry-service-center</artifactId>
+    </dependency>
+  </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/huawei-cloud/servicestage/src/main/resources/mapping.yaml b/huawei-cloud/environment/src/main/resources/mapping.yaml
similarity index 66%
copy from huawei-cloud/servicestage/src/main/resources/mapping.yaml
copy to huawei-cloud/environment/src/main/resources/mapping.yaml
index 0a61d9c..27c43e5 100644
--- a/huawei-cloud/servicestage/src/main/resources/mapping.yaml
+++ b/huawei-cloud/environment/src/main/resources/mapping.yaml
@@ -14,17 +14,13 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
-CAS_APPLICATION_ID:
-  - servicecomb.huaweicloud.servicestage.cas.application-id
+CAS_APPLICATION_NAME:
+  - servicecomb.service.application
 CAS_COMPONENT_NAME:
-  - servicecomb.huaweicloud.servicestage.cas.component-name
+  - servicecomb.service.name
 CAS_INSTANCE_VERSION:
-  - servicecomb.huaweicloud.servicestage.cas.instance-version
-CAS_INSTANCE_ID:
-  - servicecomb.huaweicloud.servicestage.cas.instance-id
-CAS_ENVIRONMENT_ID:
-  - servicecomb.huaweicloud.servicestage.cas.environment-id
+  - servicecomb.service.version
+
 PAAS_CSE_ENDPOINT:
   - servicecomb.service.registry.address
   - servicecomb.config.client.serverUri
@@ -34,9 +30,4 @@ PAAS_CSE_CC_ENDPOINT:
   - servicecomb.config.client.serverUri
 PAAS_PROJECT_NAME:
   - servicecomb.credentials.project
-servicecomb.monitor.client.enable:
-  - servicecomb.monitor.client.enabled
-servicecomb.monitor.client.sslEnalbed:
-  - servicecomb.monitor.client.sslEnabled
-servicecomb.engine.engineName:
-  - servicecomb.engine.name
+
diff --git a/huawei-cloud/pom.xml b/huawei-cloud/pom.xml
index 9fe822f..0681443 100644
--- a/huawei-cloud/pom.xml
+++ b/huawei-cloud/pom.xml
@@ -32,6 +32,7 @@
 
   <modules>
     <module>servicestage</module>
+    <module>environment</module>
     <module>dtm</module>
   </modules>
 
diff --git a/huawei-cloud/servicestage/src/main/resources/mapping.yaml b/huawei-cloud/servicestage/src/main/resources/mapping.yaml
index 0a61d9c..f06ddab 100644
--- a/huawei-cloud/servicestage/src/main/resources/mapping.yaml
+++ b/huawei-cloud/servicestage/src/main/resources/mapping.yaml
@@ -25,15 +25,7 @@ CAS_INSTANCE_ID:
   - servicecomb.huaweicloud.servicestage.cas.instance-id
 CAS_ENVIRONMENT_ID:
   - servicecomb.huaweicloud.servicestage.cas.environment-id
-PAAS_CSE_ENDPOINT:
-  - servicecomb.service.registry.address
-  - servicecomb.config.client.serverUri
-PAAS_CSE_SC_ENDPOINT:
-  - servicecomb.service.registry.address
-PAAS_CSE_CC_ENDPOINT:
-  - servicecomb.config.client.serverUri
-PAAS_PROJECT_NAME:
-  - servicecomb.credentials.project
+
 servicecomb.monitor.client.enable:
   - servicecomb.monitor.client.enabled
 servicecomb.monitor.client.sslEnalbed: