微服務架構下使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM