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 2023/05/09 07:33:24 UTC

[servicecomb-java-chassis] branch 2.8.x updated: [SCB-2792]router should get properties from instance to decide the route policy (#3781)

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

liubao pushed a commit to branch 2.8.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/2.8.x by this push:
     new be2404061 [SCB-2792]router should get properties from instance to decide the route policy (#3781)
be2404061 is described below

commit be24040618c4c4af7ce55c5be73fbda9bbf84871
Author: liubao68 <bi...@qq.com>
AuthorDate: Tue May 9 15:33:18 2023 +0800

    [SCB-2792]router should get properties from instance to decide the route policy (#3781)
---
 .../apache/servicecomb/router/RouterFilter.java    |  6 +--
 .../distribute/AbstractRouterDistributor.java      | 60 ++++++++++------------
 .../router/distribute/RouterDistributor.java       | 11 ++--
 .../servicecomb/router/ExampleDistributor.java     |  4 +-
 .../RouterDistributorDynamicConfig2Test.java       |  4 +-
 .../router/RouterDistributorDynamicConfigTest.java |  4 +-
 .../router/RouterDistributorFileConfigTest.java    |  4 +-
 ...nvokeFilter.java => RouterAddHeaderFilter.java} |  4 +-
 .../router/custom/RouterServerListFilter.java      |  3 +-
 ...uter.java => ServiceCombRouterDistributor.java} | 27 ++++++----
 ...servicecomb.common.rest.filter.HttpServerFilter |  2 +-
 11 files changed, 66 insertions(+), 63 deletions(-)

diff --git a/governance/src/main/java/org/apache/servicecomb/router/RouterFilter.java b/governance/src/main/java/org/apache/servicecomb/router/RouterFilter.java
index 7e48b9c00..549123b14 100644
--- a/governance/src/main/java/org/apache/servicecomb/router/RouterFilter.java
+++ b/governance/src/main/java/org/apache/servicecomb/router/RouterFilter.java
@@ -45,8 +45,8 @@ public class RouterFilter {
     this.routerRuleCache = routerRuleCache;
   }
 
-  public <T, E> List<T> getFilteredListOfServers(List<T> list,
-      String targetServiceName, Map<String, String> headers, RouterDistributor<T, E> distributer) {
+  public <T> List<T> getFilteredListOfServers(List<T> list,
+      String targetServiceName, Map<String, String> headers, RouterDistributor<T> distributor) {
     if (CollectionUtils.isEmpty(list)) {
       return list;
     }
@@ -69,7 +69,7 @@ public class RouterFilter {
     LOGGER.debug("route management match rule success: {}", invokeRule);
 
     // 3.distribute select endpoint
-    List<T> resultList = distributer.distribute(targetServiceName, list, invokeRule);
+    List<T> resultList = distributor.distribute(targetServiceName, list, invokeRule);
 
     LOGGER.debug("route management distribute rule success: {}", resultList);
 
diff --git a/governance/src/main/java/org/apache/servicecomb/router/distribute/AbstractRouterDistributor.java b/governance/src/main/java/org/apache/servicecomb/router/distribute/AbstractRouterDistributor.java
index d3f8f7640..4de2e6a72 100644
--- a/governance/src/main/java/org/apache/servicecomb/router/distribute/AbstractRouterDistributor.java
+++ b/governance/src/main/java/org/apache/servicecomb/router/distribute/AbstractRouterDistributor.java
@@ -33,18 +33,16 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 
-public abstract class AbstractRouterDistributor<T, E> implements
-    RouterDistributor<T, E> {
+public abstract class AbstractRouterDistributor<INSTANCE> implements
+    RouterDistributor<INSTANCE> {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRouterDistributor.class);
 
-  private Function<T, E> getIns;
+  private Function<INSTANCE, String> getVersion;
 
-  private Function<E, String> getVersion;
+  private Function<INSTANCE, String> getServerName;
 
-  private Function<E, String> getServerName;
-
-  private Function<E, Map<String, String>> getProperties;
+  private Function<INSTANCE, Map<String, String>> getProperties;
 
   private RouterRuleCache routerRuleCache;
 
@@ -57,7 +55,7 @@ public abstract class AbstractRouterDistributor<T, E> implements
   }
 
   @Override
-  public List<T> distribute(String targetServiceName, List<T> list, PolicyRuleItem invokeRule) {
+  public List<INSTANCE> distribute(String targetServiceName, List<INSTANCE> list, PolicyRuleItem invokeRule) {
     //init LatestVersion
     initLatestVersion(targetServiceName, list);
 
@@ -65,7 +63,7 @@ public abstract class AbstractRouterDistributor<T, E> implements
         routerRuleCache.getServiceInfoCacheMap().get(targetServiceName).getLatestVersionTag());
 
     // get tag list
-    Map<TagItem, List<T>> versionServerMap = getDistributList(targetServiceName, list, invokeRule);
+    Map<TagItem, List<INSTANCE>> versionServerMap = getDistributList(targetServiceName, list, invokeRule);
 
     if (CollectionUtils.isEmpty(versionServerMap)) {
       LOGGER.debug("route management can not match any rule and route the latest version");
@@ -80,11 +78,9 @@ public abstract class AbstractRouterDistributor<T, E> implements
   }
 
   @Override
-  public void init(Function<T, E> getIns,
-      Function<E, String> getVersion,
-      Function<E, String> getServerName,
-      Function<E, Map<String, String>> getProperties) {
-    this.getIns = getIns;
+  public void init(Function<INSTANCE, String> getVersion,
+      Function<INSTANCE, String> getServerName,
+      Function<INSTANCE, Map<String, String>> getProperties) {
     this.getVersion = getVersion;
     this.getServerName = getServerName;
     this.getProperties = getProperties;
@@ -101,18 +97,17 @@ public abstract class AbstractRouterDistributor<T, E> implements
    *
    * the method getProperties() contains other field that we don't need.
    */
-  private Map<TagItem, List<T>> getDistributList(String serviceName,
-      List<T> list,
+  private Map<TagItem, List<INSTANCE>> getDistributList(String serviceName,
+      List<INSTANCE> list,
       PolicyRuleItem invokeRule) {
     String latestV = routerRuleCache.getServiceInfoCacheMap().get(serviceName).getLatestVersionTag()
         .getVersion();
-    Map<TagItem, List<T>> versionServerMap = new HashMap<>();
-    for (T server : list) {
+    Map<TagItem, List<INSTANCE>> versionServerMap = new HashMap<>();
+    for (INSTANCE instance : list) {
       //get server
-      E ms = getIns.apply(server);
-      if (getServerName.apply(ms).equals(serviceName)) {
+      if (getServerName.apply(instance).equals(serviceName)) {
         //most matching
-        TagItem tagItem = new TagItem(getVersion.apply(ms), getProperties.apply(ms));
+        TagItem tagItem = new TagItem(getVersion.apply(instance), getProperties.apply(instance));
         TagItem targetTag = null;
         int maxMatch = 0;
         for (RouteItem entry : invokeRule.getRoute()) {
@@ -125,19 +120,19 @@ public abstract class AbstractRouterDistributor<T, E> implements
             targetTag = entry.getTagitem();
           }
         }
-        if (invokeRule.isWeightLess() && getVersion.apply(ms).equals(latestV)) {
+        if (invokeRule.isWeightLess() && getVersion.apply(instance).equals(latestV)) {
           TagItem latestVTag = invokeRule.getRoute().get(invokeRule.getRoute().size() - 1)
               .getTagitem();
           if (!versionServerMap.containsKey(latestVTag)) {
             versionServerMap.put(latestVTag, new ArrayList<>());
           }
-          versionServerMap.get(latestVTag).add(server);
+          versionServerMap.get(latestVTag).add(instance);
         }
         if (targetTag != null) {
           if (!versionServerMap.containsKey(targetTag)) {
             versionServerMap.put(targetTag, new ArrayList<>());
           }
-          versionServerMap.get(targetTag).add(server);
+          versionServerMap.get(targetTag).add(instance);
         }
       }
     }
@@ -145,14 +140,13 @@ public abstract class AbstractRouterDistributor<T, E> implements
   }
 
 
-  public void initLatestVersion(String serviceName, List<T> list) {
+  public void initLatestVersion(String serviceName, List<INSTANCE> list) {
     String latestVersion = null;
-    for (T server : list) {
-      E ms = getIns.apply(server);
-      if (getServerName.apply(ms).equals(serviceName)) {
+    for (INSTANCE instance : list) {
+      if (getServerName.apply(instance).equals(serviceName)) {
         if (latestVersion == null || VersionCompareUtil
-            .compareVersion(latestVersion, getVersion.apply(ms)) == -1) {
-          latestVersion = getVersion.apply(ms);
+            .compareVersion(latestVersion, getVersion.apply(instance)) == -1) {
+          latestVersion = getVersion.apply(instance);
         }
       }
     }
@@ -160,11 +154,11 @@ public abstract class AbstractRouterDistributor<T, E> implements
     routerRuleCache.getServiceInfoCacheMap().get(serviceName).setLatestVersionTag(tagitem);
   }
 
-  public List<T> getLatestVersionList(List<T> list, String targetServiceName) {
+  public List<INSTANCE> getLatestVersionList(List<INSTANCE> list, String targetServiceName) {
     String latestV = routerRuleCache.getServiceInfoCacheMap().get(targetServiceName)
         .getLatestVersionTag().getVersion();
-    return list.stream().filter(server ->
-        getVersion.apply(getIns.apply(server)).equals(latestV)
+    return list.stream().filter(instance ->
+        getVersion.apply(instance).equals(latestV)
     ).collect(Collectors.toList());
   }
 }
diff --git a/governance/src/main/java/org/apache/servicecomb/router/distribute/RouterDistributor.java b/governance/src/main/java/org/apache/servicecomb/router/distribute/RouterDistributor.java
index fe5cf37b1..fa9af8fd1 100644
--- a/governance/src/main/java/org/apache/servicecomb/router/distribute/RouterDistributor.java
+++ b/governance/src/main/java/org/apache/servicecomb/router/distribute/RouterDistributor.java
@@ -19,17 +19,18 @@ package org.apache.servicecomb.router.distribute;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
+
 import org.apache.servicecomb.router.model.PolicyRuleItem;
 
 /**
  * @Author GuoYl123
  * @Date 2019/10/17
  **/
-public interface RouterDistributor<T, E> {
+public interface RouterDistributor<INSTANCE> {
 
-  void init(Function<T, E> getIns, Function<E, String> getVersion,
-      Function<E, String> getServerName,
-      Function<E, Map<String, String>> getProperties);
+  void init(Function<INSTANCE, String> getVersion,
+      Function<INSTANCE, String> getServerName,
+      Function<INSTANCE, Map<String, String>> getProperties);
 
-  List<T> distribute(String targetServiceName, List<T> list, PolicyRuleItem invokeRule);
+  List<INSTANCE> distribute(String targetServiceName, List<INSTANCE> list, PolicyRuleItem invokeRule);
 }
diff --git a/governance/src/test/java/org/apache/servicecomb/router/ExampleDistributor.java b/governance/src/test/java/org/apache/servicecomb/router/ExampleDistributor.java
index 7c6aa04f9..42fdfad03 100644
--- a/governance/src/test/java/org/apache/servicecomb/router/ExampleDistributor.java
+++ b/governance/src/test/java/org/apache/servicecomb/router/ExampleDistributor.java
@@ -21,8 +21,8 @@ import org.apache.servicecomb.router.distribute.AbstractRouterDistributor;
 import org.springframework.stereotype.Component;
 
 @Component
-public class ExampleDistributor extends AbstractRouterDistributor<ServiceIns, ServiceIns> {
+public class ExampleDistributor extends AbstractRouterDistributor<ServiceIns> {
   public ExampleDistributor() {
-    init(a -> a, ServiceIns::getVersion, ServiceIns::getServerName, ServiceIns::getTags);
+    init(ServiceIns::getVersion, ServiceIns::getServerName, ServiceIns::getTags);
   }
 }
diff --git a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfig2Test.java b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfig2Test.java
index f3f36e453..c3901878b 100644
--- a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfig2Test.java
+++ b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfig2Test.java
@@ -70,7 +70,7 @@ public class RouterDistributorDynamicConfig2Test {
 
   private RouterFilter routerFilter;
 
-  private RouterDistributor<ServiceIns, ServiceIns> testDistributor;
+  private RouterDistributor<ServiceIns> testDistributor;
 
   @Autowired
   public void setEnvironment(Environment environment) {
@@ -83,7 +83,7 @@ public class RouterDistributorDynamicConfig2Test {
   }
 
   @Autowired
-  public void setTestDistributor(RouterDistributor<ServiceIns, ServiceIns> testDistributor) {
+  public void setTestDistributor(RouterDistributor<ServiceIns> testDistributor) {
     this.testDistributor = testDistributor;
   }
 
diff --git a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfigTest.java b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfigTest.java
index f57ed6579..47228c7b8 100644
--- a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfigTest.java
+++ b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorDynamicConfigTest.java
@@ -88,7 +88,7 @@ public class RouterDistributorDynamicConfigTest {
 
   private RouterFilter routerFilter;
 
-  private RouterDistributor<ServiceIns, ServiceIns> testDistributor;
+  private RouterDistributor<ServiceIns> testDistributor;
 
   @Autowired
   public void setEnvironment(Environment environment) {
@@ -101,7 +101,7 @@ public class RouterDistributorDynamicConfigTest {
   }
 
   @Autowired
-  public void setTestDistributor(RouterDistributor<ServiceIns, ServiceIns> testDistributor) {
+  public void setTestDistributor(RouterDistributor<ServiceIns> testDistributor) {
     this.testDistributor = testDistributor;
   }
 
diff --git a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorFileConfigTest.java b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorFileConfigTest.java
index 9e5be6937..19f0f47f3 100644
--- a/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorFileConfigTest.java
+++ b/governance/src/test/java/org/apache/servicecomb/router/RouterDistributorFileConfigTest.java
@@ -36,7 +36,7 @@ public class RouterDistributorFileConfigTest {
 
   private RouterFilter routerFilter;
 
-  private RouterDistributor<ServiceIns, ServiceIns> routerDistributor;
+  private RouterDistributor<ServiceIns> routerDistributor;
 
   @Autowired
   public void setRouterFilter(RouterFilter routerFilter) {
@@ -44,7 +44,7 @@ public class RouterDistributorFileConfigTest {
   }
 
   @Autowired
-  public void setRouterDistributor(RouterDistributor<ServiceIns, ServiceIns> routerDistributor) {
+  public void setRouterDistributor(RouterDistributor<ServiceIns> routerDistributor) {
     this.routerDistributor = routerDistributor;
   }
 
diff --git a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterInvokeFilter.java b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java
similarity index 97%
rename from handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterInvokeFilter.java
rename to handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java
index b40bf543d..9c5e45646 100644
--- a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterInvokeFilter.java
+++ b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterAddHeaderFilter.java
@@ -36,9 +36,9 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.netflix.config.DynamicPropertyFactory;
 import com.netflix.config.DynamicStringProperty;
 
-public class RouterInvokeFilter implements HttpServerFilter {
+public class RouterAddHeaderFilter implements HttpServerFilter {
 
-  private static final Logger LOGGER = LoggerFactory.getLogger(RouterInvokeFilter.class);
+  private static final Logger LOGGER = LoggerFactory.getLogger(RouterAddHeaderFilter.class);
 
   private static final String SERVICECOMB_ROUTER_HEADER = "servicecomb.router.header";
 
diff --git a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterServerListFilter.java b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterServerListFilter.java
index 0e5ae7d09..b7a3cbc7a 100644
--- a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterServerListFilter.java
+++ b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/RouterServerListFilter.java
@@ -26,7 +26,6 @@ import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.loadbalance.ServerListFilterExt;
 import org.apache.servicecomb.loadbalance.ServiceCombServer;
-import org.apache.servicecomb.registry.api.registry.Microservice;
 import org.apache.servicecomb.router.RouterFilter;
 import org.apache.servicecomb.router.distribute.RouterDistributor;
 import org.slf4j.Logger;
@@ -47,7 +46,7 @@ public class RouterServerListFilter implements ServerListFilterExt {
   public static final String ROUTER_HEADER = "X-RouterContext";
 
   @SuppressWarnings("unchecked")
-  private final RouterDistributor<ServiceCombServer, Microservice> routerDistributor = BeanUtils
+  private final RouterDistributor<ServiceCombServer> routerDistributor = BeanUtils
       .getBean(RouterDistributor.class);
 
   private final RouterFilter routerFilter = BeanUtils.getBean(RouterFilter.class);
diff --git a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombCanaryDistributer.java b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombRouterDistributor.java
similarity index 56%
rename from handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombCanaryDistributer.java
rename to handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombRouterDistributor.java
index 1bde97588..e59cf3c3e 100644
--- a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombCanaryDistributer.java
+++ b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/custom/ServiceCombRouterDistributor.java
@@ -16,20 +16,29 @@
  */
 package org.apache.servicecomb.router.custom;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.servicecomb.loadbalance.ServiceCombServer;
-import org.apache.servicecomb.registry.api.registry.Microservice;
 import org.apache.servicecomb.router.distribute.AbstractRouterDistributor;
 import org.springframework.stereotype.Component;
 
 @Component
-public class ServiceCombCanaryDistributer extends
-    AbstractRouterDistributor<ServiceCombServer, Microservice> {
+public class ServiceCombRouterDistributor extends
+    AbstractRouterDistributor<ServiceCombServer> {
 
-  public ServiceCombCanaryDistributer() {
-    init(server -> MicroserviceCache.getInstance()
-            .getService(server.getInstance().getServiceId()),
-        Microservice::getVersion,
-        Microservice::getServiceName,
-        Microservice::getProperties);
+  public ServiceCombRouterDistributor() {
+    init(
+        instance -> MicroserviceCache.getInstance()
+            .getService(instance.getInstance().getServiceId()).getVersion(),
+        instance -> MicroserviceCache.getInstance()
+            .getService(instance.getInstance().getServiceId()).getServiceName(),
+        instance -> {
+          Map<String, String> properties = new HashMap<>();
+          properties.putAll(MicroserviceCache.getInstance()
+              .getService(instance.getInstance().getServiceId()).getProperties());
+          properties.putAll(instance.getInstance().getProperties());
+          return properties;
+        });
   }
 }
diff --git a/handlers/handler-router/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter b/handlers/handler-router/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
index 02a728b19..065e42fad 100644
--- a/handlers/handler-router/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
+++ b/handlers/handler-router/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.servicecomb.router.custom.RouterInvokeFilter
\ No newline at end of file
+org.apache.servicecomb.router.custom.RouterAddHeaderFilter
\ No newline at end of file