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/06/10 03:06:47 UTC
[servicecomb-java-chassis] branch master updated: [SCB-1991]support
dynamic register service in local part 1: support bean registration
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 1c4f7c6 [SCB-1991]support dynamic register service in local part 1: support bean registration
1c4f7c6 is described below
commit 1c4f7c61d70bdac7df6f8ab2adc0f020c1d5db73
Author: liubao <bi...@qq.com>
AuthorDate: Tue Jun 9 20:49:33 2020 +0800
[SCB-1991]support dynamic register service in local part 1: support bean registration
---
coverage-reports/pom.xml | 19 +++
demo/demo-local-registry/README.md | 2 +-
.../demo/localRegistryClient/Application.java | 3 +
.../LocalRegistryServerTest.java | 22 ++-
.../RegistryBeansConfiguration.java | 49 ++++++
.../ServerEndpoint.yaml | 43 +++++
.../localregistry/LocalRegistryStore.java | 123 +++++++-------
.../servicecomb/localregistry/RegistryBean.java | 178 +++++++++++++++++++++
8 files changed, 357 insertions(+), 82 deletions(-)
diff --git a/coverage-reports/pom.xml b/coverage-reports/pom.xml
index 133390a..ae7e00d 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -498,6 +498,25 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <!-- demo zeroconfig shcema discovery -->
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-zeroconfig-schemadiscovery-registry-client</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-zeroconfig-schemadiscovery-registry-server</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-zeroconfig-schemadiscovery-registry-tests</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
<!-- test cases in integration-test folder, sort by folder name -->
<dependency>
diff --git a/demo/demo-local-registry/README.md b/demo/demo-local-registry/README.md
index 6bb7dd8..7ace9bd 100644
--- a/demo/demo-local-registry/README.md
+++ b/demo/demo-local-registry/README.md
@@ -1,6 +1,6 @@
This demo is an integration test for testing local registration an discovery.
-Local registration an discovery is an mechanism that do not using Service Center,
+Local registration and discovery is a mechanism that do not using Service Center,
`Microservice` and `MicroserviceInstance` are constructed from configuration file.
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
index d54220a..c6ed407 100644
--- a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
@@ -45,6 +45,9 @@ public class Application {
RegistrationManager.INSTANCE.getSwaggerLoader().registerSwagger("demo-local-registry",
"demo-local-registry-server",
"CodeFirstEndpoint", CodeFirstService.class);
+ RegistrationManager.INSTANCE.getSwaggerLoader().registerSwagger("demo-local-registry",
+ "demo-local-registry-server-bean",
+ "CodeFirstEndpoint", CodeFirstService.class);
}
private static void runTest() throws Exception {
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
index 058f091..759fd87 100644
--- a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
@@ -33,6 +33,9 @@ public class LocalRegistryServerTest implements CategorizedTestCase {
@RpcReference(microserviceName = "demo-local-registry-server", schemaId = "CodeFirstEndpoint")
private CodeFirstService codeFirstService;
+ @RpcReference(microserviceName = "demo-local-registry-server-bean", schemaId = "CodeFirstEndpoint")
+ private CodeFirstService codeFirstServiceBean;
+
RestTemplate template = RestTemplateBuilder.create();
@Override
@@ -48,15 +51,17 @@ public class LocalRegistryServerTest implements CategorizedTestCase {
for (Microservice m : microserviceList) {
if (m.getServiceName().equals("demo-local-registry-client")
- || m.getServiceName().equals("demo-local-registry-server")) {
+ || m.getServiceName().equals("demo-local-registry-server")
+ || m.getServiceName().equals("demo-local-registry-server-bean")) {
expectedCount++;
}
}
- TestMgr.check(2, expectedCount);
+ TestMgr.check(3, expectedCount);
}
private void testCodeFirstGetName() {
TestMgr.check("2", codeFirstService.getName("2"));
+ TestMgr.check("2", codeFirstServiceBean.getName("2"));
}
private void testServerGetName() {
@@ -64,15 +69,8 @@ public class LocalRegistryServerTest implements CategorizedTestCase {
TestMgr.check("2", template
.getForObject("cse://demo-local-registry-server/register/url/prefix/getName?name=2",
String.class));
- }
-
- @Override
- public void testHighwayTransport() throws Exception {
-
- }
-
- @Override
- public void testAllTransport() throws Exception {
-
+ TestMgr.check("2", template
+ .getForObject("cse://demo-local-registry-server-bean/register/url/prefix/getName?name=2",
+ String.class));
}
}
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/RegistryBeansConfiguration.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/RegistryBeansConfiguration.java
new file mode 100644
index 0000000..12ac101
--- /dev/null
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/RegistryBeansConfiguration.java
@@ -0,0 +1,49 @@
+/*
+ * 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.demo.localRegistryClient;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.servicecomb.localregistry.RegistryBean;
+import org.apache.servicecomb.localregistry.RegistryBean.Instance;
+import org.apache.servicecomb.localregistry.RegistryBean.Instances;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class RegistryBeansConfiguration {
+ @Bean
+ public RegistryBean demoLocalRegistryServerBean() {
+ List<String> schemaIds = new ArrayList<>();
+ schemaIds.add("ServerEndpoint");
+ schemaIds.add("CodeFirstEndpoint");
+ List<String> endpoints = new ArrayList<>();
+ endpoints.add("rest://localhost:8080");
+ List<Instance> instances = new ArrayList<>();
+ instances.add(new Instance().setEndpoints(endpoints));
+
+ return new RegistryBean()
+ .setServiceName("demo-local-registry-server-bean")
+ .setId("002")
+ .setVersion("0.0.3")
+ .setAppId("demo-local-registry")
+ .setSchemaIds(schemaIds)
+ .setInstances(new Instances().setInstances(instances));
+ }
+}
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/resources/microservices/demo-local-registry-server-bean/ServerEndpoint.yaml b/demo/demo-local-registry/demo-local-registry-client/src/main/resources/microservices/demo-local-registry-server-bean/ServerEndpoint.yaml
new file mode 100644
index 0000000..78c9d1b
--- /dev/null
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/resources/microservices/demo-local-registry-server-bean/ServerEndpoint.yaml
@@ -0,0 +1,43 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+swagger: "2.0"
+info:
+ version: "1.0.0"
+ title: "swagger definition for org.apache.servicecomb.demo.registry.ServerEndpoint"
+ x-java-interface: "gen.swagger.ServerEndpointIntf"
+basePath: "/register/url/prefix"
+schemes:
+- "http"
+consumes:
+- "application/json"
+produces:
+- "application/json"
+paths:
+ /getName:
+ get:
+ operationId: "getName"
+ parameters:
+ - name: "name"
+ in: "query"
+ required: true
+ type: "string"
+ responses:
+ "200":
+ description: "response of 200"
+ schema:
+ type: "string"
\ No newline at end of file
diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java
index 6e22f18..ff37c0f 100644
--- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java
+++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java
@@ -17,8 +17,6 @@
package org.apache.servicecomb.localregistry;
-import static org.apache.servicecomb.registry.definition.DefinitionConst.DEFAULT_APPLICATION_ID;
-
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -32,11 +30,11 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
-import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.ConfigUtil;
import org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader;
-import org.apache.servicecomb.foundation.common.base.ServiceCombConstants;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.JvmUtils;
+import org.apache.servicecomb.localregistry.RegistryBean.Instance;
import org.apache.servicecomb.registry.api.registry.FindInstancesResponse;
import org.apache.servicecomb.registry.api.registry.Microservice;
import org.apache.servicecomb.registry.api.registry.MicroserviceFactory;
@@ -46,7 +44,6 @@ import org.apache.servicecomb.registry.definition.MicroserviceDefinition;
import org.yaml.snakeyaml.Yaml;
import com.google.common.annotations.VisibleForTesting;
-import com.netflix.config.DynamicPropertyFactory;
public class LocalRegistryStore {
private static final String REGISTRY_FILE_NAME = "registry.yaml";
@@ -90,6 +87,33 @@ public class LocalRegistryStore {
selfMicroserviceInstance.setInstanceId(selfMicroservice.getServiceId());
selfMicroserviceInstance.setServiceId(selfMicroservice.getServiceId());
+ List<RegistryBean> beans = loadYamlBeans();
+ BeanUtils.getBeansOfType(RegistryBean.class).entrySet()
+ .forEach(entry -> beans.add(entry.getValue()));
+
+ initRegistryFromBeans(beans);
+
+ addSelf();
+ }
+
+ private void addSelf() {
+ microserviceMap.put(selfMicroservice.getServiceId(), selfMicroservice);
+ Map<String, MicroserviceInstance> selfInstanceMap = new HashMap<>(1);
+ selfInstanceMap.put(selfMicroserviceInstance.getInstanceId(), selfMicroserviceInstance);
+ microserviceInstanceMap.put(selfMicroservice.getServiceId(), selfInstanceMap);
+ }
+
+ public Microservice getSelfMicroservice() {
+ return selfMicroservice;
+ }
+
+ public MicroserviceInstance getSelfMicroserviceInstance() {
+ return selfMicroserviceInstance;
+ }
+
+ private List<RegistryBean> loadYamlBeans() {
+ List<RegistryBean> beans = new ArrayList<>();
+
InputStream is = null;
try {
@@ -99,7 +123,7 @@ public class LocalRegistryStore {
URL url = urls.nextElement();
is = url.openStream();
if (is != null) {
- initFromData(is);
+ beans.addAll(initFromData(is));
}
}
} catch (IOException e) {
@@ -114,91 +138,52 @@ public class LocalRegistryStore {
}
}
- addSelf();
+ return beans;
}
- private void addSelf() {
- microserviceMap.put(selfMicroservice.getServiceId(), selfMicroservice);
- Map<String, MicroserviceInstance> selfInstanceMap = new HashMap<>(1);
- selfInstanceMap.put(selfMicroserviceInstance.getInstanceId(), selfMicroserviceInstance);
- microserviceInstanceMap.put(selfMicroservice.getServiceId(), selfInstanceMap);
- }
-
- public Microservice getSelfMicroservice() {
- return selfMicroservice;
- }
-
- public MicroserviceInstance getSelfMicroserviceInstance() {
- return selfMicroserviceInstance;
- }
-
- private void initFromData(InputStream is) {
+ @SuppressWarnings("unchecked")
+ private List<RegistryBean> initFromData(InputStream is) {
Yaml yaml = new Yaml();
- @SuppressWarnings("unchecked")
Map<String, Object> data = yaml.loadAs(is, Map.class);
- initFromData(data);
+ return initFromData(data);
}
- private void initFromData(Map<String, Object> data) {
+ @SuppressWarnings("unchecked")
+ private List<RegistryBean> initFromData(Map<String, Object> data) {
+ List<RegistryBean> beans = new ArrayList<>();
+
for (Entry<String, Object> entry : data.entrySet()) {
String name = entry.getKey();
- @SuppressWarnings("unchecked")
List<Map<String, Object>> serviceConfigs = (List<Map<String, Object>>) entry.getValue();
for (Map<String, Object> serviceConfig : serviceConfigs) {
- @SuppressWarnings("unchecked")
- List<Map<String, Object>> instancesConfig =
- (List<Map<String, Object>>) serviceConfig.get("instances");
-
- String appId = (String) serviceConfig.get("appid");
- String version = (String) serviceConfig.get("version");
- String serviceId = (String) serviceConfig.get("id");
- @SuppressWarnings("unchecked")
- List<String> schemas = (List<String>) serviceConfig.get("schemaIds");
-
- Microservice microservice = new Microservice();
- microservice.setAppId(validAppId(appId));
- microservice.setServiceName(name);
- microservice.setVersion(version);
- microservice.setServiceId(serviceId == null ? UUID.randomUUID().toString() : serviceId);
- microserviceMap.put(microservice.getServiceId(), microservice);
- if (schemas != null) {
- microservice.setSchemas(schemas);
- }
-
- addInstances(instancesConfig, microservice);
+ beans.add(RegistryBean.buildFromYamlModel(name, serviceConfig));
}
}
+ return beans;
}
- private String validAppId(String configAppId) {
- if (!StringUtils.isEmpty(configAppId)) {
- return configAppId;
- }
- if (DynamicPropertyFactory.getInstance()
- .getStringProperty(ServiceCombConstants.CONFIG_APPLICATION_ID_KEY, null).get() != null) {
- return DynamicPropertyFactory.getInstance()
- .getStringProperty(ServiceCombConstants.CONFIG_APPLICATION_ID_KEY, null).get();
- }
- return DEFAULT_APPLICATION_ID;
+ private void initRegistryFromBeans(List<RegistryBean> beans) {
+ beans.forEach((bean -> {
+ Microservice microservice = new Microservice();
+ microservice.setAppId(bean.getAppId());
+ microservice.setServiceName(bean.getServiceName());
+ microservice.setVersion(bean.getVersion());
+ microservice.setServiceId(bean.getId());
+ microservice.setSchemas(bean.getSchemaIds());
+ microserviceMap.put(microservice.getServiceId(), microservice);
+ addInstances(bean, microservice);
+ }));
}
- private void addInstances(List<Map<String, Object>> instancesConfig, Microservice microservice) {
+ private void addInstances(RegistryBean bean, Microservice microservice) {
Map<String, MicroserviceInstance> instanceMap = new ConcurrentHashMap<>();
microserviceInstanceMap.put(microservice.getServiceId(), instanceMap);
- if (instancesConfig == null) {
- return;
- }
-
- for (Map<String, Object> instanceConfig : instancesConfig) {
- @SuppressWarnings("unchecked")
- List<String> endpoints = (List<String>) instanceConfig.get("endpoints");
-
+ for (Instance item : bean.getInstances().getInstances()) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId(UUID.randomUUID().toString());
- instance.setEndpoints(endpoints);
+ instance.setEndpoints(item.getEndpoints());
instance.setServiceId(microservice.getServiceId());
-
instanceMap.put(instance.getInstanceId(), instance);
}
}
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
new file mode 100644
index 0000000..6a71aa4
--- /dev/null
+++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/RegistryBean.java
@@ -0,0 +1,178 @@
+/*
+ * 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.localregistry;
+
+import static org.apache.servicecomb.registry.definition.DefinitionConst.DEFAULT_APPLICATION_ID;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.foundation.common.base.ServiceCombConstants;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+/**
+ * Configuration bean for local services. Bean configuration is token
+ * same as `registry.yaml` file configuration.
+ *
+ */
+public class RegistryBean {
+ public static class Instances {
+ private List<Instance> instances;
+
+ public List<Instance> getInstances() {
+ return instances;
+ }
+
+ public Instances setInstances(List<Instance> instances) {
+ this.instances = instances;
+ return this;
+ }
+ }
+
+ public static class Instance {
+ private List<String> endpoints;
+
+ public List<String> getEndpoints() {
+ return endpoints;
+ }
+
+ public Instance setEndpoints(List<String> endpoints) {
+ this.endpoints = endpoints;
+ return this;
+ }
+ }
+
+ private String id;
+
+ private String serviceName;
+
+ private String version;
+
+ private String appId;
+
+ private List<String> schemaIds;
+
+ private Instances instances;
+
+ @SuppressWarnings("unchecked")
+ public static RegistryBean buildFromYamlModel
+ (String serviceName, Map<String, Object> serviceConfig) {
+ return new RegistryBean()
+ .setId(validId((String) serviceConfig.get("id")))
+ .setServiceName(serviceName)
+ .setVersion((String) serviceConfig.get("version"))
+ .setAppId(validAppId((String) serviceConfig.get("appid")))
+ .setSchemaIds(validListsValue((List<String>) serviceConfig.get("schemaIds")))
+ .setInstances(
+ new Instances()
+ .setInstances(validInstances((List<Map<String, Object>>) serviceConfig.get("instances"))));
+ }
+
+ @SuppressWarnings("unchecked")
+ private static List<Instance> validInstances(List<Map<String, Object>> instancesConfig) {
+ if (instancesConfig == null) {
+ return Collections.emptyList();
+ }
+
+ List<Instance> instances = new ArrayList<>();
+ for (Map<String, Object> instanceConfig : instancesConfig) {
+ instances.add(new Instance().setEndpoints(
+ validListsValue((List<String>) instanceConfig.get("endpoints"))));
+ }
+ return instances;
+ }
+
+ private static List<String> validListsValue(List<String> listsValue) {
+ return listsValue == null ? Collections.emptyList() : listsValue;
+ }
+
+ private static String validId(String serviceId) {
+ return StringUtils.isEmpty(serviceId) ? UUID.randomUUID().toString() : serviceId;
+ }
+
+ private static String validAppId(String configAppId) {
+ if (!StringUtils.isEmpty(configAppId)) {
+ return configAppId;
+ }
+ if (DynamicPropertyFactory.getInstance()
+ .getStringProperty(ServiceCombConstants.CONFIG_APPLICATION_ID_KEY, null).get() != null) {
+ return DynamicPropertyFactory.getInstance()
+ .getStringProperty(ServiceCombConstants.CONFIG_APPLICATION_ID_KEY, null).get();
+ }
+ return DEFAULT_APPLICATION_ID;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public RegistryBean setId(String id) {
+ this.id = id;
+ return this;
+ }
+
+ public String getServiceName() {
+ return serviceName;
+ }
+
+ public RegistryBean setServiceName(String serviceName) {
+ this.serviceName = serviceName;
+ return this;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public RegistryBean setVersion(String version) {
+ this.version = version;
+ return this;
+ }
+
+ public String getAppId() {
+ return appId;
+ }
+
+ public RegistryBean setAppId(String appId) {
+ this.appId = appId;
+ return this;
+ }
+
+ public List<String> getSchemaIds() {
+ return schemaIds;
+ }
+
+ public RegistryBean setSchemaIds(List<String> schemaIds) {
+ this.schemaIds = schemaIds;
+ return this;
+ }
+
+ public Instances getInstances() {
+ return instances;
+ }
+
+ public RegistryBean setInstances(Instances instances) {
+ this.instances = instances;
+ return this;
+ }
+}