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 2021/08/26 02:26:38 UTC

[servicecomb-java-chassis] branch master updated: #SCB-2320 Add instanceId and serviceId in invocation header. (#2526)

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


The following commit(s) were added to refs/heads/master by this push:
     new c32a139  #SCB-2320 Add instanceId and serviceId in invocation header. (#2526)
c32a139 is described below

commit c32a1399ffba279cde6b382691b8681834e61f06
Author: zyl <72...@users.noreply.github.com>
AuthorDate: Thu Aug 26 10:21:12 2021 +0800

    #SCB-2320 Add instanceId and serviceId in invocation header. (#2526)
---
 .../java/org/apache/servicecomb/core/Const.java    |  4 ++++
 .../core/invocation/InvocationFactory.java         | 23 ++++++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/Const.java b/core/src/main/java/org/apache/servicecomb/core/Const.java
index fc90ac0..cd44333 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Const.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Const.java
@@ -41,6 +41,10 @@ public final class Const {
 
   public static final String SRC_MICROSERVICE = "x-cse-src-microservice";
 
+  public static final String SRC_SERVICE_ID = "x-src-serviceId";
+
+  public static final String SRC_INSTANCE_ID = "x-src-instanceId";
+
   public static final String TARGET_MICROSERVICE = "x-cse-target-microservice";
 
   public static final String REMOTE_ADDRESS = "x-cse-remote-address";
diff --git a/core/src/main/java/org/apache/servicecomb/core/invocation/InvocationFactory.java b/core/src/main/java/org/apache/servicecomb/core/invocation/InvocationFactory.java
index 1ade3a7..f3504b0 100644
--- a/core/src/main/java/org/apache/servicecomb/core/invocation/InvocationFactory.java
+++ b/core/src/main/java/org/apache/servicecomb/core/invocation/InvocationFactory.java
@@ -27,6 +27,9 @@ import org.apache.servicecomb.core.definition.InvocationRuntimeType;
 import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.registry.RegistrationManager;
+import org.apache.servicecomb.registry.api.registry.Microservice;
+
+import com.netflix.config.DynamicPropertyFactory;
 
 public final class InvocationFactory {
   private InvocationFactory() {
@@ -42,11 +45,27 @@ public final class InvocationFactory {
   }
 
   public static Invocation setSrcMicroservice(Invocation invocation) {
-    String microserviceName = RegistrationManager.INSTANCE.getMicroservice().getServiceName();
-    invocation.addContext(Const.SRC_MICROSERVICE, microserviceName);
+    Microservice microservice = RegistrationManager.INSTANCE.getMicroservice();
+    invocation.addContext(Const.SRC_MICROSERVICE, microservice.getServiceName());
+    if (addSourceServiceId()) {
+      invocation.addContext(Const.SRC_SERVICE_ID, microservice.getServiceId());
+    }
+    if (addSourceInstanceId()) {
+      invocation.addContext(Const.SRC_INSTANCE_ID, microservice.getInstance().getInstanceId());
+    }
     return invocation;
   }
 
+  public static boolean addSourceServiceId() {
+    return DynamicPropertyFactory.getInstance().
+        getBooleanProperty("servicecomb.context.source.serviceId", true).get();
+  }
+
+  public static boolean addSourceInstanceId() {
+    return DynamicPropertyFactory.getInstance().
+        getBooleanProperty("servicecomb.context.source.instanceId", true).get();
+  }
+
   /*
    * transport server收到请求时,创建invocation
    */