swagger结合dubbo的rest服务测试
swagger结合dubbo的rest服务测试
背景介绍
我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的swagger结合controller的测试页面,而swagger-dubbo就可以满足这个自动化测试场景需求。
准备知识
dubbo、swagger、spring
配置
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>
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>
工程jar依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>swagger-dubbo</artifactId>
<version>2.0.3</version>
</dependency>
使用
启动服务,访问链接http://127.0.0.1:8080/distv2/index.html,出现swagger的页面,并且输入配置json地址http://127.0.0.1:8080/swagger-dubbo/api-docs,查看显示的接口应该是你服务导出的所有dubbo接口
默认dubbo接口或实现类不加任何swagger的注解(比如Api,ApiParam等),则只取到类的基本信息,包括类名、方法、参数名称,所以信息需要自己添加swagger的注解到接口方法上面
选择一个接口测试,基本参数直接输入,复杂对象输入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服务测试的更多相关文章
- 基于开源Dubbo分布式RPC服务框架的部署整合
一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...
- 12个强大的Web服务测试工具
在过去的几年中,web服务或API的普及和使用有所增加. web服务或API是程序或软件组件的集合,可以帮助应用程序进行交互或通过形成其他应用程序或服务器之间的连接执行一些进程/事务处理.基本上有两种 ...
- 【Rpc】基于开源Dubbo分布式RPC服务框架的部署整合
一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...
- Dubbo消费方服务调用过程源码分析
参考:dubbo消费方服务调用过程源码分析dubbo基于spring的构建分析Dubbo概述--调用过程dubbo 请求调用过程分析dubbo集群容错机制代码分析1dubbo集群容错策略的代码分析2d ...
- 分布式应用开发 | SpringBoot+dubbo+zookeeper实现服务注册发现 | 远程服务调用
前言 通过新建两个独立服务--提供者.消费者,模拟两个独立分布的应用,通过使用dubbo+zookeeper来实现远程服务调用. 目录 项目搭建 provider-server consumer-se ...
- vs自带服务测试工具
在vs安装目录有一个vs自带的服务测试工具,地址为: "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Wcf ...
- 基于Nginx和Zookeeper实现Dubbo的分布式服务
一.前言 公司的项目基于阿里的Dubbo 微服务框架开发.为了符合相关监管部门的安全要求,公司购买了华东1.华东2两套异地服务器,一套是业务服务器,一套是灾备服务器.准备在这两套服务器上实现 Dubb ...
- Dubbo中暴露服务的过程解析
dubbo暴露服务有两种情况,一种是设置了延迟暴露(比如delay="5000"),另外一种是没有设置延迟暴露或者延迟设置为-1(delay="-1"): 设置 ...
- centos HA高可用集群 heartbeat搭建 heartbeat测试 主上停止heartbeat服务 测试脑裂 两边都禁用ping仲裁 第三十二节课
centos HA高可用集群 heartbeat搭建 heartbeat测试 主上停止heartbeat服务 测试脑裂 两边都禁用ping仲裁 第三十二节课 heartbeat是Linu ...
随机推荐
- Nginx与前端开发
Nginx与Node.js "Nginx是一款轻量级的HTTP服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡." 作为前 ...
- Python菜鸟快乐游戏编程_pygame(1)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Angular记录(11)
开始使用Angular写页面 使用WebStorm:版本2018.3.5 官网资料 资料大部分有中文翻译,很不错 速查表:https://www.angular.cn/guide/cheatsheet ...
- python之路(8)常用模块
目录 os模块 sys模块 json模块 pickle模块 xml模块 re模块 logging模块 configparser模块 hashlib模块 time模块 random模块 subproce ...
- jq选择器(jq 与 js 互相转换),jq操作css样式 / 文本内容, jq操作类名,jq操作全局属性,jq获取盒子信息,jq获取位置信息
jq选择器(jq 与 js 互相转换) // 获取所有的页面元素jq对象 $('css3选择器语法'); var $box = $(".box:nth-child(1)"); 获取 ...
- uCosII中的任务
任务基本概念 任务是一个接受操作系统管理的独立运行单元,在uCosII中类似与普通平台上的main()函数,需要自己来保护其因调用或中断二产生的断点,所以需要一个自己的私有堆栈,即任务堆栈: 任务有两 ...
- #0 scrapy爬虫学习中遇到的坑记录
python 基础学习中对于scrapy的使用遇到了一些问题. 首先进行的是对Amazon.cn的检索结果页进行爬取,很顺利,无碍. 下一个目标是对baidu的搜索结果进行爬取 1,反爬虫 1.1 我 ...
- FCN网络
https://www.cnblogs.com/gujianhan/p/6030639.html
- C语言strcpy,strncpy和strlcpy讲解
前言 C风格的字符串处理函数有很多,如strcpy().strcat()等等. strcpy与strcat char* strcpy (char* dest, const char* src); ch ...
- Luogu CF451E Devu and Flowers 题解报告
题目传送门 [题目大意] 有n种颜色的花,第i种颜色的花有a[i]朵,从这些花中选m朵出来,问有多少种方案?答案对109+7取模 [思路分析] 这是一个多重集的组合数问题,答案就是:$$C_{n+m- ...