Java获取ZK上Dubbo服务信息及泛化调用
    获取ZK上Dubbo服务信息(包含service / method / 参数信息等)
    使用 curator 实现(curator介绍:

    | Java
    package tech;

    import org.apache.curator.RetryPolicy;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;

    import java.nio.charset.StandardCharsets;
    import java.util.List;

    /**
    * @author liping.li@joymo.tech
    * @date 2021-04-10
    */
    public class GetDubboInfoDemo {

    1. public static void main(String[] args) {
    2. GetDubboInfoDemo dubboUtils = new GetDubboInfoDemo();<br /> RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 4);<br /> CuratorFramework client = CuratorFrameworkFactory._newClient_("zk1test2.joymo.tech:5181,zk2test2.joymo.tech:5181,zk3test2.joymo.tech:5181", retryPolicy);<br /> client.start();<br /> try {<br /> List<String> nodeList = dubboUtils.getServices(client);
    3. for(String node : nodeList){<br /> if(node.contains("centerorder") && node.endsWith("Facade")){<br /> System._out_.println(" \\n ------------------------- centerorder service ----------------------- ");<br /> System._out_.println(node);<br /> dubboUtils.getMetadata(node, client);<br /> System._out_.println(" \\n ------------------------- centerorder service end ----------------------- ");<br /> }<br /> }<br /> }catch (Exception e){<br /> e.printStackTrace();<br /> }finally {<br /> client.close();<br /> }
    4. }
    5. private String getMetadata(String serviceName, CuratorFramework client)throws Exception{<br /> String serviceMetadataNode = "/dubbo/metadata/" + serviceName + "/1.0.0/dubbo/provider/centerorder-service";<br /> String metadata = "";<br /> if(client.checkExists().forPath(serviceMetadataNode) != null) {<br /> byte[] temp = client.getData().forPath(serviceMetadataNode);<br /> metadata = new String(temp, StandardCharsets._UTF_8_);<br /> System._out_.println(metadata);<br /> }<br /> return metadata;<br /> }
    6. private List<String> getServices(CuratorFramework client)throws Exception{
    7. return client.getChildren().forPath("/dubbo");<br /> }<br />} |

    | —- |

    运行结果
    Java获取ZK上Dubbo服务信息及泛化调用 - 图1
    示例
    tech.joymo.centerorder.facade.product.OrderProductFacade 详细信息

    JSON
    {
    “parameters”:{
    “side”:”provider”,
    “release”:”2.7.8”,
    “methods”:”queryOrderProductByOrderNo”,
    “deprecated”:”false”,
    “dubbo”:”2.0.2”,
    “threads”:”300”,
    “interface”:”tech.joymo.centerorder.facade.product.OrderProductFacade”,
    “version”:”1.0.0”,
    “actives”:”2147483647”,
    “qos.enable”:”false”,
    “generic”:”false”,
    “timeout”:”3000”,
    “revision”:”1.0.0-SNAPSHOT”,
    “metadata-type”:”remote”,
    “application”:”centerorder-service”,
    “dynamic”:”true”,
    “group”:”dubbo”,
    “anyhost”:”true”
    },
    “canonicalName”:”tech.joymo.centerorder.facade.product.OrderProductFacade”,
    “codeSource”:”jar:file:/home/appuser/app.jar!/BOOT-INF/lib/centerorder-client-1.0.0-SNAPSHOT.jar!/“,
    “methods”:[
    {
    “name”:”queryOrderProductByOrderNo”,
    “parameterTypes”:[
    “java.lang.String”
    ],
    “returnType”:”tech.joymo.common.caja.base.common.Result”
    }
    ],
    “types”:[
    {
    “type”:”int”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    {
    “type”:”java.lang.Object”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    {
    “type”:”tech.joymo.common.caja.base.common.Result”,
    “properties”:{
    “msg”:{
    “type”:”java.lang.String”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    “trace”:{
    “type”:”java.lang.String”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    “code”:{
    “type”:”java.lang.String”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    “data”:{
    “type”:”java.lang.Object”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    “status”:{
    “type”:”java.lang.Integer”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    }
    },
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    {
    “type”:”char”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    {
    “type”:”java.lang.Integer”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    },
    {
    “type”:”java.lang.String”,
    “typeBuilderName”:”org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder”
    }
    ]
    }


    Dubbo 泛化调用

    | Java
    package tech;

    import com.alibaba.fastjson.JSON;
    import org.apache.dubbo.config.ApplicationConfig;
    import org.apache.dubbo.config.ReferenceConfig;
    import org.apache.dubbo.config.RegistryConfig;
    import org.apache.dubbo.rpc.service.GenericService;

    /**
    * @author liping.li@joymo.tech
    * @date 2021-04-10
    */
    public class InvokeDemoTest {

    1. public static void main(String[] args) {<br /> ApplicationConfig applicationConfig = new ApplicationConfig();<br /> applicationConfig.setName("centerorder-servic");<br /> RegistryConfig registryConfig = new RegistryConfig();<br /> registryConfig.setAddress("zookeeper://zk1test2.joymo.tech:5181?backup=zk2test2.joymo.tech:5181,zk3test2.joymo.tech:5181");
    2. ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();<br /> referenceConfig.setGeneric("true");
    3. referenceConfig.setInterface("tech.joymo.centerorder.facade.product.OrderProductFacade");<br /> referenceConfig.setApplication(applicationConfig);<br /> referenceConfig.setVersion("1.0.0");<br /> referenceConfig.setTimeout(10000);<br /> referenceConfig.setGroup("dubbo");
    4. referenceConfig.setRegistry(registryConfig);
    5. System._out_.println(referenceConfig.toString());<br /> GenericService genericService = referenceConfig.get();<br /> Object result = genericService.$invoke("queryOrderProductByOrderNo", new String[]{"java.lang.String"}, new Object[]{"NO20210409203900000340"});
    6. System._out_.println(" ---------------- result -------------");<br /> System._out_.println(JSON._toJSONString_(result));<br /> }

    } | | —- |

    运行结果
    Java获取ZK上Dubbo服务信息及泛化调用 - 图2
    详细入门
    Dubbo 泛化测试