You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "robocanic (via GitHub)" <gi...@apache.org> on 2023/08/24 08:14:27 UTC
[GitHub] [dubbo] robocanic commented on issue #11876: RpcContext获取参数问题
robocanic commented on issue #11876:
URL: https://github.com/apache/dubbo/issues/11876#issuecomment-1691220310
我也遇到了类似的问题,在dubbo-samples中有一个上下文参数传递的demo [https://github.com/apache/dubbo-samples/blob/4b98c856b8c084fa51d9f0d4bbd6d45fe64cdeea/10-task/dubbo-samples-develop/dubbo-samples-develop-consumer/src/main/java/org/apache/dubbo/samples/develop/ContextTask.java#L37](url)
消费者在setAttachment之后,provider这边是get不到的。
这是consumer
``` java
public void contextServiceCall(){
//往服务端传递参数
RpcContext.getClientAttachment().setAttachment("clientKey1","This is client");
String res = contextService.invoke("context1");
//接收传递回来的参数
Map<String, Object> serverAttachments = RpcContext.getServerContext().getObjectAttachments();
System.out.println("ContextTask clientAttachment: " + JSON.toJSONString(serverAttachments));
System.out.println("ContextService Return: " + res);
}
```
这是provider
```java
@DubboService
public class ContextServiceImpl implements ContextService{
@Override
public String invoke(String param) {
//ServerAttachment接收传递过来的参数
Map<String, Object> serverAttachments =
RpcContext.getServerContext().getObjectAttachments();
System.out.println("ContextService server Receives server Attachments:"+ JSON.toJSONString(serverAttachments));
//往客户端传递参数
RpcContext.getServerContext().setAttachment("serverKey","这是server");
StringBuilder s = new StringBuilder();
s.append("ContextService param:").append(param);
return s.toString();
}
}
```
运行后在consumer这端是可以拿到provider传的attachments的
但是provider拿不到consumer传的attachments:
这是consumer的日志截图:

这是provider的日志截图

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org