首页  

dubbo之RpcContext     所属分类 dubbo 浏览量 808
RpcContext  ThreadLocal  
记录 rpc 调用的上下文信息

RpcContext是一个临时状态记录器,当接收到RPC请求,或发起RPC请求时,RpcContext的状态都会变化。
比如:A调B,B再调C,则B机器上,在B调C之前,RpcContext记录的是A调B的信息,在B调C之后,RpcContext记录的是B调C的信息。
 
消费端

// 远程调用之前,设置 attachment 
RpcContext.getContext().setAttachment("userKey", "userValue");
// 远程调用
xxxService.xxx();
// 是否为消费端 true
boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
// 获取提供者IP
String serverIP = RpcContext.getContext().getRemoteHost();
String application = RpcContext.getContext().getUrl().getParameter("application");

// 发起新的RPC调用,上下文会变
yyyService.yyy();
RpcContext.getContext();  


提供方
public class XxxServiceImpl implements XxxService {

    public void xxx() {
        // 获取 Attachment 参数
        String value = RpcContext.getContext().getAttachment("userKey");
        // 是否为提供端,true
        boolean isProviderSide = RpcContext.getContext().isProviderSide();
        // 获取调用方IP
        String clientIP = RpcContext.getContext().getRemoteHost();
        String application = RpcContext.getContext().getUrl().getParameter("application");
        
        yyyService.yyy();
        //  变成消费端,返回false
        boolean isProviderSide = RpcContext.getContext().isProviderSide();
    } 
}

异步调用


Future<T> future = RpcContext.getContext().asyncCall(new Callable<T>(){...});
// 获取异步调用结果 
Future<T> future = RpcContext.getContext().getFuture();




消费端 执行Rpc调用前,经过Filter处理, 将信息写入RpcContext ConsumerContextFilter RpcContext.getContext() setInvoker(invoker) .setInvocation(invocation) .setLocalAddress(NetUtils.getLocalHost(), 0) .setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()); 服务端执行调用之前,经过Filter处理,将信息写入RpcContext ContextFilter RpcContext.getContext() .setInvoker(invoker) .setInvocation(invocation) .setAttachments(attachments) .setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()); provider 的remoteAddress 从tcp连接中获取的 DubboProtocol RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress()); 每发起RPC调用,上下文状态会变 消费者在执行调用之前(AbstractInvoker),将RpcContext中的内容写入到invocation,实现参数传递 Map< String, String> context = RpcContext.getContext().getAttachments(); if (context != null) { //只新增,不修改 invocation.addAttachmentsIfAbsent(context); } 异步实现中,将async参数设置为true, 也会传递到提供者来实现异步调用。 setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString()); DubboInvoker在执行调用的时候,会判断ASYNC_KEY是否为true, 如果是,则会向context中写入future对象: ... else if (isAsync) { ResponseFuture future = currentClient.request(inv, timeout) ; RpcContext.getContext().setFuture(new FutureAdapter< Object>(future)); return new RpcResult(); } ... 不等待调用结果返回 ,以实现消费端的异步调用。 判断是否为Provider或者Consumer , 通过remoteAdress和localAddress做对比来实现 // address为remoteAddress,在调用之前Filter中写入 String host; if (address.getAddress() == null) { host = address.getHostName(); } else { host = address.getAddress().getHostAddress(); } return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()).equals(NetUtils.filterLocalHost(NetUtils.getIpByHost(host)));

上一篇     下一篇
K8S简介

十六条实用的人生经验

dubbo 分布式追踪 traceId 传递要点

dubbo RpcContexta attachments 实例

Dubbo Filter 顺序

dubbo 自定义 filter 实例