dubbo 远程调用

2023-06-07,

记得服务暴露的时候createServer()里 server = Exchangers.bind(url, requestHandler);

requestHandler在DubboProtocol里的实现如下:

 private ExchangeHandler requestHandler = new ExchangeHandlerAdapter() {

        @Override
public Object reply(ExchangeChannel channel, Object message) throws RemotingException {
if (message instanceof Invocation) {
Invocation inv = (Invocation) message;
Invoker<?> invoker = getInvoker(channel, inv);
// need to consider backward-compatibility if it's a callback
if (Boolean.TRUE.toString().equals(inv.getAttachments().get(IS_CALLBACK_SERVICE_INVOKE))) {
String methodsStr = invoker.getUrl().getParameters().get("methods");
boolean hasMethod = false;
if (methodsStr == null || methodsStr.indexOf(",") == -1) {
hasMethod = inv.getMethodName().equals(methodsStr);
} else {
String[] methods = methodsStr.split(",");
for (String method : methods) {
if (inv.getMethodName().equals(method)) {
hasMethod = true;
break;
}
}
}
if (!hasMethod) {
logger.warn(new IllegalStateException("The methodName " + inv.getMethodName()
+ " not found in callback service interface ,invoke will be ignored."
+ " please update the api interface. url is:"
+ invoker.getUrl()) + " ,invocation is :" + inv);
return null;
}
}
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());
}

其中getInvoker()会去AbstractProtocol里的exporterMap通过serviceKey(facade.EchoService:20880)得到DubboExporter实例,最后得到RegistryProtocol$InvokerDelegete实例返回,最后调用inoker()方法,总结了下各invoker和export的持有关系如下:

dubbo 远程调用的相关教程结束。

《dubbo 远程调用.doc》

下载本文的Word格式文档,以方便收藏与打印。