使用Spring Cloud Zuul整合Swagger实现微服务API文档聚合
在微服务架构中,每个服务都独立部署,其Swagger生成的API文档分散在各自节点上,难以统一查阅。通过Spring Cloud Zuul网关可以实现文档的集中管理。
前提条件
确保已搭建Eureka服务注册中心,具体可参考Eureka服务治理相关教程。
步骤一:创建swagger-service-a服务
新建Spring Boot项目,引入以下依赖:
<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>
<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>
在启动类上添加@EnableDiscoveryClient注解,注册到Eureka。
配置Swagger:
@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("服务A接口文档")
.version("1.0")
.build();
}
}
配置文件application.properties:
spring.application.name=swagger-service-a
server.port=10010
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
swagger.base-package=com.example
添加测试控制器:
@RestController
public class AaaController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/service-a")
public String getServiceInfo() {
return "service-a Services: " + discoveryClient.getServices();
}
}
启动服务,访问http://localhost:10010/swagger-ui.html查看文档。
步骤二:创建swagger-service-b服务
依照上述步骤创建服务B,端口设为10020,启动后访问其Swagger页面确认正常。
步骤三:构建API网关并集成Swagger
新建网关服务swagger-api-gateway,引入依赖:
<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>
<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>
启动类添加@SpringCloudApplication和@EnableZuulProxy。
配置Swagger,与之前类似但需定义文档资源提供器:
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
resources.add(createResource("service-a", "/swagger-service-a/v2/api-docs", "2.0"));
resources.add(createResource("service-b", "/swagger-service-b/v2/api-docs", "2.0"));
return resources;
}
private SwaggerResource createResource(String name, String location, String version) {
SwaggerResource resource = new SwaggerResource();
resource.setName(name);
resource.setLocation(location);
resource.setSwaggerVersion(version);
return resource;
}
}
网关配置文件:
spring.application.name=swagger-api-gateway
server.port=10030
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
swagger.base-package=com.example
启动网关,访问http://localhost:10030/swagger-ui.html即可看到聚合后的API文档,通过下拉框切换不同服务的接口。