Dubbo invoke命令

连接

telnet localhost 20880

接口列表

ls

接口下方法列表

ls -l MctCoreMerchantBaseInfoService

调用接口

参数已json格式传递

invoke com.bestpay.merchant.core.facade.merchant.MctCoreMerchantBaseInfoService.queryerchantBaseInfoByLoginNo({"loginNo": "[email protected]"})

报错

No such method queryerchantBaseInfoByLoginNo in service MctCoreMerchantBaseInfoService

错误原因:

查看dubbo invoke调用之后的处理 InvokeTelnetHandler
发现在校验参数的时候,会去判断类型是否一致。当我们传入json的时候,参数类型是jsonObject,所以我们得添加一个属性class,来指定参数类型。

Dubbo invoke命令_第1张图片

解决方案:

在invoke入参中加上入参类型

com.bestpay.merchant.core.facade.merchant.MctCoreMerchantBaseInfoService.queryerchantBaseInfoByLoginNo({"loginNo": "[email protected]", "class": "com.bestpay.merchant.core.model.req.MctCoreMerchantBaseInfoQueryReqDto"})

dubbo telnet源码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alibaba.dubbo.rpc.protocol.dubbo.telnet;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.common.utils.PojoUtils;
import com.alibaba.dubbo.common.utils.ReflectUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.telnet.TelnetHandler;
import com.alibaba.dubbo.remoting.telnet.support.Help;
import com.alibaba.dubbo.rpc.Exporter;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcInvocation;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import com.alibaba.fastjson.JSON;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Activate
@Help(
    parameter = "[service.]method(args)",
    summary = "Invoke the service method.",
    detail = "Invoke the service method."
)
public class InvokeTelnetHandler implements TelnetHandler {
    public InvokeTelnetHandler() {
    }

    private static Method findMethod(Exporter exporter, String method, List args) {
        Invoker invoker = exporter.getInvoker();
        Method[] methods = invoker.getInterface().getMethods();
        Method[] arr$ = methods;
        int len$ = methods.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Method m = arr$[i$];
            if (m.getName().equals(method) && isMatch(m.getParameterTypes(), args)) {
                return m;
            }
        }

        return null;
    }

    private static boolean isMatch(Class[] types, List args) {
        if (types.length != args.size()) {
            return false;
        } else {
            for(int i = 0; i < types.length; ++i) {
                Class type = types[i];
                Object arg = args.get(i);
                if (arg == null) {
                    if (type.isPrimitive()) {
                        throw new NullPointerException(String.format("The type of No.%d parameter is primitive(%s), but the value passed is null.", i + 1, type.getName()));
                    }
                } else if (ReflectUtils.isPrimitive(arg.getClass())) {
                    if (!ReflectUtils.isPrimitive(type)) {
                        return false;
                    }
                } else if (arg instanceof Map) {
                    String name = (String)((Map)arg).get("class");
                    Class cls = arg.getClass();
                    if (name != null && name.length() > 0) {
                        cls = ReflectUtils.forName(name);
                    }

                    if (!type.isAssignableFrom(cls)) {
                        return false;
                    }
                } else if (arg instanceof Collection) {
                    if (!type.isArray() && !type.isAssignableFrom(arg.getClass())) {
                        return false;
                    }
                } else if (!type.isAssignableFrom(arg.getClass())) {
                    return false;
                }
            }

            return true;
        }
    }

    public String telnet(Channel channel, String message) {
        if (message != null && message.length() != 0) {
            StringBuilder buf = new StringBuilder();
            String service = (String)channel.getAttribute("telnet.service");
            if (service != null && service.length() > 0) {
                buf.append("Use default service " + service + ".\r\n");
            }

            int i = message.indexOf("(");
            if (i >= 0 && message.endsWith(")")) {
                String method = message.substring(0, i).trim();
                String args = message.substring(i + 1, message.length() - 1).trim();
                i = method.lastIndexOf(".");
                if (i >= 0) {
                    service = method.substring(0, i).trim();
                    method = method.substring(i + 1).trim();
                }

                List list;
                try {
                    list = JSON.parseArray("[" + args + "]", Object.class);
                } catch (Throwable var18) {
                    return "Invalid json argument, cause: " + var18.getMessage();
                }

                Invoker invoker = null;
                Method invokeMethod = null;
                Iterator i$ = DubboProtocol.getDubboProtocol().getExporters().iterator();

                while(i$.hasNext()) {
                    Exporter exporter = (Exporter)i$.next();
                    if (service != null && service.length() != 0) {
                        if (service.equals(exporter.getInvoker().getInterface().getSimpleName()) || service.equals(exporter.getInvoker().getInterface().getName()) || service.equals(exporter.getInvoker().getUrl().getPath())) {
                            invokeMethod = findMethod(exporter, method, list);
                            invoker = exporter.getInvoker();
                            break;
                        }
                    } else {
                        invokeMethod = findMethod(exporter, method, list);
                        if (invokeMethod != null) {
                            invoker = exporter.getInvoker();
                            break;
                        }
                    }
                }

                if (invoker != null) {
                    if (invokeMethod != null) {
                        try {
                            Object[] array = PojoUtils.realize(list.toArray(), invokeMethod.getParameterTypes(), invokeMethod.getGenericParameterTypes());
                            RpcContext.getContext().setLocalAddress(channel.getLocalAddress()).setRemoteAddress(channel.getRemoteAddress());
                            long start = System.currentTimeMillis();
                            Object result = invoker.invoke(new RpcInvocation(invokeMethod, array)).recreate();
                            long end = System.currentTimeMillis();
                            buf.append(JSON.toJSONString(result));
                            buf.append("\r\nelapsed: ");
                            buf.append(end - start);
                            buf.append(" ms.");
                        } catch (Throwable var17) {
                            return "Failed to invoke method " + invokeMethod.getName() + ", cause: " + StringUtils.toString(var17);
                        }
                    } else {
                        buf.append("No such method " + method + " in service " + service);
                    }
                } else {
                    buf.append("No such service " + service);
                }

                return buf.toString();
            } else {
                return "Invalid parameters, format: service.method(args)";
            }
        } else {
            return "Please input method name, eg: \r\ninvoke xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke com.xxx.XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})";
        }
    }
}
 
  

 

你可能感兴趣的:(Dubbo,微服务,Java)