This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-website.git
The following commit(s) were added to refs/heads/master by this push:
new d3f94dc rest的header设置补充 (#724)
d3f94dc is described below
commit d3f94dc3c861e1b01d08be1ce8451a8dc876803e
Author: sourirer <[email protected]>
AuthorDate: Fri Jan 29 14:27:07 2021 +0800
rest的header设置补充 (#724)
Co-authored-by: yi.chen2 <[email protected]>
---
content/en/docs/v2.7/user/rest.md | 31 ++++++++++++++++++++++++++++++-
content/zh/docs/v2.7/user/rest.md | 30 +++++++++++++++++++++++++++++-
2 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/content/en/docs/v2.7/user/rest.md
b/content/en/docs/v2.7/user/rest.md
index 9269e61..0d008e0 100644
--- a/content/en/docs/v2.7/user/rest.md
+++ b/content/en/docs/v2.7/user/rest.md
@@ -43,11 +43,13 @@ Working in progress ...
* Add Custom Exception Handler
* Configure HTTP Log Output
* Verification of Input Parameters
- * Should REST Services be Published Transparently
+ * Should REST Services be Published Transparently
+ * Get Headers In Dubbo Rest Provider
* Details of REST Service Consumer
* Scenario 1: Non-Dubbo Consumer Calls Dubbo REST Service
* Scenario 2: Dubbo Consumer Calls Dubbo REST Service
* Scenario 3: Dubbo Consumer Calls Non-Dubbo REST Service
+ * Custom Header By Dubbo Consumer while Calling REST Service
* JAX-RS Restrictions in Dubbo
* REST FAQ
* Can Dubbo REST Services be Integrated With Dubbo Registration Center and
Monitoring Center?
@@ -899,6 +901,15 @@ Of course, your service code may already act as a Facade
and DTO before adding R
This kind of system is cumbersome, and the workload of data conversion is not
small, so it should be avoided if possible.
+### Get Headers In Dubbo Rest Provider
+
+Dubbo take out and split headers by RpcContextFilter and put them into
attachments of RpcContext, so provider can get headers from RpcContext
attachments like:
+
+```
+ String header-value1 = RpcContext.getContext().getAttachment(header-key1)
+ String header-value2 = RpcContext.getContext().getAttachment(header-key2)
+```
+
### Consumer of RESTful Remoting
Here we use three scenarios:
@@ -1021,6 +1032,24 @@ For the configuration in Spring, because the REST
service is not provided by Dub
<dubbo:reference id="userService" interface="xxx.UserService"
url="rest://api.foo.com/services/" timeout="2000" connections="10"/>
```
+### Custom Header By Dubbo Consumer while Calls REST Service
+
+When Dubbo calls rest, it uses the method of converting the attachments of
RpcContext to header.
+
+Therefore, you can set headers in the following ways:
+
+```
+ RpcContext.getContext().setAttachment("header-key1", "header-value1");
+ RpcContext.getContext().setAttachment("header-key2", "header-value2");
+```
+
+Then the headers will be looks like following:
+
+```
+ header-key1 = header-value1
+ header-key2 = header-value2
+```
+
### JAX-RS restrictions in Dubbo
The REST development in Dubbo is fully compatible with standard JAX-RS, but
the features it supports are currently a subset of full JAX-RS, in part because
it is limited to the specific architecture of Dubbo and Spring. The limitations
of JAX-RS used in Dubbo include but are not limited to:
diff --git a/content/zh/docs/v2.7/user/rest.md
b/content/zh/docs/v2.7/user/rest.md
index 33e33ae..56bc7b3 100644
--- a/content/zh/docs/v2.7/user/rest.md
+++ b/content/zh/docs/v2.7/user/rest.md
@@ -44,11 +44,13 @@ description: "在 Dubbo 中开发 REST 风格的远程调用"
* 添加自定义的Exception处理
* 配置HTTP日志输出
* 输入参数的校验
- * 是否应该透明发布REST服务
+ * 是否应该透明发布REST服务
+ * Dubbo的REST提供端在被调用时使用header
* REST服务消费端详解
* 场景1:非dubbo的消费端调用dubbo的REST服务
* 场景2:dubbo消费端调用dubbo的REST服务
* 场景3:dubbo的消费端调用非dubbo的REST服务
+ * Dubbo的消费端在调用REST服务时配置自定义header
* Dubbo中JAX-RS的限制
* REST常见问题解答(REST FAQ)
* Dubbo REST的服务能和Dubbo注册中心、监控中心集成吗?
@@ -885,6 +887,15 @@ Dubbo的REST调用和dubbo中其它某些RPC不同的是,需要在服务代码
这种体系比较繁琐,数据转换之类的工作量也不小,所以一般应尽量避免如此。
+### dubbo的提供端在调用REST服务时使用header
+
+Dubbo通过RpcContextFilter将header取出分解之后设置到RpcContext的attachments,所以在提供端可以直接从RpcContext的attachments中获取到消费端设置的header信息:
+
+```
+ RpcContext.getContext().getAttachment(key1)
+ RpcContext.getContext().getAttachment(key2)
+```
+
## REST服务消费端详解
这里我们用三种场景来分别讨论:
@@ -1007,6 +1018,23 @@ public class User implements Serializable {
<dubbo:reference id="userService" interface="xxx.UserService"
url="rest://api.foo.com/services/" timeout="2000" connections="10"/>
```
+### dubbo的消费端在调用REST服务时配置自定义header
+
+Dubbo进行rest调用的时候,采用的是将RpcContext的attachment转换为header的方式,所以,dubbo消费端可以按以下方式进行自定义header的设置:
+
+```
+ RpcContext.getContext().setAttachment("key1", "value1");
+ RpcContext.getContext().setAttachment("key2", "value2");
+```
+
+即可设置如下格式的header:
+
+```
+ key1=value1
+ key2=value2
+```
+
+
## Dubbo中JAX-RS的限制
Dubbo中的REST开发是完全兼容标准JAX-RS的,但其支持的功能目前是完整JAX-RS的一个子集,部分因为它要受限于dubbo和spring的特定体系。