一:消费端发送请求
1.当调用dubbo接口方法时,因为获取的类实例是FactoryBean接口返回的代理类,所以会先经过InvokerInvocationHandler的invoke方法,这个代理是在类初始化时设置的
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}
2.在MockClusterInvoker里先检查是不是Mock类型,不是的话就会在FailoverClusterInvoker以及他的父类AbstractClusterInvoker进行处理
public Result invoke(final Invocation invocation) throws RpcException {
checkWheatherDestoried();
LoadBalance loadbalance;
List<Invoker<T>> invokers = list(invocation);
if (invokers != null && invokers.size() > 0) {
loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
.getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
} else {
loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
}
RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
return doInvoke(invocation, invokers, loadbalance);
}
3.先从注册目录里查找满足该方法的Invoker列表,然后用MockInvokersSelector进行路由,这个对象是最开始创建注册目录是setRouters(routers);设置进去的。然后获取负载均衡,默认是随机,判断需不需要异步,最后把这些参数传到Fai,loverClusterInvoker,获取默认重试次数三次,当不是第一次调用时需要重新获取最新Invoker列表,通过负载均衡进行筛选本次需要是用的Invoker,在ThreadLocal<RpcContext> LOCAL保存本次的调用信息。
public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
List<Invoker<T>> copyinvokers = invokers;
checkInvokers(copyinvokers, invocation);
int len = getUrl().getMethodParameter(invocation.getMethodName(), Constants.RETRIES_KEY, Constants.DEFAULT_RETRIES) + 1;
if (len <= 0) {
len = 1;
}
// retry loop.
RpcException le = null; // last exception.
List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyinvokers.size()); // invoked invokers.
Set<String> providers = new HashSet<String>(len);
for (int i = 0; i < len; i++) {
//重试时,进行重新选择,避免重试时invoker列表已发生变化.
//注意:如果列表发生了变化,那么invoked判断会失效,因为invoker示例已经改变
if (i > 0) {
checkWheatherDestoried();
copyinvokers = list(invocation);
//重新检查一下
checkInvokers(copyinvokers, invocation);
}
Invoker<T> invoker = select(loadbalance, invocation, copyinvokers, invoked);
invoked.add(invoker);
RpcContext.getContext().setInvokers((List)invoked);
try {
Result result = invoker.invoke(invocation);
return result;
} finally {
providers.add(invoker.getUrl().getAddress());
}
}
}
4.那么下来的invoker会是哪个对象呢?在消费端注册时会设置监听接口,也就是RegistryDirectory自身,当收到通知时会触发notify方法,方法最后会调用 refreshInvoker(invokerUrls);
Map<String, Invoker<T>> newUrlInvokerMap = toInvokers(invokerUrls) ;// 将URL列表转成Invoker列表
Map<String, List<Invoker<T>>> newMethodInvokerMap = toMethodInvokers(newUrlInvokerMap); // 换方法名映射Invoker列表
invoker = new InvokerDelegete<T>(protocol.refer(serviceType, url), url, providerUrl);
/**
* 代理类,主要用于存储注册中心下发的url地址,用于重新重新refer时能够根据providerURL queryMap overrideMap重新组装
*/
private static class InvokerDelegete<T> extends InvokerWrapper<T>{
private URL providerUrl;
public InvokerDelegete(Invoker<T> invoker, URL url, URL providerUrl) {
super(invoker, url);
this.providerUrl = providerUrl;
}
public URL getProviderUrl() {
return providerUrl;
}
}
5.invoke方法的调用由InvokerWrapper-》ProtocolFilterWrapper,这块是通过连接过滤器链进行串联起来的buildInvokerChain-》ListenerInvokerWrapper-》AbstractInvoker,给RpcInvocation 设置Invoker,添加本调用类的相关超时等配置项,获取并且设置本线程的上下文 RpcContext.getContext().getAttachments()的配置,判断是否异步-》DubboInvoker
public Result invoke(Invocation inv) throws RpcException {
RpcInvocation invocation = (RpcInvocation) inv;
invocation.setInvoker(this);
if (attachment != null && attachment.size() > 0) {
invocation.addAttachmentsIfAbsent(attachment);
}
Map<String, String> context = RpcContext.getContext().getAttachments();
if (context != null) {
invocation.addAttachmentsIfAbsent(context);
}
if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)){
invocation.setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString());
}
RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
try {
return doInvoke(invocation);
} catch (InvocationTargetException e) { // biz exception
}
6.判断同步异步,或者不需要回复,设置上下文RpcContext异步Future为null,然后开始超时等待get()-》 DefaultFuture类的get(timeout);
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];
}
try {
{
RpcContext.getContext().setFuture(null);
return (Result) currentClient.request(inv, timeout).get();
}
}
}
7.获取交换层,因为这些invoker都是从开始refer服务后更新的缓存中取出来的,所以路径为ReferenceCountExchangeClient-》HeaderExchangeClient-》HeaderExchangeChannel,这是Request 第一次被创建出来,设置超时等待的DefaultFuture,
public ResponseFuture request(Object request, int timeout) throws RemotingException {
// create request.
Request req = new Request();
req.setVersion("2.0.0");
req.setTwoWay(true);
req.setData(request);
DefaultFuture future = new DefaultFuture(channel, req, timeout);
try{
channel.send(req);
}
return future;
}
8.开始通过channel准备往通道里面发请求,这个通道就是[id: 0x, /本地IP:端口=> /服务提供者IP:端口]-》AbstractPeer-》AbstractClient-》AbstractClient从NettyClient中获取getChannel();最后通过NettyChannel的channel写入请求ChannelFuture future = channel.write(message);经过netty框架的下行通道ChannelDownstreamHandler处理,到达我们最开始启动客户端 时设置的final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);NettyClient的pipeline.addLast("handler", nettyHandler);
这个handle是通过this传进来的,handler.sent(channel, e.getMessage());-》AbstractPeer-》MultiMessageHandler的父类AbstractChannelHandlerDelegate-》HeartbeatHandler这里会设置写入时间-》WrappedChannelHandler-》DecodeHandler的父类AbstractChannelHandlerDelegate-》HeaderExchangeHandler重新设置写入时间
具体的通信编解码工具是依靠DubboCountCodec来完成的。
二:提供端处理请求
9.先用DubboCountCodec解码,NettyHandler的messageReceived(handler.received(channel, e.getMessage());)处理请求-》NettyServer的父类AbstractPeer-》MultiMessageHandler-》HeartbeatHandler给通道channel设置读取时间,判断是心跳,回复还是请求-》AllChannelHandler获取之前配置的线程池,然后把这个请求放入线程池处理,设置channel为接收状态, cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
10.DecodeHandler判断请求有没有进行再次编码,如果是请求消息,取出(Request)message).getData()为DecodeableRpcInvocation类型,-》HeaderExchangeHandler获取之前链接的时候存储的ExchangeChannel
public void received(Channel channel, Object message) throws RemotingException {
channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
try {
if (message instanceof Request) {
// handle request.
Request request = (Request) message;
if (request.isEvent()) {
handlerEvent(channel, request);
} else {
if (request.isTwoWay()) {
Response response = handleRequest(exchangeChannel, request);
channel.send(response);
} else {
handler.received(exchangeChannel, request.getData());
}
}
} else if (message instanceof Response) {
handleResponse(channel, (Response) message);
}
} finally {
HeaderExchangeChannel.removeChannelIfDisconnected(channel);
}
}
创建回复对象Response ,并且调用DubboProtocol的创建的匿名类requestHandler处理请求,
Response handleRequest(ExchangeChannel channel, Request req) throws RemotingException {
Response res = new Response(req.getId(), req.getVersion());
// find handler by message class.
Object msg = req.getData();
try {
// handle data.
Object result = handler.reply(channel, msg);
res.setStatus(Response.OK);
res.setResult(result);
} catch (Throwable e) {
res.setStatus(Response.SERVICE_ERROR);
res.setErrorMessage(StringUtils.toString(e));
}
return res;
}
根据channel和Invocation 获取之前暴露的DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get(serviceKey);这个key是接口全限定名:端口号,然后
exporter.getInvoker()返回具体的方法代理Invoker。给上下文RpcContext设置远程地址,开始invoke调用
public Object reply(ExchangeChannel channel, Object message) throws RemotingException {
if (message instanceof Invocation) {
Invocation inv = (Invocation) message;
Invoker<?> invoker = getInvoker(channel, inv);
RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress());
return invoker.invoke(inv);
}
throw new RemotingException(channel, "Unsupported request: " + message == null ? null : (message.getClass().getName() + ": " + message) + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
}
11.ProtocolFilterWrapper过滤器链处理--》ListenerInvokerWrapper-》RegistryProtocol内部类InvokerDelegete的父类InvokerWrapper,这些都是暴露服务是后封装的-》AbstractProxyInvoker,在最初暴露服务时会用Javassist生成一个代理
public Result invoke(Invocation invocation) throws RpcException {
try {
return new RpcResult(doInvoke(proxy, invocation.getMethodName(), invocation.getParameterTypes(), invocation.getArguments()));
}
}
调用相应的方法返回结果 res.setStatus(Response.OK); res.setResult(result);
new AbstractProxyInvoker<T>(proxy, type, url) {
@Override
protected Object doInvoke(T proxy, String methodName,
Class<?>[] parameterTypes,
Object[] arguments) throws Throwable {
return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
}
};
三:提供端发送处理结果
12.往HeaderExchangeHandler的channel通道里写入回应 channel.send(response);在NettyChannel父类AbstractChannel父类AbstractPeer设置发送标志sent也就是超时,ChannelFuture future = channel.write(message);四:消费端接收处理结果
13.在HeaderExchangeHandler的received方法中处理结果handleResponse(channel, (Response) message);但结果不是心跳和空的话DefaultFuture.received(channel, response);future.doReceived(response);之前消费端等待请求结果时调用get堵塞在done.await(timeout, TimeUnit.MILLISECONDS);
public Object get(int timeout) throws RemotingException {
if (! isDone()) {
long start = System.currentTimeMillis();
lock.lock();
try {
while (! isDone()) {
done.await(timeout, TimeUnit.MILLISECONDS);
if (isDone() || System.currentTimeMillis() - start > timeout) {
break;
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
if (! isDone()) {
throw new TimeoutException(sent > 0, channel, getTimeoutMessage(false));
}
}
return returnFromResponse();
}
而结果消息到来时response 会有值,并且done.signal();
private void doReceived(Response res) {
lock.lock();
try {
response = res;
if (done != null) {
done.signal();
}
} finally {
lock.unlock();
}
if (callback != null) {
invokeCallback(callback);
}
}
最后返回结果即可returnFromResponse();