motan icon indicating copy to clipboard operation
motan copied to clipboard

如何在RPC调用间隐式传参呢?

Open nkcoder opened this issue 9 years ago • 3 comments

我通过RpcContext没成功,代码如下:

consumer端:

DefaultRequest request = new DefaultRequest();
request.setAttachment("myparams1", authToken);
RpcContext.getContext().setRequest(request);
xxxRemoteService.call();

或者:

RpcContext.getContext().putAttribute("myparams1", authToken);

provider端:

Request request = RpcContext.getContext().getRequest();
String authToken = request.getAttachments().get("myparams1");

或者:

String authToken = (String) RpcContext.getContext().getAttribute("myparams1");

请问不知道是否使用方式不正确?

(dubbo使用RpcContext.getContext().getAttachment()RpcContext.getContext().setAttachment是可以的)

nkcoder avatar Apr 14 '17 02:04 nkcoder

为了防止attachment被调用方误用,目前并没有开放这个功能。 如果是通用行为需要透传参数,如认证信息等,建议通过实现自定义的filter来只透传指定参数。关于spi扩展可以参考https://github.com/weibocom/motan/wiki/zh_developguide#编写一个motan扩展

rayzhang0603 avatar Apr 18 '17 05:04 rayzhang0603

@rayzhang0603 现在支持这种吗

fallsea avatar Jan 08 '19 11:01 fallsea

@fallsea 已经支持了,可以通过RpcContext传递

 RpcContext.getContext().setRpcAttachment("k","v");

 RpcContext.getContext().getRequest().getAttachments().get("k");

rayzhang0603 avatar Jan 08 '19 12:01 rayzhang0603