dubbo RpcContexta attachments 实例
所属分类 dubbo
浏览量 952
调用端
dyyx.util.WebUtil.run_hello
String name = request.getParameter("name");
if(StringUtils.isBlank(name)) {
name = "dyyx";
}
RpcContext.getContext().getAttachments().put("name", name);
context.put("attachment_name", RpcContext.getContext().getAttachments().get("name"));
context.put("isConsumerSide", RpcContext.getContext().isConsumerSide());
context.put("isProviderSide", RpcContext.getContext().isProviderSide());
context.put("remoteAddressString", RpcContext.getContext().getRemoteAddressString());
context.put("remotePort", RpcContext.getContext().getRemotePort());
context.put("remoteHostName", RpcContext.getContext().getRemoteHostName());
// context.put("application", RpcContext.getContext().getUrl().getParameter("application"));
String result = getService().hello(name);
String attachment_name_after_invoke = RpcContext.getContext().getAttachments().get("name");
if(attachment_name_after_invoke==null) {
attachment_name_after_invoke = "NULL";
}
context.put("attachment_name_after_invoke", attachment_name_after_invoke);
服务端
dyyx.HelloServiceImpl.hello(String)
public String hello(String name) {
Map< String,Object> context = new HashMap< >();
context.put("attachment_name", RpcContext.getContext().getAttachments().get("name"));
context.put("isConsumerSide", RpcContext.getContext().isConsumerSide());
context.put("isProviderSide", RpcContext.getContext().isProviderSide());
context.put("remoteAddressString", RpcContext.getContext().getRemoteAddressString());
context.put("remotePort", RpcContext.getContext().getRemotePort());
context.put("remoteHostName", RpcContext.getContext().getRemoteHostName());
// context.put("application", RpcContext.getContext().getUrl().getParameter("application"));
String info = "hello,"+name+","+LocalDateTime.now()+",context="+context;
return info;
}
完整代码
https://gitee.com/dyyx/dubbodemo/blob/master/dubbo-hello-provider/src/main/java/dyyx/HelloServiceImpl.java
https://gitee.com/dyyx/dubbodemo/blob/master/dubbo-hello-consumer/src/main/java/dyyx/util/WebUtil.java
dubbo 分布式追踪 traceId 传递要点
dubbo之RpcContext
dubbo实例
上一篇
下一篇
十六条实用的人生经验
dubbo 分布式追踪 traceId 传递要点
dubbo之RpcContext
Dubbo Filter 顺序
dubbo 自定义 filter 实例
TBA聊考研