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

[servicecomb-java-chassis] branch master updated: [SCB-2124]add a version rule switch when use lastest version rule and TESTING status to pulish service

This is an automated email from the ASF dual-hosted git repository.

wujimin 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 5924f00  [SCB-2124]add a version rule switch when use lastest version rule and TESTING status to pulish service
5924f00 is described below

commit 5924f009c95358e39b9359cfedb4b897d603ebcf
Author: liubao <bi...@qq.com>
AuthorDate: Tue Nov 24 11:59:43 2020 +0800

    [SCB-2124]add a version rule switch when use lastest version rule and TESTING status to pulish service
---
 .../registry/config/ServiceRegistryCommonConfig.java      |  9 +++++++++
 .../registry/consumer/MicroserviceVersions.java           | 15 +++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/config/ServiceRegistryCommonConfig.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/config/ServiceRegistryCommonConfig.java
index bda0542..f307164 100644
--- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/config/ServiceRegistryCommonConfig.java
+++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/config/ServiceRegistryCommonConfig.java
@@ -22,10 +22,19 @@ import com.netflix.config.DynamicPropertyFactory;
 public class ServiceRegistryCommonConfig {
   private static final String REGISTRY_EMPTY_PROTECTION = "servicecomb.service.registry.instance.empty.protection";
 
+  private static final String REGISTRY_FILTER_UP_INSTANCES = "servicecomb.service.registry.instance.useUpInstancesOnly";
+
   public static boolean isEmptyInstanceProtectionEnabled() {
     return
         DynamicPropertyFactory.getInstance()
             .getBooleanProperty(REGISTRY_EMPTY_PROTECTION,
                 true).get();
   }
+
+  public static boolean useUpInstancesOnly() {
+    return
+        DynamicPropertyFactory.getInstance()
+            .getBooleanProperty(REGISTRY_FILTER_UP_INSTANCES,
+                false).get();
+  }
 }
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 a5c0028..ce43bf3 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
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.stream.Collectors;
 
 import org.apache.servicecomb.foundation.common.VendorExtensions;
 import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
@@ -33,6 +34,7 @@ import org.apache.servicecomb.registry.api.event.DestroyMicroserviceEvent;
 import org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent;
 import org.apache.servicecomb.registry.api.event.task.SafeModeChangeEvent;
 import org.apache.servicecomb.registry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.registry.api.registry.MicroserviceInstanceStatus;
 import org.apache.servicecomb.registry.api.registry.MicroserviceInstances;
 import org.apache.servicecomb.registry.config.ServiceRegistryCommonConfig;
 import org.apache.servicecomb.registry.definition.DefinitionConst;
@@ -184,7 +186,16 @@ public class MicroserviceVersions {
 
   protected void safeSetInstances(List<MicroserviceInstance> pulledInstances, String rev) {
     try {
-      setInstances(pulledInstances, rev);
+      List<MicroserviceInstance> filteredInstance = pulledInstances;
+      // 增加一个配置项只使用 `UP` 实例。 在使用 `TESTING` 进行拨测, 并且配置了
+      // servicecomb.references.version-rule=latest 场景,需要保证不使用
+      // `TESTING` 实例。 不能依赖 InstanceStatusDiscoveryFilter, 避免
+      // 构建的 VersionRule 实例列表为空。
+      if (ServiceRegistryCommonConfig.useUpInstancesOnly()) {
+        filteredInstance = pulledInstances.stream().filter(item -> MicroserviceInstanceStatus.UP == item.getStatus())
+            .collect(Collectors.toList());
+      }
+      setInstances(filteredInstance, rev);
     } catch (Throwable e) {
       waitingDelete = true;
       LOGGER.error("Failed to setInstances, appId={}, microserviceName={}.",
@@ -209,7 +220,7 @@ public class MicroserviceVersions {
     }
   }
 
-  protected void setInstances(List<MicroserviceInstance> pulledInstances, String rev) {
+  private void setInstances(List<MicroserviceInstance> pulledInstances, String rev) {
     synchronized (lock) {
       MergedInstances mergedInstances = mergeInstances(pulledInstances, instances);
       instances = mergedInstances.instanceIdMap.values();