Spring Cloud Zuul集成Swagger实现过程解析

作者:dreamstar 时间:2021-05-26 12:36:09 

Spring Cloud Zuul 集成Swagger

1.准备服务注册中心eureka-server

2.创建微服务swagger-service-a

step1. 创建微服务swagger-service-a(Spring Boot项目),添加eureka-client起步依赖,web起步依赖 和swagger依赖


<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
   <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
   </dependency>
   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.9.2</version>
   </dependency>

step2.在配置类添加注解@EnableDiscoveryClient ,,将当前应用 添加到 服务治理体系中,开启微服务注册与发现。

step3.配置swagger


package com.example.swaggerservicea;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
 @Bean
 public Docket api() {

return new Docket(DocumentationType.SWAGGER_2)
       .apiInfo(apiInfo())
       .select()
       .apis(RequestHandlerSelectors.any())
       .paths(PathSelectors.any()).build();
 }

private ApiInfo apiInfo() {
   return new ApiInfoBuilder()
       .title("swagger-service-a 实例文档")
       .description("swagger-service-a 实例文档 1.0")
       .termsOfServiceUrl("https:github")
       .version("1.0")
       .build();
 }

}

step4.application.properties文件中添加配置

#微服务基本信息
spring.application.name=swagger-service-a
server.port=10010
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example

step5.添加一个微服务提供的功能


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AaaController {
 @Autowired
 DiscoveryClient discoveryClient;

@GetMapping("/service-a")
 public String dc() {
   String services = "service-a Services: " + discoveryClient.getServices();
   System.out.println(services);
   return services;
 }
}

step6.启动微服务,访问 http://localhost:10010/swagger-ui.html

3.创建微服务swagger-service-b (参考swagger-service-a ), 启动微服务swagger-service-b并访问http://localhost:10020/swagger-ui.html

4.构建API网关并整合Swagger

step1.创建API网关微服务swagger-api-gateway,添加eureka-client起步依赖,zuul起步依赖 和 swagger依赖 :spring-cloud-starter-netflix-eureka-client,spring-cloud-starter-netflix-zuul


<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
   </dependency>
   <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
   </dependency>
   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.9.2</version>
   </dependency>

step2.在配置类添加注解@SpringCloudApplication ,@EnableZuulProxy

step3.配置swagger


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
 @Bean
 public Docket api() {

return new Docket(DocumentationType.SWAGGER_2)
       .apiInfo(apiInfo())
       .select()
       .apis(RequestHandlerSelectors.any())
       .paths(PathSelectors.any()).build();
 }

private ApiInfo apiInfo() {
   return new ApiInfoBuilder()
       .title("swagger-service 实例文档")
       .description("swagger-service 实例文档 1.0")
       .termsOfServiceUrl("https:github")
       .version("1.0")
       .build();
 }

}

step4.配置swagger


package com.example.swaggerapigateway;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
 @Override
 public List<SwaggerResource> get() {
   List resources = new ArrayList<>();
   resources.add(swaggerResource("service-a", "/swagger-service-a/v2/api-docs", "2.0"));
   resources.add(swaggerResource("service-b", "/swagger-service-b/v2/api-docs", "2.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;
 }
}

step5.application.properties文件中添加配置

#微服务基本信息
spring.application.name=swagger-api-gateway
server.port=10030
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example

step6.启动微服务,访问http://localhost:10030/swagger-ui.html

Spring Cloud Zuul集成Swagger实现过程解析

来源:https://www.cnblogs.com/dreamstar99/p/13857267.html

标签:Spring,Cloud,Zuul,集成,Swagger
0
投稿

猜你喜欢

  • Java实现常见排序算法的优化

    2022-05-20 13:08:37
  • Android 6.0指纹识别App开发案例

    2021-06-05 10:30:07
  • Android Studio中Logcat写入和查看日志

    2021-08-05 12:37:18
  • JavaScript中栈和队列应用详情

    2023-05-01 00:25:52
  • C#异常处理总结及简单实例

    2022-10-02 13:48:30
  • 弹出一个带确认和取消的dialog实例

    2023-11-22 06:07:13
  • java如何通过IP解析地理位置

    2021-09-11 08:01:07
  • Java实现文件分割和文件合并实例

    2022-12-15 17:18:35
  • C#实现Winform小数字键盘模拟器

    2021-08-29 12:34:25
  • c# 实现KMP算法的示例代码

    2023-12-02 06:35:19
  • Java Spring5学习之JdbcTemplate详解

    2023-11-25 20:17:23
  • Android实战打飞机游戏之怪物(敌机)类的实现(4)

    2021-07-26 09:13:41
  • C# 的析构以及垃圾回收实例分析

    2021-12-22 19:03:37
  • 初识MyBatis及基本配置和执行

    2021-11-12 05:53:40
  • 关于idea中SpringBoot启动失败的坑

    2022-07-18 13:02:24
  • RadioButton实现选择后可取消选择

    2023-10-15 23:50:39
  • Android编程中File文件常见存储与读取操作demo示例

    2021-11-24 18:33:20
  • SpringBoot多数据源配置详细教程(JdbcTemplate、mybatis)

    2023-08-26 01:59:33
  • java使用归并删除法删除二叉树中节点的方法

    2022-03-31 23:06:12
  • 解析JAVA深度克隆与浅度克隆的区别详解

    2023-11-02 10:57:28
  • asp之家 软件编程 m.aspxhome.com