dubbo是阿里巴巴开源的一套rpc方案,以为理念很契合微服务,这几年很火,用户里面不凡京东,当当,去哪儿等大公司。
rpc场景

 

dubbo架构

 

官网也提供了一个很简单实用的demo来演示dubbo协议的使用,用起来的确很简单强大。

dubbo demo

可参考 http://dubbo.io/ 首页的例子已经很好了。

基于telnet的简单调试接口

任何一个dubbo服务都支持一个简单的telent交互。比如

telnet localhost 20880
>ls -l
> ls -l DemoService
> invoke DemoSerivce.sayHello("seveniruby")

这种方式只能用来简单验证接口的可用

传统的基于xml配置的dubbo的测试方法

首先创建一个xml文件放到resources下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="dubbo://127.0.0.1:9090"/>
<dubbo:reference id="demoService" interface="com.testerhome.tapi.dubbo.DemoService"/>
</beans>

通过使用一份xml配置文件进行测试

test("dubbo use registy xml"){
val context = new ClassPathXmlApplicationContext("dubbo/consumer.xml")
context.start()
val demoService = context.getBean("demoService").asInstanceOf[DemoService]
println(demoService.sayHello("seveniruby")) val req=new RequestModel()
req.setName("james")
req.getChild.setName("lily")
println(TData.toJson(demoService.reqModel(req)))
}

基于api的dubbo测试方法

其实除了xml配置之外,官方也提供了一份直接通过api进行配置的方式,这个方式无疑是可编程比较灵活的

test("dubbo use registry"){
// 当前应用配置
val application = new ApplicationConfig
application.setName("yyy") // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接 // 引用远程服务
val reference = new ReferenceConfig[DemoService] // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application)
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(classOf[DemoService])
//reference.setUrl("dubbo://127.0.0.1:20881")
reference.setTimeout(5000) // 和本地bean一样使用DemoService
val DemoService = reference.get // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
System.out.println(DemoService.sayHello("seveniruby")) val req=new RequestModel()
req.setName("james")
req.getChild.setName("lily")
System.out.println(TData.toJson(DemoService.reqModel(req)))
}

泛化调用

官方原话是

泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 GenericService 调用所有服务实现。

这种情况适合自己打造接口测试框架使用。以上2个方式都需要依赖研发提供的dubbo接口的jar包,这无疑会增加项目的负担。
使用泛化可以不依赖任何研发提供的jar包,不过缺点也明显,仍然需要jar包或者其他的文档去分析dubbo接口的调用参数信息。
例子

test("泛化调用 by provider conf use map"){

  var reference = new ReferenceConfig[GenericService]() // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
reference.setGeneric(true) // 声明为泛化接口
reference.setApplication(new ApplicationConfig("generic-provider"))
//reference.setRegistry(registry)
reference.setInterface("com.testerhome.tapi.dubbo.DemoService") // 弱类型接口名
reference.setTimeout(5000)
reference.setUrl(s"dubbo://127.0.0.1:20881") val genericService = reference.get
val result = genericService.$invoke("sayHello", Array("java.lang.String"), Array("xxxx".asInstanceOf[AnyRef]))
log.info(result) val childMap= mutable.Map[String, AnyRef]()
childMap.put("name", "children")
val map= mutable.Map[String, AnyRef]()
map.put("name", "aaa")
map.put("id", "11")
map.put("child", childMap.asJava) val resModel=genericService.$invoke(
"reqModel",
Array("com.testerhome.tapi.dubbo.RequestModel"),
Array(map.asJava.asInstanceOf[AnyRef]))
log.info(resModel)
log.info(TData.toJson(resModel))
}

虽然看起来还是依赖jar包,不过这个依赖就挺小了。如果你技术稍微“猥琐”点,就应该可以想到,只需要借助asm之类的字节码分析框架即可自动生成接口测试用例模板了。

dubbo测试的技术关注点

  • dubbo支持很多的协议,如果用的是http或者hessian协议,他们本身是文本的,可以直接使用restassured框架进行接口测试
  • dubbo的registry保存了dubbo各种服务的注册信息,测试的时候可以直接用registry,而不是直接连接到提供服务的provider上

最新文章

  1. 再谈Jquery Ajax方法传递到action
  2. C++嵌套多个命名空间举例
  3. HDfs命令
  4. passing argument 3 of ‘wtk_hlv_rec_init’ discards ‘const’ qualifier from pointer target type
  5. [QoS]cisco3560限速配置案例-收集于网工泡泡
  6. Python Paramiko模块安装和使用
  7. 矩阵快速幂 POJ 3070 Fibonacci
  8. 【英语】Bingo口语笔记(15) - Give系列
  9. Two kinds of Quaternion SlerpImp (Unity)
  10. 《University Calculus》-chape3-微分法-基本概念、定理
  11. excel下拉级联的做法
  12. html Embed用法 页面播放视频
  13. Linux(Redhat)安装Redis
  14. 基数排序模板(基数排序,C++模板)
  15. 编写一个数组工具类, 编写本软件的 帮助文档(API文档)
  16. linux 学习笔记 文件权限管理篇
  17. luogu-1563玩具谜题
  18. call、apply、bind三者的区别
  19. nginx 更改用户组
  20. LeetCode OJ 94. Binary Tree Inorder Traversal

热门文章

  1. 再度吐槽,PHP在centos7的安装方式稍不注意可能就打击你的积极性
  2. Bootstrap-轮播图-No.2
  3. WebUI自动化之Java语言讲解
  4. 【csp模拟赛5】购物(shopping.cpp)--常规
  5. ubuntu配置pip3以及scrapy
  6. vue-cli 本地代理 造成session丢失 而登录不上去 解决办法
  7. single-pass单遍聚类方法
  8. Java中String、StringBuffer、StringBuilder
  9. 转载:在Excel中将数据库字段转换成驼峰式
  10. moogdb操作