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


    注意:

      如果你正在研究微服务,那必然少不了服务之间的相互调用,哪么服务之间的接口以及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 
    10 @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
    11 name–参数名 
    12 value–参数说明 
    13 required–是否必填
    14 
    15 @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收 
    16 value–表示对象名 
    17 
    18 @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改 
    19 value–字段说明 
    20 name–重写属性名字 
    21 dataType–重写属性类型 
    22 required–是否必填 
    23 example–举例说明 
    24 hidden–隐藏
    25 
    26 @ApiImplicitParam() 用于方法 
    27 表示单独的请求参数
    28 
    29 @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam 
    30 name–参数ming 
    31 value–参数说明 
    32 dataType–数据类型 
    33 paramType–参数类型 
    34 example–举例说明
    35 
    36 @ApiIgnore
    37 作用于方法上,使用这个注解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

  • 相关阅读:
    Qt Creator 安装SDK,在MSVC编译模式下使用CDB调试器
    QString与std::string的相互转换
    白话代码中的复杂度分析-大O复杂度表示法 时间,空间复杂度分析 最好,最坏,平均复杂度
    QT 如何使窗体初始最大化
    make_ext4fs
    Qt5.4中遇到找不到头文件<QApplication>等
    MariaDB 数据库的备份
    MariaDB -- 数据类型
    MariaDB基础操作
    keepalived + lvs 网站高可用集群
  • 原文地址:https://www.cnblogs.com/zyc-blogs/p/9183434.html
Copyright © 2020-2023  润新知