Dubbo uses the invoke directive to invoke the Dubbo interface

preface

Recently, I was assigned a task to provide services externally through Dubbo. Dubbo does not have the Controller layer in our general Web project, so there is no way to call debugging through HTTP. As a cute new me, I was really at a loss, until the old driver reminded me to use dubbo Invoke to test the interface, and I had no direction.

For dubbo setup, see my blog: SpringBoot implements RPC calls using Dubbo

The body of the

First of all, we need to know that Dubbo is different from traditional Web projects that need to be run in web containers such as Tomcat and JBoss. The operation mechanism of Dubbo can be explored by referring to Dubbo components below.

Dubbo core components

  • Provider: Exposes the provider of the service, which can be started as a JAR or container
  • Consumer: Service consumer that invokes the remote service.
  • Registry: Service registry and discovery center.
  • Monitor: Counts the number of services and calls, and calls the time monitoring center.
  • Container: Container in which the service runs.



DubbotheContainerA module is an independent container that can be used fordubboProvide a service discovery mechanism. And thisContainerContainers are used by defaultdubboThe protocol exposes the service at port number 20880, so we call it through this interfacedubboInterface.

Use invoke to invoke the Dubbo interface service

Use the Telnet command to access the console command: Telnet IP port

telnet 127.0. 01. 20880
Copy the code

View available Dubbo interfaces

  • ls: Displays the service list.
  • ls -l: Displays the service details list.
  • ls XxxService: Displays a list of methods for the service.
  • ls -l XxxService: Displays a list of method details for the service.

Use the invoke command to inject

If you are injecting JSON, you can use the following format to make the Dubbo service call

Invoke XxxService. XxxMethod ({" name ":" luo "},18, "male")Copy the code

If an object is injected, the dubbo service call can be made in the following format:

Invoke XxxService. XxxMethod ([{" name ":" luo ", "age" :18, "sex", "male","class":"com.luo.entity.User"})Copy the code

Quickly generate the Invoke command

Use a Demo to help generate the corresponding Invoke command

public class GenerateInvoke {

    public static void main(String[] args) throws NoSuchMethodException {
        QueryReq queryReq = new QueryReq();
        queryReq.setName("The stream source")
        String invokeCommand = getInvoke(queryReq, ICeshiService.class, "query");
        System.out.println("Left invoke commands");
        System.out.println(invokeCommand);
    }


    /** * Generate invoke command *@paramReqParam request parameter *@paramReqService Interface of the request service Service *@paramThe reqMathed requested service method *@throws NoSuchMethodException
     */
    public static String getInvoke(Object reqParam, Class
        reqService, String reqMathed) throws NoSuchMethodException {
        JSONObject jsonObject = JSONObject.parseObject(JsonUtil.toJson(reqParam));
        jsonObject.put("class", reqParam.getClass().getName());
        return "invoke "+ reqService.getName()+
                "."+ reqService.getMethod(reqMathed,reqParam.getClass()).getName()+
                "("+jsonObject.toJSONString()+")"; }}Copy the code