motan
motan copied to clipboard
如何在RPC调用间隐式传参呢?
我通过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是可以的)
为了防止attachment被调用方误用,目前并没有开放这个功能。 如果是通用行为需要透传参数,如认证信息等,建议通过实现自定义的filter来只透传指定参数。关于spi扩展可以参考https://github.com/weibocom/motan/wiki/zh_developguide#编写一个motan扩展
@rayzhang0603 现在支持这种吗
@fallsea 已经支持了,可以通过RpcContext传递
RpcContext.getContext().setRpcAttachment("k","v");
RpcContext.getContext().getRequest().getAttachments().get("k");