dubbo 远程调用

Posted l2c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo 远程调用相关的知识,希望对你有一定的参考价值。

记得服务暴露的时候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学习远程调用原理

Dubbo的RPC远程过程调用+Dubbo的负载均衡+Zookeeper注册中心

Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载)

Dubbo-服务消费者远程调用

Dubbo的使用

Dubbo多个远程代码执行漏洞