1. 三种发送请求
在DubboInvoker的doInvoke中调用
三种调用方式
- 同步
- 异步返回
- 异步不返回
之前已经介绍过ExchangeClient,可参考dubbo笔记-remoting(5)Exchanger
protected Result doInvoke(final Invocation invocation) throws Throwable {
RpcInvocation inv = (RpcInvocation) invocation;
final String methodName = RpcUtils.getMethodName(invocation);
inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());
inv.setAttachment(Constants.VERSION_KEY, version);
ExchangeClient currentClient;
if (clients.length == 1) {
currentClient = clients[0];
} else {
currentClient = clients[index.getAndIncrement() % clients.length];
}
try {
boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);
boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
if (isOneway) {
boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
currentClient.send(inv, isSent);
RpcContext.getContext().setFuture(null);
return new RpcResult();
} else if (isAsync) {
ResponseFuture future = currentClient.request(inv, timeout);
RpcContext.getContext().setFuture(new FutureAdapter<Object>(future));
return new RpcResult();
} else {
RpcContext.getContext().setFuture(null);
return (Result) currentClient.request(inv, timeout).get();
}
} catch (TimeoutException e) {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
} catch (RemotingException e) {
throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
2. DefaultFuture
同步请求和异步返回内部实现实质是一样的,都是返回一个DefaultFuture,同步请求会调用DefaultFuture的get方法,阻塞等待线程结果返回,异步返回则将DefaultFuture返回给调用者,择机调用get方法
参考:Dubbo源码分析----DefaultFuture
3.接收请求
NettyServerHandler在接收到请求后会将请求转发到AllChannelHandler
AllChannelHandler会将请求在线程池中处理,ChannelEventRunnable是入口处理逻辑,最终会调用DubboProtocol的Invoker来调用方法执行
4. Dispatcher
事件派发策略,上面的接收请求的AllChannelHandler就是接收事件派发策略的其中一种,即接收到请求都转到线程池中处理,还有其他处理方法
参考:Dubbo源码分析----Dispatcher和ThreadPool
4.请求超时处理
DefaultFuture静态构造函数里面会启动一个线程不断检查超时的请求处理
参考:
https://blog.csdn.net/lz710117239/article/details/77530536
网友评论