swagger结合dubbo的rest服务测试

背景介绍

我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的swagger结合controller的测试页面,而swagger-dubbo就可以满足这个自动化测试场景需求。

准备知识

dubbo、swagger、spring

配置

  1. web.xml配置springmvc的DispatcherServlet

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" metadata-complete="true"> <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:application/*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> <servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping> </web-app>
  2. example-servlet.xml配置springmvc组件

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"> <mvc:annotation-driven>
    <!-- 支持fastjson -->
    <!-- <mvc:message-converters>
    <bean
    class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    <property name="supportedMediaTypes">
    <list>
    <value>application/json;charset=UTF-8</value>
    </list>
    </property>
    </bean>
    </mvc:message-converters> -->
    </mvc:annotation-driven>
    <context:annotation-config />
    <context:component-scan base-package="com.deepoove.swagger.dubbo.example" />
    <context:property-placeholder /> <!-- <context:property-placeholder location="classpath*:swagger-dubbo.properties" /> --> <bean class="com.deepoove.swagger.dubbo.spring.SwaggerDubboConfig" />
    <bean class="com.deepoove.swagger.dubbo.example.AnnotationScanConfig" /> <mvc:resources location="/dist/" mapping="/dist/**" />
    <mvc:resources location="/distv2/" mapping="/distv2/**" /> <!-- 跨域支持 -->
    <mvc:cors>
    <mvc:mapping path="/swagger-dubbo/**" allowed-origins="*" />
    <mvc:mapping path="/h/**" allowed-origins="*" />
    </mvc:cors> </beans>
  3. 工程jar依赖

    <dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>swagger-dubbo</artifactId>
    <version>2.0.3</version>
    </dependency>

使用

  1. 启动服务,访问链接http://127.0.0.1:8080/distv2/index.html,出现swagger的页面,并且输入配置json地址http://127.0.0.1:8080/swagger-dubbo/api-docs,查看显示的接口应该是你服务导出的所有dubbo接口

  2. 默认dubbo接口或实现类不加任何swagger的注解(比如Api,ApiParam等),则只取到类的基本信息,包括类名、方法、参数名称,所以信息需要自己添加swagger的注解到接口方法上面

  3. 选择一个接口测试,基本参数直接输入,复杂对象输入json串即可,如有访问不通,基本是跨域问题,配置host即可

基本原理

  • com.deepoove.swagger.dubbo.spring.SwaggerDubboConfig配置类
  • org.springframework.context.annotation.ConfigurationClassPostProcessor注解配置bean解析类
  • com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan,dubbo扫包配置注解
  • com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory,dubbo spring扩展工厂

代码参考

  • https://github.com/yaojf/swagger-dubbo
  • 相对于fork的源码主要改动点,针对spring里面的动态代理类,获取动态代理类本身,获取具体的参数名称,并提供统一的配置bean
  • 增加swagger.dubbo.open配置参数,对是否开启swagger-dubbo最开关,默认关闭

swagger结合dubbo的rest服务测试的更多相关文章

  1. 基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  2. 12个强大的Web服务测试工具

    在过去的几年中,web服务或API的普及和使用有所增加. web服务或API是程序或软件组件的集合,可以帮助应用程序进行交互或通过形成其他应用程序或服务器之间的连接执行一些进程/事务处理.基本上有两种 ...

  3. 【Rpc】基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  4. Dubbo消费方服务调用过程源码分析

    参考:dubbo消费方服务调用过程源码分析dubbo基于spring的构建分析Dubbo概述--调用过程dubbo 请求调用过程分析dubbo集群容错机制代码分析1dubbo集群容错策略的代码分析2d ...

  5. 分布式应用开发 | SpringBoot+dubbo+zookeeper实现服务注册发现 | 远程服务调用

    前言 通过新建两个独立服务--提供者.消费者,模拟两个独立分布的应用,通过使用dubbo+zookeeper来实现远程服务调用. 目录 项目搭建 provider-server consumer-se ...

  6. vs自带服务测试工具

    在vs安装目录有一个vs自带的服务测试工具,地址为: "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Wcf ...

  7. 基于Nginx和Zookeeper实现Dubbo的分布式服务

    一.前言 公司的项目基于阿里的Dubbo 微服务框架开发.为了符合相关监管部门的安全要求,公司购买了华东1.华东2两套异地服务器,一套是业务服务器,一套是灾备服务器.准备在这两套服务器上实现 Dubb ...

  8. Dubbo中暴露服务的过程解析

    dubbo暴露服务有两种情况,一种是设置了延迟暴露(比如delay="5000"),另外一种是没有设置延迟暴露或者延迟设置为-1(delay="-1"): 设置 ...

  9. centos HA高可用集群 heartbeat搭建 heartbeat测试 主上停止heartbeat服务 测试脑裂 两边都禁用ping仲裁 第三十二节课

    centos   HA高可用集群  heartbeat搭建 heartbeat测试  主上停止heartbeat服务  测试脑裂  两边都禁用ping仲裁  第三十二节课 heartbeat是Linu ...

随机推荐

  1. dsu on tree 树上启发式合并 学习笔记

    近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...

  2. 在IntelliJ IDEA中,注解@Slf4j找不到log

    问题: 解决方法:

  3. [Leetcode] 01 Matrix

    问题: https://leetcode.com/problems/01-matrix/#/description 基本思路:广度优先遍历,根据所有最短距离为N的格找到所有距离为N+1的格,直到所有的 ...

  4. 浅谈 JSP & Servlet

    body { text-align: center; } div.develon { background-color: #cccccc; font-size: 20px; } 背景 相信大家都见过这 ...

  5. Python 数据分析4

    本章概要 数据加载.存储与文件格式 数据加载.存储与文件格式 读取文本格式数据 read_csv 默认是按照逗号分割,也可设定其他分割符 df = pd.read_csv('file', sep='| ...

  6. Jmeter、Postman 、 loadrunner SoapUI 接口测试工具

    一. loadrunner  简称 LR 二. Jmeter 1.安装包:apache-jmeter-4.0.tgz   解压.学会此工具的使用  和POSTman 一样的. 2.本机测试:双击apa ...

  7. java学习笔记09-类与对象

    物以类聚,人以群分,我们把具有相似特性或品质的物体归为一类. 类:类是一种模板,它描述一类对象的行为和状态. 对象:对象是类的一个实例,有状态和行为. 比如在一支nba球队中,每个球员都有球衣号码,场 ...

  8. Mock.js简易教程,脱离后端独立开发,实现增删改查功能(转)

    在我们的生产实际中,后端的接口往往是较晚才会出来,并且还要写接口文档,于是我们的前端的许多开发都要等到接口给我们才能进行,这样对于我们前端来说显得十分的被动,于是有没有可以制造假数据来模拟后端接口呢, ...

  9. 【easy】189. Rotate Array

    题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...

  10. MAC本apache+php配置虚拟域名时踩的坑

    昨天在调试Mac自带的Apache+PHP配置域名时,调试的让我怀疑人生.顿时心里一万个草泥马,我就是配置个虚拟域名啊,这么让我受伤 . 1 首先检查一下Apache是否开启, qutao@bogon ...