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 e3825642b387f393c6a86f3b2d7b347d8a5388bb
Author: GuoYL <[email protected]>
AuthorDate: Mon Nov 18 20:46:02 2019 +0800

    [SCB-1407] generic modify
---
 .../router/constom/CanaryInvokeFilter.java          |  9 ++++++++-
 .../router/constom/CanaryServerListFilter.java      | 21 +++++++++++++++++----
 .../exception/RouterIllegalParamException.java      |  2 ++
 .../servicecomb/router/RouterDistributorTest.java   | 15 ++++++++-------
 4 files changed, 35 insertions(+), 12 deletions(-)

diff --git 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryInvokeFilter.java
 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryInvokeFilter.java
index 7ccbeb3..c1e8a02 100644
--- 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryInvokeFilter.java
+++ 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryInvokeFilter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.servicecomb.router.constom;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.netflix.config.DynamicStringProperty;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -26,6 +27,7 @@ import java.util.concurrent.CompletableFuture;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
 import org.apache.servicecomb.swagger.invocation.Response;
@@ -75,7 +77,12 @@ public class CanaryInvokeFilter implements HttpServerFilter {
     loadHeaders();
     if (invocation.getContext("canary_context") != null && 
!CollectionUtils.isEmpty(allHeader)) {
       Map<String, String> headerMap = getHeaderMap(httpServletRequestEx);
-      invocation.addContext("canary_context", Json.encode(headerMap));
+      try {
+        invocation.addContext("canary_context", 
JsonUtils.OBJ_MAPPER.writeValueAsString(headerMap));
+      } catch (JsonProcessingException e) {
+        LOGGER.error("canary context serialization failed");
+        e.printStackTrace();
+      }
     }
     return null;
   }
diff --git 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryServerListFilter.java
 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryServerListFilter.java
index 4532587..4397ffc 100644
--- 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryServerListFilter.java
+++ 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/constom/CanaryServerListFilter.java
@@ -16,11 +16,14 @@
  */
 package org.apache.servicecomb.router.constom;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.loadbalance.ServerListFilterExt;
 import org.apache.servicecomb.loadbalance.ServiceCombServer;
 import com.netflix.config.DynamicPropertyFactory;
@@ -28,12 +31,13 @@ import com.netflix.config.DynamicPropertyFactory;
 import io.vertx.core.json.Json;
 import org.apache.servicecomb.router.RouterFilter;
 import org.apache.servicecomb.router.distribute.RouterDistributor;
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 
 public class CanaryServerListFilter implements ServerListFilterExt {
 
   private static final String ENABLE = "servicecomb.release_way";
 
-  RouterDistributor distributer = new ServiceCombCanaryDistributer();
+  RouterDistributor<ServiceCombServer, Microservice> distributer = new 
ServiceCombCanaryDistributer();
 
   @Override
   public boolean enabled() {
@@ -54,9 +58,18 @@ public class CanaryServerListFilter implements 
ServerListFilterExt {
   private Map<String, String> addHeaders(Invocation invocation) {
     Map<String, String> headers = new HashMap<>();
     if (invocation.getContext("canary_context") != null) {
-      Map<String, String> canaryContext = Json
-          .decodeValue(invocation.getContext("canary_context"), Map.class);
-      headers.putAll(canaryContext);
+      Map<String, String> canaryContext = null;
+      try {
+        canaryContext = JsonUtils.OBJ_MAPPER
+            .readValue(invocation.getContext("canary_context"),
+                new TypeReference<Map<String, String>>() {
+                });
+      } catch (JsonProcessingException e) {
+        e.printStackTrace();
+      }
+      if (canaryContext != null) {
+        headers.putAll(canaryContext);
+      }
     }
     for (int i = 0; i < invocation.getArgs().length; i++) {
       if (invocation.getOperationMeta().getParamName(i) != null &&
diff --git 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/exception/RouterIllegalParamException.java
 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/exception/RouterIllegalParamException.java
index dc674c7..61fda32 100644
--- 
a/handlers/handler-router/src/main/java/org/apache/servicecomb/router/exception/RouterIllegalParamException.java
+++ 
b/handlers/handler-router/src/main/java/org/apache/servicecomb/router/exception/RouterIllegalParamException.java
@@ -22,6 +22,8 @@ package org.apache.servicecomb.router.exception;
  **/
 public class RouterIllegalParamException extends RuntimeException {
 
+  private static final long serialVersionUID = 4359709211352400087L;
+
   public RouterIllegalParamException(String message) {
     super(message);
   }
diff --git 
a/handlers/handler-router/src/test/java/org/apache/servicecomb/router/RouterDistributorTest.java
 
b/handlers/handler-router/src/test/java/org/apache/servicecomb/router/RouterDistributorTest.java
index 7de2aa5..d346a9a 100644
--- 
a/handlers/handler-router/src/test/java/org/apache/servicecomb/router/RouterDistributorTest.java
+++ 
b/handlers/handler-router/src/test/java/org/apache/servicecomb/router/RouterDistributorTest.java
@@ -27,6 +27,7 @@ import java.util.Map;
 import mockit.Expectations;
 import org.apache.servicecomb.router.cache.RouterRuleCache;
 import org.apache.servicecomb.router.distribute.AbstractRouterDistributor;
+import org.apache.servicecomb.router.distribute.RouterDistributor;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -64,25 +65,25 @@ public class RouterDistributorTest {
       + "            tags:\n"
       + "              version: 1\n"
       + "              app: a";
-  String targetServiceName = "test_server";
+
+  private static String targetServiceName = "test_server";
 
   @Test
   public void testVersionNotMatch() {
-    Map headermap = new HashMap();
+    Map<String, String> headermap = new HashMap<>();
     headermap.put("xxx", "xx");
     headermap.put("xx", "xx");
     headermap.put("formate", "json");
     List<ServiceIns> list = getMockList();
     list.remove(1);
     List<ServiceIns> serverList = mainFilter(list, headermap);
-    serverList.get(0).getHost().equals("01");
     Assert.assertEquals(1, serverList.size());
     Assert.assertEquals("01", serverList.get(0).getHost());
   }
 
   @Test
   public void testVersionMatch() {
-    Map headermap = new HashMap();
+    Map<String, String> headermap = new HashMap<>();
     headermap.put("xxx", "xx");
     headermap.put("xx", "xx");
     headermap.put("formate", "json");
@@ -103,7 +104,7 @@ public class RouterDistributorTest {
   }
 
   private List<ServiceIns> mainFilter(List<ServiceIns> serverlist, Map<String, 
String> headermap) {
-    TestDistributer TestDistributer = new TestDistributer();
+    RouterDistributor<ServiceIns, ServiceIns> testDistributer = new 
TestDistributer();
     DynamicPropertyFactory dpf = DynamicPropertyFactory.getInstance();
     DynamicStringProperty strp = new DynamicStringProperty("", ruleStr);
     new Expectations(dpf) {
@@ -115,14 +116,14 @@ public class RouterDistributorTest {
     RouterRuleCache.refresh();
     return RouterFilter
         .getFilteredListOfServers(serverlist, targetServiceName, headermap,
-            TestDistributer);
+            testDistributer);
   }
 
   class ServiceIns extends Server {
 
     String version = "1.1";
     String serverName = targetServiceName;
-    Map<String, String> tags = new HashMap();
+    Map<String, String> tags = new HashMap<>();
 
     public ServiceIns(String id) {
       super(id);

Reply via email to