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/04/10 06:57:13 UTC
[servicecomb-java-chassis] 01/02: [SCB-1862] ServiceComb add
extensions to support cas env variables
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 144eac326fe18bff3c64fa9437571ae700eb641d
Author: heyile <25...@qq.com>
AuthorDate: Thu Apr 9 18:03:46 2020 +0800
[SCB-1862] ServiceComb add extensions to support cas env variables
---
dependencies/bom/pom.xml | 6 ++
.../foundation/common/RegisterManager.java | 4 ++
pom.xml | 1 +
.../serviceregistry/adapter/DefaultEnvAdapter.java | 29 +++++++++
.../serviceregistry/adapter/EnvAdapter.java | 44 ++++++++++++++
.../serviceregistry/adapter/EnvAdapterManager.java | 67 +++++++++++++++++++++
.../task/MicroserviceInstanceRegisterTask.java | 2 +
.../task/MicroserviceRegisterTask.java | 3 +
....servicecomb.serviceregistry.adapter.EnvAdapter | 18 ++++++
.../adapter/CasEnvAdapterHighOne.java | 44 ++++++++++++++
.../serviceregistry/adapter/CasEnvAdapterOne.java | 44 ++++++++++++++
.../adapter/CasEnvAdapterThree.java | 34 +++++++++++
.../serviceregistry/adapter/CasEnvAdapterTwo.java | 44 ++++++++++++++
.../adapter/TestEnvAdapterManager.java | 63 ++++++++++++++++++++
....servicecomb.serviceregistry.adapter.EnvAdapter | 21 +++++++
third-part-extensions/cas-env-variables/pom.xml | 37 ++++++++++++
.../org/apache/servicecomb/cas/CasEnvConfig.java | 68 ++++++++++++++++++++++
.../servicecomb/cas/CasEnvVariablesAdapter.java | 40 +++++++++++++
....servicecomb.serviceregistry.adapter.EnvAdapter | 18 ++++++
.../src/main/resources/mapping.yaml | 41 +++++++++++++
.../apache/servicecomb/cas/TestCasEnvConfig.java | 46 +++++++++++++++
.../servicecomb/cas/TestEnvVariablesAdapter.java | 50 ++++++++++++++++
third-part-extensions/pom.xml | 37 ++++++++++++
23 files changed, 761 insertions(+)
diff --git a/dependencies/bom/pom.xml b/dependencies/bom/pom.xml
index 21f2ca8..c691b0a 100644
--- a/dependencies/bom/pom.xml
+++ b/dependencies/bom/pom.xml
@@ -365,6 +365,12 @@
<artifactId>transport-rest-vertx</artifactId>
<version>${project.version}</version>
</dependency>
+ <!-- ServiceComb third-extensions -->
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>cas-env-variables</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
</project>
\ No newline at end of file
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/RegisterManager.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/RegisterManager.java
index d2933cf..5fa4b8c 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/RegisterManager.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/RegisterManager.java
@@ -35,6 +35,10 @@ public class RegisterManager<KEY, VALUE> {
private Object lockObj = new Object();
+ protected Map<KEY, VALUE> getObjMap() {
+ return objMap;
+ }
+
public RegisterManager(String name) {
this.name = name;
}
diff --git a/pom.xml b/pom.xml
index 2579f06..e681f69 100755
--- a/pom.xml
+++ b/pom.xml
@@ -126,6 +126,7 @@
<module>inspector</module>
<module>solutions</module>
<module>clients</module>
+ <module>third-part-extensions</module>
</modules>
<reporting>
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/DefaultEnvAdapter.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/DefaultEnvAdapter.java
new file mode 100644
index 0000000..7895d54
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/DefaultEnvAdapter.java
@@ -0,0 +1,29 @@
+/*
+ * 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.serviceregistry.adapter;
+
+public class DefaultEnvAdapter implements EnvAdapter {
+ @Override
+ public String getEnvName() {
+ return "default-env-default-adapter";
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapter.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapter.java
new file mode 100644
index 0000000..02fc5b6
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapter.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public interface EnvAdapter {
+
+ default boolean enabled() {
+ return true;
+ }
+
+ String getEnvName();
+
+ int getOrder();
+
+ default void beforeRegisterService(Microservice microservice) {
+ //do nothing default
+ }
+
+ default void beforeRegisterSchema(String schemaId, String content) {
+ //do nothing default
+ }
+
+ default void beforeRegisterInstance(MicroserviceInstance instance) {
+ //do nothing default
+ }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapterManager.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapterManager.java
new file mode 100644
index 0000000..8b31d26
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/adapter/EnvAdapterManager.java
@@ -0,0 +1,67 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import java.util.List;
+
+import org.apache.servicecomb.foundation.common.RegisterManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EnvAdapterManager extends RegisterManager<String, EnvAdapter> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(EnvAdapterManager.class);
+
+ private static final List<EnvAdapter> ENV_ADAPTERS = SPIServiceUtils.getSortedService(EnvAdapter.class);
+
+ private static final String NAME = "service env adapter";
+
+ public static final EnvAdapterManager INSTANCE = new EnvAdapterManager();
+
+ public EnvAdapterManager() {
+ super(NAME);
+ ENV_ADAPTERS.forEach(envAdapter -> {
+ if (envAdapter.enabled()) {
+ getObjMap().putIfAbsent(envAdapter.getEnvName(), envAdapter);
+ }
+ });
+ }
+
+ public void processMicroserviceWithAdapters(Microservice microservice) {
+ getObjMap().forEach((name, adapter) -> {
+ LOGGER.info("Start process microservice with adapter {}", name);
+ adapter.beforeRegisterService(microservice);
+ });
+ }
+
+ public void processSchemaWithAdapters(String schemaId, String content) {
+ getObjMap().forEach((name, adapter) -> {
+ LOGGER.info("Start process schema with adapter {}", name);
+ adapter.beforeRegisterSchema(schemaId, content);
+ });
+ }
+
+ public void processInstanceWithAdapters(MicroserviceInstance instance) {
+ getObjMap().forEach((name, adapter) -> {
+ LOGGER.info("Start process instance with adapter {}", name);
+ adapter.beforeRegisterInstance(instance);
+ });
+ }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
index 6b18693..438fbce 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
@@ -21,6 +21,7 @@ import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+import org.apache.servicecomb.serviceregistry.adapter.EnvAdapterManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
@@ -67,6 +68,7 @@ public class MicroserviceInstanceRegisterTask extends AbstractRegisterTask {
microserviceInstance.getHealthCheck().setInterval(serviceRegistryConfig.getHeartbeatInterval());
microserviceInstance.getHealthCheck().setTimes(serviceRegistryConfig.getResendHeartBeatTimes());
+ EnvAdapterManager.INSTANCE.processInstanceWithAdapters(microserviceInstance);
String instanceId = srClient.registerMicroserviceInstance(microserviceInstance);
MicroserviceInstance temIns = srClient.findServiceInstance(microserviceInstance.getServiceId(), instanceId);
if (temIns != null) {
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 3a7f33a..31909b1 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -33,6 +33,7 @@ import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.client.http.Holder;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+import org.apache.servicecomb.serviceregistry.adapter.EnvAdapterManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -93,6 +94,7 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
return false;
}
} else {
+ EnvAdapterManager.INSTANCE.processMicroserviceWithAdapters(microservice);
serviceId = srClient.registerMicroservice(microservice);
if (StringUtils.isEmpty(serviceId)) {
LOGGER.error(
@@ -342,6 +344,7 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
* @return true if register success, otherwise false
*/
private boolean registerSingleSchema(String schemaId, String content) {
+ EnvAdapterManager.INSTANCE.processSchemaWithAdapters(schemaId, content);
return srClient.registerSchema(microservice.getServiceId(), schemaId, content);
}
diff --git a/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
new file mode 100644
index 0000000..b095b89
--- /dev/null
+++ b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
@@ -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.serviceregistry.adapter.DefaultEnvAdapter
\ No newline at end of file
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterHighOne.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterHighOne.java
new file mode 100644
index 0000000..b92c9ac
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterHighOne.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class CasEnvAdapterHighOne implements EnvAdapter {
+ private static final String NAME = "cas_env_one";
+
+ @Override
+ public String getEnvName() {
+ return NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+
+ @Override
+ public void beforeRegisterService(Microservice microservice) {
+ microservice.getProperties().put(NAME, "order=0");
+ }
+
+ @Override
+ public void beforeRegisterInstance(MicroserviceInstance instance) {
+ instance.getProperties().put(NAME, "order=0");
+ }
+}
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterOne.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterOne.java
new file mode 100644
index 0000000..86fd689
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterOne.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class CasEnvAdapterOne implements EnvAdapter {
+ private static final String NAME = "cas_env_one";
+
+ @Override
+ public String getEnvName() {
+ return NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ return 10;
+ }
+
+ @Override
+ public void beforeRegisterService(Microservice microservice) {
+ microservice.getProperties().put(NAME, "order=10");
+ }
+
+ @Override
+ public void beforeRegisterInstance(MicroserviceInstance instance) {
+ instance.getProperties().put(NAME, "order=10");
+ }
+}
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterThree.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterThree.java
new file mode 100644
index 0000000..9bec43a
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterThree.java
@@ -0,0 +1,34 @@
+/*
+ * 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.serviceregistry.adapter;
+
+public class CasEnvAdapterThree implements EnvAdapter {
+ @Override
+ public String getEnvName() {
+ return "cas-env-three";
+ }
+
+ @Override
+ public boolean enabled() {
+ return false;
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+}
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterTwo.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterTwo.java
new file mode 100644
index 0000000..f3e2e9e
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/CasEnvAdapterTwo.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class CasEnvAdapterTwo implements EnvAdapter {
+ private static final String NAME = "cas_env_two";
+
+ @Override
+ public String getEnvName() {
+ return NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+
+ @Override
+ public void beforeRegisterService(Microservice microservice) {
+ microservice.getProperties().put(NAME, "order=0");
+ }
+
+ @Override
+ public void beforeRegisterInstance(MicroserviceInstance instance) {
+ instance.getProperties().put(NAME, "order=0");
+ }
+}
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/TestEnvAdapterManager.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/TestEnvAdapterManager.java
new file mode 100644
index 0000000..7bef5be
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/adapter/TestEnvAdapterManager.java
@@ -0,0 +1,63 @@
+/*
+ * 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.serviceregistry.adapter;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.junit.Test;
+
+public class TestEnvAdapterManager {
+
+ private EnvAdapterManager manager = EnvAdapterManager.INSTANCE;
+
+ @Test
+ public void testLoadAdapter() {
+ assertEquals(3, manager.values().size());
+ assertNull(manager.findValue("cas-env-three"));
+ assertNotNull(manager.findValue("default-env-default-adapter"));
+ assertNotNull(manager.findValue("cas_env_one"));
+ assertNotNull(manager.findValue("cas_env_two"));
+
+ assertEquals(0, manager.findValue("cas_env_one").getOrder());
+ assertEquals(0, manager.findValue("cas_env_two").getOrder());
+ assertEquals(0, manager.findValue("default-env-default-adapter").getOrder());
+ }
+
+ @Test
+ public void testProcessMicroservice() {
+ Microservice microservice = new Microservice();
+ manager.processMicroserviceWithAdapters(microservice);
+
+ assertEquals("order=0", microservice.getProperties().get("cas_env_one"));
+ assertEquals("order=0", microservice.getProperties().get("cas_env_two"));
+ assertNull(microservice.getProperties().get("default-env-default-adapter"));
+ }
+
+ @Test
+ public void testProcessInstance() {
+ MicroserviceInstance instance = new MicroserviceInstance();
+ manager.processInstanceWithAdapters(instance);
+
+ assertEquals("order=0", instance.getProperties().get("cas_env_one"));
+ assertEquals("order=0", instance.getProperties().get("cas_env_two"));
+ assertNull(instance.getProperties().get("default-env-default-adapter"));
+ }
+}
diff --git a/service-registry/src/test/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter b/service-registry/src/test/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
new file mode 100644
index 0000000..df069fd
--- /dev/null
+++ b/service-registry/src/test/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
@@ -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.
+#
+
+org.apache.servicecomb.serviceregistry.adapter.CasEnvAdapterOne
+org.apache.servicecomb.serviceregistry.adapter.CasEnvAdapterTwo
+org.apache.servicecomb.serviceregistry.adapter.CasEnvAdapterHighOne
+org.apache.servicecomb.serviceregistry.adapter.CasEnvAdapterThree
\ No newline at end of file
diff --git a/third-part-extensions/cas-env-variables/pom.xml b/third-part-extensions/cas-env-variables/pom.xml
new file mode 100644
index 0000000..4c20b62
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/pom.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ 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>third-part-extensions</artifactId>
+ <groupId>org.apache.servicecomb</groupId>
+ <version>2.0.2-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>cas-env-variables</artifactId>
+ <name>Java Chassis::Third-Part Extensions::Cas Env Variables</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>service-registry</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvConfig.java b/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvConfig.java
new file mode 100644
index 0000000..139912b
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvConfig.java
@@ -0,0 +1,68 @@
+/*
+ * 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.cas;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+public class CasEnvConfig {
+ private static final String CAS_BASE = "servicecomb.cas.";
+
+ private static final String APPLICATION_ID = CAS_BASE + "application-id";
+
+ private static final String COMPONENT_NAME = CAS_BASE + "component-name";
+
+ private static final String INSTANCE_VERSION = CAS_BASE + "instance-version";
+
+ private static final String INSTANCE_ID = CAS_BASE + "instance-id";
+
+ private static final String ENVIRONMENT_ID = CAS_BASE + "environment-id";
+
+ public static final CasEnvConfig INSTANCE = new CasEnvConfig();
+
+ private Map<String, String> properties = new HashMap<>();
+
+ private CasEnvConfig() {
+ init();
+ }
+
+ private void init() {
+ properties.put("CAS_APPLICATION_ID", DynamicPropertyFactory
+ .getInstance().getStringProperty(APPLICATION_ID, EMPTY).get());
+ properties.put("CAS_COMPONENT_NAME", DynamicPropertyFactory
+ .getInstance().getStringProperty(COMPONENT_NAME, EMPTY).get());
+ properties.put("CAS_INSTANCE_VERSION", DynamicPropertyFactory
+ .getInstance().getStringProperty(INSTANCE_VERSION, EMPTY).get());
+ properties.put("CAS_INSTANCE_ID", DynamicPropertyFactory
+ .getInstance().getStringProperty(INSTANCE_ID, EMPTY).get());
+ properties.put("CAS_ENVIRONMENT_ID", DynamicPropertyFactory
+ .getInstance().getStringProperty(ENVIRONMENT_ID, EMPTY).get());
+ }
+
+ public Map<String, String> getNonEmptyProperties() {
+ return properties.entrySet().stream().filter(entry -> StringUtils.isNotEmpty(entry.getValue()))
+ .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
+ }
+}
diff --git a/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvVariablesAdapter.java b/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvVariablesAdapter.java
new file mode 100644
index 0000000..ecbd12e
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/main/java/org/apache/servicecomb/cas/CasEnvVariablesAdapter.java
@@ -0,0 +1,40 @@
+/*
+ * 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.cas;
+
+import org.apache.servicecomb.serviceregistry.adapter.EnvAdapter;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class CasEnvVariablesAdapter implements EnvAdapter {
+ private static final String NAME = "cas-env-variables-adapter";
+
+ @Override
+ public String getEnvName() {
+ return NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+
+ @Override
+ public void beforeRegisterInstance(MicroserviceInstance instance) {
+ instance.getProperties().putAll(CasEnvConfig.INSTANCE.getNonEmptyProperties());
+ }
+}
diff --git a/third-part-extensions/cas-env-variables/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter b/third-part-extensions/cas-env-variables/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
new file mode 100644
index 0000000..88bdc59
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.adapter.EnvAdapter
@@ -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.cas.CasEnvVariablesAdapter
\ No newline at end of file
diff --git a/third-part-extensions/cas-env-variables/src/main/resources/mapping.yaml b/third-part-extensions/cas-env-variables/src/main/resources/mapping.yaml
new file mode 100644
index 0000000..f986613
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/main/resources/mapping.yaml
@@ -0,0 +1,41 @@
+#
+# 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.
+#
+CAS_APPLICATION_ID:
+ - servicecomb.cas.application-id
+CAS_COMPONENT_NAME:
+ - servicecomb.cas.component-name
+CAS_INSTANCE_VERSION:
+ - servicecomb.cas.instance-version
+CAS_INSTANCE_ID:
+ - servicecomb.cas.instance-id
+CAS_ENVIRONMENT_ID:
+ - servicecomb.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:
+ - servicecomb.monitor.client.sslEnabled
+servicecomb.engine.engineName:
+ - servicecomb.engine.name
diff --git a/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestCasEnvConfig.java b/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestCasEnvConfig.java
new file mode 100644
index 0000000..211bf96
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestCasEnvConfig.java
@@ -0,0 +1,46 @@
+/*
+ * 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.cas;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestCasEnvConfig {
+
+ @BeforeClass
+ public static void init() {
+ System.setProperty("servicecomb.cas.application-id", "application-id");
+ System.setProperty("servicecomb.cas.environment-id", "env-id");
+ }
+
+ @Test
+ public void testConfig() {
+ CasEnvConfig instance = CasEnvConfig.INSTANCE;
+ assertEquals(2, instance.getNonEmptyProperties().size());
+ assertEquals("application-id", instance.getNonEmptyProperties().get("CAS_APPLICATION_ID"));
+ assertEquals("env-id", instance.getNonEmptyProperties().get("CAS_ENVIRONMENT_ID"));
+ }
+
+ @AfterClass
+ public static void destroy() {
+ System.getProperties().remove("servicecomb.cas.application-id");
+ System.getProperties().remove("servicecomb.cas.environment-id");
+ }
+}
diff --git a/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestEnvVariablesAdapter.java b/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestEnvVariablesAdapter.java
new file mode 100644
index 0000000..0aec4cb
--- /dev/null
+++ b/third-part-extensions/cas-env-variables/src/test/java/org/apache/servicecomb/cas/TestEnvVariablesAdapter.java
@@ -0,0 +1,50 @@
+/*
+ * 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.cas;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestEnvVariablesAdapter {
+
+ @BeforeClass
+ public static void init() {
+ System.setProperty("servicecomb.cas.application-id", "application-id");
+ System.setProperty("servicecomb.cas.environment-id", "env-id");
+ }
+
+ @Test
+ public void testProcessInstance() {
+ CasEnvVariablesAdapter adapter = new CasEnvVariablesAdapter();
+ MicroserviceInstance instance = new MicroserviceInstance();
+ adapter.beforeRegisterInstance(instance);
+
+ assertEquals(2, instance.getProperties().size());
+ assertEquals("application-id", instance.getProperties().get("CAS_APPLICATION_ID"));
+ assertEquals("env-id", instance.getProperties().get("CAS_ENVIRONMENT_ID"));
+ }
+
+ @AfterClass
+ public static void destroy() {
+ System.getProperties().remove("servicecomb.cas.application-id");
+ System.getProperties().remove("servicecomb.cas.environment-id");
+ }
+}
diff --git a/third-part-extensions/pom.xml b/third-part-extensions/pom.xml
new file mode 100644
index 0000000..ee2cce6
--- /dev/null
+++ b/third-part-extensions/pom.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ 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>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>java-chassis-parent</artifactId>
+ <version>2.0.2-SNAPSHOT</version>
+ <relativePath>../parents/default</relativePath>
+ </parent>
+
+ <artifactId>third-part-extensions</artifactId>
+ <name>Java Chassis::Third-Part Extensions</name>
+ <packaging>pom</packaging>
+ <modelVersion>4.0.0</modelVersion>
+
+ <modules>
+ <module>cas-env-variables</module>
+ </modules>
+
+</project>
\ No newline at end of file