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 2018/06/22 09:05:30 UTC

[incubator-servicecomb-docs] branch master updated: 增加 "使用Context传递控制消息" 说明

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/incubator-servicecomb-docs.git


The following commit(s) were added to refs/heads/master by this push:
     new bddadbd  增加 "使用Context传递控制消息" 说明
bddadbd is described below

commit bddadbdf66a30c7bff01f0ca3de7a26ac13d050c
Author: liubao <ba...@huawei.com>
AuthorDate: Fri Jun 22 10:44:10 2018 +0800

    增加 "使用Context传递控制消息" 说明
---
 java-chassis-reference/zh_CN/SUMMARY.md            |  1 +
 .../zh_CN/general-development/context.md           | 36 ++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/java-chassis-reference/zh_CN/SUMMARY.md b/java-chassis-reference/zh_CN/SUMMARY.md
index d914e87..ed68d3c 100644
--- a/java-chassis-reference/zh_CN/SUMMARY.md
+++ b/java-chassis-reference/zh_CN/SUMMARY.md
@@ -54,6 +54,7 @@
   * [框架上报版本号](general-development/report-framework-version.md)
   * [跨应用调用](general-development/cross-app-invocation.md)
   * [定制序列化和反序列化方法](general-development/secret-field.md)
+  * [使用Context传递控制消息](general-development/context.md)
 * [服务能力开放](edge/open-service.md)
   * [使用Edge Service做边缘服务](edge/by-servicecomb-sdk.md)
   * [使用confd和Nginx做边缘服务](edge/nginx.md)
diff --git a/java-chassis-reference/zh_CN/general-development/context.md b/java-chassis-reference/zh_CN/general-development/context.md
new file mode 100644
index 0000000..1b4b97a
--- /dev/null
+++ b/java-chassis-reference/zh_CN/general-development/context.md
@@ -0,0 +1,36 @@
+# 使用Context传递控制消息
+
+ServiceComb提供了Context在微服务之间传递数据。Context是key/value对,只能够使用String类型的数据。由于Context会序列化为json格式并通过HTTP Header传递,因此也不支持ASCII之外的字符,其他字符需要开发者先自行编码再传递。Context在一次请求中,会在请求链上传递,不需要重新设置。[access log](../build-provider/access-log-configuration.md)的trace id等功能都基于这个特性实现的。
+
+## 场景描述
+* 在认证场景,Edge Service认证通过以后,需要将会话ID、用户名称等信息传递给微服务,实现鉴权等逻辑
+* 灰度发布场景,需要结合自定义的tag实现引流,tag信息需要传递给微服务
+
+## 使用参考
+
+* 在Hanlder中获取和设置Context
+
+Handler包含了Invocation对象,可以直接调用invocation.addContext和invocation.getContext设置。
+
+* 在服务接口中获取Context
+
+通过接口注入
+```
+public Response cseResponse(InvocationContext c1)
+```
+或者
+```
+ContextUtils.getInvocationContext()
+```
+
+* 在Edge Service中设置Context
+
+通过重载EdgeInvocation
+```
+EdgeInvocation edgeInvocation = new EdgeInvocation() {
+  protected void createInvocation() {
+    super.createInvocation();
+    this.invocation.addContext("hello", "world");
+  }
+};
+```
\ No newline at end of file