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. NOI2009 管道取珠 神仙DP

    原题链接 原题让求的是\(\sum\limits a_i^2\),这个东西直接求非常难求.我们考虑转化一下问题. 首先把\(a_i^2\)拆成\((1+1+...+1)(1+1+...+1)\),两个 ...

  2. 深入了解SQL的四种连接&不然要命的!

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

  3. 【深度学习】RNN | GRU | LSTM

    目录: 1.RNN 2.GRU 3.LSTM 一.RNN 1.RNN结构图如下所示: 其中: $a^{(t)} = \boldsymbol{W}h^{t-1} + \boldsymbol{W}_{e} ...

  4. 03--STL序列容器(Deque)

    一:Deque双端队列<头尾操作> stack和queue是在Deque的基础上改进的,所以先介绍双端队列Deque     deque是“double-ended queue”的缩写,和 ...

  5. Python语言的循环语句、迭代器与生成器、函数学习

    while循环语句 无限循环 我们可以通过设置条件表达式永远不为false来实现无限循环,实例如下: for语句 Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串 Python ...

  6. ubuntu18系统美化

    1. 将选定的背景图片 login-bg.jpg 移动到 /usr/share/backgrounds/ 目录下 sudo mv currentdir/mypicture.jpg /usr/share ...

  7. [算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

    Description: Xiangqi is one of the most popular two-player board games in China. The game represents ...

  8. iview-admin安装

    桌面创建project文件夹. 文件夹内右键选择gitbash here,输入git init.文件夹内会生成.git文件夹. 再输入git config --global user.name &qu ...

  9. uwsgi+anaconda+nginx部署django项目(ubuntu下)

    conda 环境不必多说: conda(或source)  activate  test 进入test虚拟环境 接下来安装uwsgi: pip install uwsgi 在conda环境下大概率安装 ...

  10. 第31月第25天 xcode debug 限制uitextfiled输入

    1.xcode debug 了解了每个设置的意思,个人觉得对于一个普通的app来说可以这样配置这些设置: Generate Debug Symbols:DEBUG和RELEASE下均设为YES(和Xc ...