Spring Cloud Zuul如何与Swagger无缝集成,详细实现步骤是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计897个文字,预计阅读时间需要4分钟。
Spring Cloud Zuul 集成 Swagger 1.x 准备服务注册中心 Eureka Server
1. 创建微服务 Swagger-Service-A (Spring Boot 项目) - 添加 Eureka Client 起步依赖 - 添加 Web 起步依赖 - 添加 Swagger 依赖
yaml dependencies: spring-cloud-starter-netflix-eureka-client spring-boot-starter-web springfox-swagger2
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> <!-- 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("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.启动微服务,访问 localhost:10010/swagger-ui.html
3.创建微服务swagger-service-b (参考swagger-service-a ), 启动微服务swagger-service-b并访问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> <!-- 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("localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example
step6.启动微服务,访问localhost:10030/swagger-ui.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计897个文字,预计阅读时间需要4分钟。
Spring Cloud Zuul 集成 Swagger 1.x 准备服务注册中心 Eureka Server
1. 创建微服务 Swagger-Service-A (Spring Boot 项目) - 添加 Eureka Client 起步依赖 - 添加 Web 起步依赖 - 添加 Swagger 依赖
yaml dependencies: spring-cloud-starter-netflix-eureka-client spring-boot-starter-web springfox-swagger2
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> <!-- 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("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.启动微服务,访问 localhost:10010/swagger-ui.html
3.创建微服务swagger-service-b (参考swagger-service-a ), 启动微服务swagger-service-b并访问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> <!-- 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("localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example
step6.启动微服务,访问localhost:10030/swagger-ui.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

