From 787e562d234e1310950458463c07b1cd020e0a1a Mon Sep 17 00:00:00 2001 From: liubao Date: Thu, 3 Nov 2022 23:25:24 +0800 Subject: [PATCH 1/2] [SCB-2716]enable swagger override by default and allow microservcie info update --- .../src/main/resources/microservice.yaml | 2 +- .../consumer/MicroserviceVersions.java | 8 +++--- .../config/ServiceRegistryConfigBuilder.java | 7 +++--- .../task/TestMicroserviceRegisterTask.java | 25 +++++++++++++------ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/demo/demo-edge/edge-service/src/main/resources/microservice.yaml b/demo/demo-edge/edge-service/src/main/resources/microservice.yaml index 544cbcf1d14..1813c3f1033 100644 --- a/demo/demo-edge/edge-service/src/main/resources/microservice.yaml +++ b/demo/demo-edge/edge-service/src/main/resources/microservice.yaml @@ -29,7 +29,7 @@ servicecomb: enabled: true allowedHeaders: xxx rest: - address: 0.0.0.0:18080 + address: 0.0.0.0:18090 server: # for test case run in one core machine verticle-count: 1 diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java index 6d0a941594d..da5f99cb564 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java @@ -229,10 +229,10 @@ private void setInstances(List pulledInstances, String rev // clear cache versions.forEach((key, value) -> value.setInstances(new ArrayList<>())); for (Entry> entry : mergedInstances.microserviceIdMap.entrySet()) { - // ensure microserviceVersion exists - versions.computeIfAbsent(entry.getKey(), - microserviceId -> createMicroserviceVersion(microserviceId, entry.getValue())) - .setInstances(entry.getValue()); + // always update microservice versions, because we allow microservice info override, like schema info + MicroserviceVersion newVersion = createMicroserviceVersion(entry.getKey(), entry.getValue()); + newVersion.setInstances(entry.getValue()); + versions.put(entry.getKey(), newVersion); } for (MicroserviceVersionRule microserviceVersionRule : versionRules.values()) { diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java index 1ebb7212a40..ae6b7fd080f 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java @@ -223,13 +223,14 @@ public int getResendHeartBeatTimes() { return times < 0 ? ServiceRegistryConfig.DEFAULT_CHECK_TIMES : times; } - // 配置项供开发测试使用,减少接口频繁变更情况下,需要修改版本号的工作量。 - // 生产环境不能打开这个配置项,否则会导致网关、consumer等在provider之前启动的场景下,访问不到新增、更新的接口。 + // Setting override schema to true, must also set OperationInstancesDiscoveryFilter false. + // Or when new version add some services, the gateway will not update its route and can not + // route to the target service. public boolean isAlwaysOverrideSchema() { DynamicBooleanProperty property = DynamicPropertyFactory.getInstance() .getBooleanProperty("servicecomb.service.registry.instance.alwaysOverrideSchema", - false); + true); return property.get(); } diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java index d1cabe09644..aa0d1349ce6 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java @@ -32,13 +32,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import mockit.Expectations; import mockit.Mocked; -import org.junit.jupiter.api.Assertions; public class TestMicroserviceRegisterTask { private EventBus eventBus; @@ -225,7 +225,8 @@ public void testAlreadyRegisteredSchemaIdSetMatch(@Mocked ServiceRegistryClient } @Test(expected = IllegalStateException.class) - public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClient srClient) { + public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClient srClient, + @Mocked ServiceRegistryConfig config) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); @@ -240,6 +241,8 @@ public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClie schemaResponse.setSchemaId("s1"); new Expectations() { { + config.isAlwaysOverrideSchema(); + result = false; srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); @@ -366,7 +369,7 @@ public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) { * There is schema in service center which is different from local schema. */ @Test(expected = IllegalStateException.class) - public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { + public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient, @Mocked ServiceRegistryConfig config) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); @@ -382,6 +385,8 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { new Expectations() { { + config.isAlwaysOverrideSchema(); + result = false; srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); @@ -401,7 +406,8 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { * env = production and there are schemas only existing in service center */ @Test(expected = IllegalStateException.class) - public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) { + public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient, + @Mocked ServiceRegistryConfig config) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); @@ -420,6 +426,8 @@ public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegist new Expectations() { { + config.isAlwaysOverrideSchema(); + result = false; srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); @@ -489,8 +497,8 @@ public void testReRegisterForDevAndLocalSchemasAreLess(@Mocked ServiceRegistryCl * There is schema in service center which is different from local schema. */ @Test - public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryClient srClient) { - + public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryClient srClient, + @Mocked ServiceRegistryConfig config) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); @@ -506,6 +514,8 @@ public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryCli new Expectations() { { + config.isAlwaysOverrideSchema(); + result = false; srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); @@ -602,7 +612,8 @@ public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryCli registerTask.run(); } catch (IllegalStateException exception) { isIllegalException = true; - List events = collector.getEvents().stream().filter(e -> MicroserviceRegisterTask.class.getName().equals(e.getLoggerName())).collect(Collectors.toList()); + List events = collector.getEvents().stream() + .filter(e -> MicroserviceRegisterTask.class.getName().equals(e.getLoggerName())).collect(Collectors.toList()); Assertions.assertEquals("service center schema and local schema both are different:\n" + " service center schema:\n" + From 76c0353b2808e125feefb0719e7b30ea3be37594 Mon Sep 17 00:00:00 2001 From: liubao Date: Fri, 4 Nov 2022 14:29:43 +0800 Subject: [PATCH 2/2] [SCB-2716]fix test case --- demo/docker-run-config-edge/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/docker-run-config-edge/pom.xml b/demo/docker-run-config-edge/pom.xml index f861e14e887..83839c38e9d 100644 --- a/demo/docker-run-config-edge/pom.xml +++ b/demo/docker-run-config-edge/pom.xml @@ -218,13 +218,13 @@ --> - 18080 + 18090 - 18080:18080 + 18090:18090 service-center