注意:

  如果你正在研究微服务,那必然少不了服务之间的相互调用,哪么服务之间的接口以及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中添加如下坐标

     <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

  二、创建配置类

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

 @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zyc.controller"))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Hello系统api")
.description("Hello系统接口文档说明")
.contact(new Contact("vker", "", "6492178@gmail.com"))
.version("1.0")
.build();
} @Bean
UiConfiguration uiConfig() {
return new UiConfiguration(null, "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}

   三、启动类加注解

    @EnableSwagger2

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

    

 @Api("Hello接口")
@RestController
@RequestMapping("/company")
public class Hello { @ApiOperation(value="/get/hello方法")
@GetMapping("/get/hello")
public String hello() {
return "Hello Zuul springBoot-microcloud-provider-company";
} }

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

 @Api() 用于类;表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,可以使用tags替代 @ApiOperation() 用于方法;表示一个http请求的操作
value用于方法描述
notes用于提示内容 @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填 @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
value–表示对象名 @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏 @ApiImplicitParam() 用于方法
表示单独的请求参数 @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
name–参数ming
value–参数说明
dataType–数据类型
paramType–参数类型
example–举例说明 @ApiIgnore
作用于方法上,使用这个注解swagger将忽略这个接口

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

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

 @Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider{ @Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("Hello接口", "/study-proxy/company-proxy/v2/api-docs", "1.0"));
resources.add(swaggerResource("检查系统接口", "/study-proxy/prevention-check/v2/api-docs", "1.0"));
return resources;
} private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}

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

  

 spring:
application:
name: springBoot-user-zuul
zuul:
prefix: /study-proxy
ignored-services: "*"
routes:
microcloud-provider-company: /company-proxy/**
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. Dynamics 365 安装问题——无法访MSCRM_CONFIG数据库

    1.  问题 2.  原因 出现此问题的一个或多个下列条件都为真︰ 在多务器中安装 Microsoft Dynamics 365.然后,直接在运行 Microsoft SQL Server 的服务器上 ...

  2. 独角兽估值30亿美金,我们聊聊RPA是什么

    https://www.jianshu.com/p/397ecd238ffc 缩短法定工作时间,已成国际劳动立法趋势,全球政府都曾面对这样的议题,过往企业IT也在思考这件事,开发出更好的软件系统帮助员 ...

  3. python zlib模块缺失报错:RuntimeError: Compression requires the (missing) zlib module

    解决方式: # yum install zlib # yum install zlib-devel 下载成功后,进入python2.7的目录,重新执行 #make #make install 此时先前 ...

  4. java将图片输出base64位码显示

    注意需要过滤:\r \n数据 jkd1.7的 import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; /** * 网络图片转换Base ...

  5. Don't rely on luck.

    https://www.codewars.com/kata/dont-rely-on-luck/train/javascript 答案: 重写 Math.floor = function () ... ...

  6. Codeforces 1163E Magical Permutation [线性基,构造]

    codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...

  7. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  8. python实现:判断某一天是那一年中的第几天

    方法1:先判断是否是闰年,然后再利用求和,得出某一天是第几天 # 方法1:low版 def func1(year, month, day): # 分别创建平年,闰年的月份天数列表(注意列表下标从0开始 ...

  9. 原创:史上对BM25模型最全面最深刻的解读以及lucene排序深入讲解

    垂直搜索结果的优化包括对搜索结果的控制和排序优化两方面,其中排序又是重中之重.本文将全面深入探讨垂直搜索的排序模型的演化过程,最后推导出BM25模型的排序.然后将演示如何修改lucene的排序源代码, ...

  10. lnmp环境脚本快速搭建

    进入lnmp官网 https://lnmp.org/download.html 如图: 进入Linux服务器并执行wget命令下载 wget http://soft.vpser.net/lnmp/ln ...