【java】测试dubbo业务

注意:本篇文章主要是测试dubbo下的业务,并非测试dubbo框架本身

在如下情况下可以使用这种方式

  1. dubbo消费端不到服务端提供API或者jar包
  2. 想通过http接口测试dubbo
  3. 字符串和对象的互相转化

核心伪代码,利用下面的代码起一个HTTP服务


class DubboParams {
    public String url;
    public String method;
    public String params;
    public String interfaceName;
    public String type;
    public String value;

    public String getType() {
        return type;
    }

    public DubboParams setType(String type) {
        this.type = type;
        return this;
    }

    public String getValue() {
        return value;
    }

    public DubboParams setValue(String value) {
        this.value = value;
        return this;
    }

    public String getUrl() {
        return url;
    }

    public DubboParams setUrl(String url) {
        this.url = url;
        return this;
    }

    public String getMethod() {
        return method;
    }

    public DubboParams setMethod(String method) {
        this.method = method;
        return this;
    }

    public String getParams() {
        return params;
    }

    public DubboParams setParams(String params) {
        this.params = params;
        return this;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public DubboParams setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
        return this;
    }
}

 public Response runDubbo( DubboParams dubboParams){
        String url = dubboParams.getUrl();
        String method = dubboParams.getMethod();
        String interfaceName = dubboParams.getInterfaceName() ;
        String types = dubboParams.getType();
        String values = dubboParams.getValue();
        JSONArray jsonArray = JSON.parseArray(types);
        String[] parameterTypes = new String[jsonArray.size()];
        for(int i=0;i<jsonArray.size();i++ ){
            // 设置参数类型
            parameterTypes[i]  = jsonArray.get(i).toString();
        }
        JSONArray jsonArray1 = JSON.parseArray(values);
        Object[] parameterObjs = new Object[jsonArray1.size()];
        jsonArray1.toArray(parameterObjs);
        // Application Info
        ApplicationConfig application = new ApplicationConfig();
        application.setName("yyy");
        // Registry Info
        RegistryConfig registry = new RegistryConfig();
        // 无注册中心
        registry.setAddress("N/A");
// NOTES: ReferenceConfig holds the connections to registry and providers, please cache it for performance.
// Refer remote service
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // In case of memory leak, please cache.
        reference.setApplication(application);
        reference.setRegistry(registry);
        // 直连url
        reference.setUrl(url);
        reference.setInterface(interfaceName);
        reference.setGeneric(true);
// Use xxxService just like a local bean
        // 客户端泛泛调用
        GenericService genericService = reference.get();
        Object result = genericService.$invoke(method, parameterTypes, parameterObjs);
        System.out.println(result.toString());
        return Response.ok().putData(result);   
    }

利用postman调用http接口,从而实现调用dubbo接口
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Doe 发布 [V1.0.0] 前段时间排查某问题的时候,想要快速知道某些dubbo接口(三无)的响应结果,但不想启动项目(因为这些项目不是你负责的,不会部署而且超级笨重),也不想新建一个dubbo客户端项目(占地方),也不想开telnet客户端连接口(麻烦而且有限制)。所以扣了dubbo的netty模块源码,封装了个收发客户端集成一个工具,可以快速调试dubbo接口。源码地址:https://github.com/VIPJoey/doe 极简模式 普通模式 目录结构 mmc-dubbo-api 接口项目,主要用于测试。 mmc-dubbo-provider dubbo提供者项目,主要用于测试。 mmc-dubbo-doe 主项目,实现dubbo接口调试。 deploy 部署文档 功能特性 极简模式:通过dubbo提供的telnet协议收发数据。 普通模式:通过封装netty客户端收发数据。 用例模式:通过缓存数据,方便下一次操作,依赖普通模式。 增加依赖:通过调用maven命令,下载jar包和热加载到系统,主要用来分析接口方法参数,主要作用在普通模式。 依赖列表:通过分析pom文件,展示已经加载的jar包。 其它特性 springboot 整合 redis,支持spring el 表达式。 springboot 整合 thymeleaf。 springboot 整合 logback。 netty rpc 实现原理。 开发环境 jdk 1.8 maven 3.5.3 dubbo 2.6.1 lombok 1.16.20 idea 2018 windows 7 安装步骤 安装jdk 安装maven,并设置好环境变量,仓库目录。 进入mmc-dubbo-api目录,执行mvn clean install命令,省api的jar包。 进入mmc-dubbo-doe目录,执行mvn clean install 命令,在target目录生成dubbo-doe-1.0.0-RELEASE.jar 在F盘(可以任意盘)创建目录F:\app\doe 把dubbo-doe-1.0.0-RELEASE.jar拷贝到F:\app\doe 把deploy目录中的所有文件拷贝到F:\app\doe 如果您电脑安装了git bash,可以在bash窗口运行 ./deploy.sh start,否则如果没有安装git bash,只能打开cmd切换到F:\app\doe目录,然后执行java -jar dubbo-doe-1.0.0-RELEASE.jar --spring.profiles.active=prd 打开浏览器,访问地址:http://localhost:9876/doe/home/index 全剧终

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值