注意:

  如果你正在研究微服务,那必然少不了服务之间的相互调用,哪么服务之间的接口以及api就必须生成系统的管理文档了。如果你希望更好的管理你的API,你希望有一个工具能一站式地解决API相关的所有事情,那么,swagger将是一个不错的选择,以下就为大家介绍swagger是使用方法,如有不对之处,还望指正!

1、项目结构

  springBoot-user-zuul-swagger   — zuul网关 端口   9501

   ls-prevention-check       —   服务1         端口   8091

  microcloud-provider-company     —   服务2         端口 8105

2、实现放法

  一、添加依赖

  分别在三个服务中添加Swagger需要依赖两个jar包,在pom.xml中添加如下坐标

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.7.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.7.0</version>
  10. </dependency>

  二、创建配置类

    分别在服务提供者项目中加入Swagger一个配置类来进行对swagger的基本配置,

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig {
  4.  
  5. @Bean
  6. public Docket createRestApi() {
  7. return new Docket(DocumentationType.SWAGGER_2)
  8. .apiInfo(apiInfo())
  9. .select()
  10. .apis(RequestHandlerSelectors.basePackage("com.zyc.controller"))
  11. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
  12. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  13. .paths(PathSelectors.any())
  14. .build();
  15. }
  16.  
  17. private ApiInfo apiInfo() {
  18. return new ApiInfoBuilder()
  19. .title("Hello系统api")
  20. .description("Hello系统接口文档说明")
  21. .contact(new Contact("vker", "", "6492178@gmail.com"))
  22. .version("1.0")
  23. .build();
  24. }
  25.  
  26. @Bean
  27. UiConfiguration uiConfig() {
  28. return new UiConfiguration(null, "list", "alpha", "schema",
  29. UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
  30. }
  31. }

   三、启动类加注解

    @EnableSwagger2

  四、在需要生成的类或方法上加具体注解

    

  1. @Api("Hello接口")
  2. @RestController
  3. @RequestMapping("/company")
  4. public class Hello {
  5.  
  6. @ApiOperation(value="/get/hello方法")
  7. @GetMapping("/get/hello")
  8. public String hello() {
  9. return "Hello Zuul springBoot-microcloud-provider-company";
  10. }
  11.  
  12. }

  注意:这里只是一个小栗子:详细注解含义如下:

  1. @Api() 用于类;表示标识这个类是swagger的资源
  2. tags–表示说明
  3. value–也是说明,可以使用tags替代
  4.  
  5. @ApiOperation() 用于方法;表示一个http请求的操作
  6. value用于方法描述
  7. notes用于提示内容
  8.  
  9. @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
  10. name–参数名
  11. value–参数说明
  12. required–是否必填
  13.  
  14. @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
  15. value–表示对象名
  16.  
  17. @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
  18. value–字段说明
  19. name–重写属性名字
  20. dataType–重写属性类型
  21. required–是否必填
  22. example–举例说明
  23. hidden–隐藏
  24.  
  25. @ApiImplicitParam() 用于方法
  26. 表示单独的请求参数
  27.  
  28. @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
  29. name–参数ming
  30. value–参数说明
  31. dataType–数据类型
  32. paramType–参数类型
  33. example–举例说明
  34.  
  35. @ApiIgnore
  36. 作用于方法上,使用这个注解swagger将忽略这个接口

  五、重点:在springBoot-user-zuul-swagger项目中添加文档资源配置类DocumentationConfig

       注意:springBoot-user-zuul-swagger必须集成Zuul作为路由访问

  1. @Component
  2. @Primary
  3. public class DocumentationConfig implements SwaggerResourcesProvider{
  4.  
  5. @Override
  6. public List<SwaggerResource> get() {
  7. List resources = new ArrayList<>();
  8. resources.add(swaggerResource("Hello接口", "/study-proxy/company-proxy/v2/api-docs", "1.0"));
  9. resources.add(swaggerResource("检查系统接口", "/study-proxy/prevention-check/v2/api-docs", "1.0"));
  10. return resources;
  11. }
  12.  
  13. private SwaggerResource swaggerResource(String name, String location, String version) {
  14. SwaggerResource swaggerResource = new SwaggerResource();
  15. swaggerResource.setName(name);
  16. swaggerResource.setLocation(location);
  17. swaggerResource.setSwaggerVersion(version);
  18. return swaggerResource;
  19. }
  20. }

  六、在springBoot-user-zuul-swagger的application.yml中添加相关配置:

  

  1. spring:
  2. application:
  3. name: springBoot-user-zuul
  4. zuul:
  5. prefix: /study-proxy
  6. ignored-services: "*"
  7. routes:
  8. microcloud-provider-company: /company-proxy/**
  9. ls-prevention-check: /prevention-check/**

  七、访问http://localhost:9501/swagger-ui.html

微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上的更多相关文章

  1. Spring Cloud Zuul与网关中间件

    Spring Cloud Zuul与网关中间件_网易订阅 http://dy.163.com/v2/article/detail/DC7L8UV10511HSJK.html

  2. 浅谈服务架构“五脏六腑”之Spring Cloud

    本文将从 Spring Cloud 出发,分两小节讲述微服务框架的「五脏六腑」: 第一小节「服务架构」旨在说明的包括两点,一服务架构是什么及其必要性:二是服务架构的基本组成.为什么第一节写服务架构而不 ...

  3. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

    目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...

  4. 【spring cloud】spring cloud zuul 路由网关

    GitHub源码地址:https://github.com/AngelSXD/springcloud 版本介绍: <properties> <project.build.source ...

  5. 如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上

    本文不涉及技术,只是单纯的一个小技巧. 阅读本文前,你需要对spring-cloud-zuul.spring-cloud-eureka.以及swagger的配置和使用有所了解. 如果你的系统也是用zu ...

  6. 微服务架构下的API网关

    顾名思义,是出现在系统边界上的一个面向API的.串行集中式的强管控服务,这里的边界是企业IT系统的边界,主要起到隔离外部访问与内部系统的作用.在微服务概念的流行之前,API网关的实体就已经诞生了,例如 ...

  7. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  8. Spring Cloud Zuul性能调整

    Spring Cloud 版本: Dalston.SR5 这两天通过JMeter测了一下Spring Cloud Zuul的性能,用的是两台虚机8核8G和4核8G,宿主机是10核逻辑20核,代理的服务 ...

  9. Spring Cloud 微服务二:API网关spring cloud zuul

    前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...

随机推荐

  1. 15 分钟学会使用 Git 和远程代码库

    Git是个了不起但却复杂的源代码管理系统.它能支持复杂的任务,却因此经常被认为太过复杂而不适用于简单的日常工作.让我们诚实一记吧:Git是复杂的,我们不要装作它不是.但我仍然会试图教会你用(我的)基本 ...

  2. EntityFramework6 学习笔记(三)

    你可能要问,我用EF不就为了避免写SQL吗?如果要写SQL我不如直接用ADO.NET得了.话虽然这么说没错,可有些时候使用EF操作数据还是有一些不方便,例如让你根据条件删除一组记录,如果按照正常的流程 ...

  3. OLED液晶屏幕(2)取模软件

    https://blog.csdn.net/ling3ye/article/details/53399305 文件夹说明: Adafruit_SSD1306-master   ——SSD1306库(O ...

  4. webpack的plugin原理

    plugin是webpack生态的重要组成,它为用户提供了一种可以直接访问到webpack编译过程的方式.它可以访问到编译过程触发的所有关键事件. 1. 基本概念 1. 如何实现一个插件 1. plu ...

  5. js中回调函数,promise 以及 async/await 的对比用法 对比!!!

    在编程项目中,我们常需要用到回调的做法来实现部分功能,那么在js中我们有哪些方法来实现回调的? 方法1:回调函数 首先要定义这个函数,然后才能利用回调函数来调用! login: function (f ...

  6. Python爬虫 | 多线程、多进程、协程

    对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程,打开一个Word就启动了 ...

  7. 2019.12.09 java循环(while)

    class Demo04 { public static void main(String[] args) { int sum=0; int i=1; while(i<=100){ //sum ...

  8. mysql 创建分组

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  9. 第06组 Beta版本演示

    队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/12052263.html 作业博客 : https://edu.cnblogs.com/campus ...

  10. 关于资源获取(请把https改为http)

    所有demo以及资源获取,请把https改为http.