大數據

乾貨 | Dubbo 接口測試技術,測試開發進階必備

image.png

Dubbo 接口測試為霍格沃茲測試學院特色課程,全網深度領先,想一起系統進階的同學文末加群交流。

Dubbo 是什麼?

Dubbo 是阿里巴巴開源的一套 RPC 方案,因為理念很契合微服務,這幾年很火,用戶裡面不凡京東,噹噹,去哪兒等大公司。

RPC 場景
image.png

Dubbo 架構
image.png

官網也提供了一個很簡單實用的 Demo 來演示 Dubbo 協議的使用,用起來的確很簡單強大。

Dubbo demo

首頁的例子已經很好了。可參考 http://Dubbo.io/

下面介紹幾種常用的 Dubbo 接口測試方法。

基於 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 之類的字節碼分析框架即可自動生成接口測試用例模板了。

Tips | Dubbo 測試的技術關注點

Dubbo 支持很多的協議,如果用的是 HTTP 或者 Hessian 協議,他們本身是文本的,可以直接使用 RESTAssured 框架進行接口測試;
Dubbo 的 Registry 保存了 Dubbo 各種服務的註冊信息,測試的時候可以直接用 registry,而不是直接連接到提供服務的 provider 上;

更多技術文章分享及測試資料點此獲取

Leave a Reply

Your email address will not be published. Required fields are marked *