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 41141974a23f228771a28aec4f88baf517d2b07e
Author: GuoYL <[email protected]>
AuthorDate: Mon Nov 18 16:59:53 2019 +0800

    [SCB-1407] Header can passthrough by congfig
---
 .../router/constom/CanaryInvokeFilter.java         | 48 ++++++++++++++++------
 .../router/constom/CanaryServerListFilter.java     | 11 +++--
 2 files changed, 43 insertions(+), 16 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 b41b216..63446e3 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
@@ -1,10 +1,14 @@
 package org.apache.servicecomb.router.constom;
 
+import com.netflix.config.DynamicStringProperty;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 
+import java.util.stream.Collectors;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.definition.OperationMeta;
@@ -13,6 +17,7 @@ import 
org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.yaml.snakeyaml.Yaml;
 
@@ -24,7 +29,9 @@ public class CanaryInvokeFilter implements HttpServerFilter {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(CanaryInvokeFilter.class);
 
-  private static final String PASS_HEADER = "servicecomb.passheader";
+  private static final String PASS_HEADER = "servicecomb.router.header";
+
+  private static List<String> allHeader = new ArrayList<>();
 
   @Override
   public int getOrder() {
@@ -51,30 +58,45 @@ public class CanaryInvokeFilter implements HttpServerFilter 
{
   @Override
   public Response afterReceiveRequest(Invocation invocation,
       HttpServletRequestEx httpServletRequestEx) {
-    if (invocation.getContext("canary_context") != null) {
-      Map<String, String> headerMap = 
getHeaderMap(invocation.getMicroserviceName(),
-          httpServletRequestEx);
+    loadHeaders();
+    if (invocation.getContext("canary_context") != null && 
!CollectionUtils.isEmpty(allHeader)) {
+      Map<String, String> headerMap = getHeaderMap(httpServletRequestEx);
       invocation.addContext("canary_context", Json.encode(headerMap));
     }
     return null;
   }
 
   /**
+   * 读配置文件Header
+   */
+  private void loadHeaders() {
+    DynamicStringProperty headerStr = DynamicPropertyFactory.getInstance()
+        .getStringProperty(PASS_HEADER, null, () -> {
+          allHeader = null;
+          DynamicStringProperty temHeader = 
DynamicPropertyFactory.getInstance()
+              .getStringProperty(PASS_HEADER, null);
+          Yaml yaml = new Yaml();
+          allHeader = yaml.load(temHeader.get());
+        });
+    try {
+      if (allHeader != null) {
+        Yaml yaml = new Yaml();
+        allHeader = yaml.load(headerStr.get());
+      }
+    } catch (Exception e) {
+      LOGGER.error("route management Serialization failed: {}", 
e.getMessage());
+    }
+  }
+
+  /**
    * 取出所用的header
    *
-   * @param serviceName
    * @param httpServletRequestEx
    * @return
    */
-  public Map<String, String> getHeaderMap(String serviceName,
-      HttpServletRequestEx httpServletRequestEx) {
-    Yaml yaml = new Yaml();
-    String headerStr = 
DynamicPropertyFactory.getInstance().getStringProperty(PASS_HEADER, null)
-        .get();
-    Map<String, String> headerKeyMap = yaml.load(headerStr);
-    Set<String> headerKeySet = headerKeyMap.keySet();
+  private Map<String, String> getHeaderMap(HttpServletRequestEx 
httpServletRequestEx) {
     Map<String, String> headerMap = new HashMap<>();
-    headerKeySet.forEach(headerKey -> {
+    allHeader.forEach(headerKey -> {
       String val = httpServletRequestEx.getHeader(headerKey);
       if (!StringUtils.isEmpty(val)) {
         headerMap.put(headerKey, httpServletRequestEx.getHeader(headerKey));
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 dc9211a..297cd25 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
@@ -29,6 +29,13 @@ public class CanaryServerListFilter implements 
ServerListFilterExt {
   public List<ServiceCombServer> 
getFilteredListOfServers(List<ServiceCombServer> list,
       Invocation invocation) {
     String targetServiceName = invocation.getMicroserviceName();
+    Map<String, String> headers = addHeaders(invocation);
+    return RouterFilter
+        .getFilteredListOfServers(list, targetServiceName, headers,
+            distributer);
+  }
+
+  private Map<String, String> addHeaders(Invocation invocation) {
     Map<String, String> headers = new HashMap<>();
     if (invocation.getContext("canary_context") != null) {
       Map<String, String> canaryContext = Json
@@ -43,8 +50,6 @@ public class CanaryServerListFilter implements 
ServerListFilterExt {
       }
     }
     headers.putAll(invocation.getContext());
-    return RouterFilter
-        .getFilteredListOfServers(list, targetServiceName, headers,
-            distributer);
+    return headers;
   }
 }

Reply via email to