版本选择:
本次swagger接口聚合并非引入swagger依赖,而是使用了knife4j,这个依赖包是对swagger前段页面做了美化,使其更加好看,操作性更强
注意:本次文章中接口文档底层为swagger2
版本选择
组件 | 版本 |
---|
spring-boot | 2.3.12.RELEASE | spring-cloud | Hoxton.SR12 | knife4j-spring-boot-starter | 2.0.9 |
package com.vcolco.gateway.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandler {
@Autowired(required = false)
private SecurityConfiguration securityConfiguration;
@Autowired(required = false)
private UiConfiguration uiConfiguration;
private final SwaggerResourcesProvider swaggerResources;
@Autowired
public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
this.swaggerResources = swaggerResources;
}
@GetMapping("/configuration/security")
public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()),
HttpStatus.OK));
}
@GetMapping("/configuration/ui")
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping
public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
}
}
获取接口资源列表
package com.vcolco.gateway.config;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Slf4j
@Component
@Primary
@AllArgsConstructor
public class SwaggerResourceConfig implements SwaggerResourcesProvider {
private static final String SWAGGER2URL = "/v2/api-docs";
private final RouteLocator routeLocator;
@Value("${spring.application.name}")
private String gateway;
@Autowired
public SwaggerResourceConfig(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<String> routeHosts = new ArrayList<>();
Flux<Route> routes = routeLocator.getRoutes();
routes.filter(route -> route.getUri().getHost() != null)
.filter(route -> !gateway.equals(route.getUri().getHost()))
.subscribe(route -> routeHosts.add(route.getUri().getHost()));
Set<String> docServerSet = new HashSet<>();
routeHosts.forEach(instance -> {
String url = "/" + instance.toLowerCase() + SWAGGER2URL;
if (!docServerSet.contains(url)) {
docServerSet.add(url);
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setUrl(url);
swaggerResource.setName(instance);
resources.add(swaggerResource);
}
});
return resources;
}
}
网关访问接口文档
http://ip:port/doc.html
网关获取各微服务列表逻辑剖析
一、获取微服务资源列表
GET http://网关IP:网关端口/swagger-resources
上方获取接口资源列表中,底层使用routeLocator获取所有路由列表(我本身是设置根据服务名路由)
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
二、根据选中资源获取对应服务资源明细
跟据此接口,则可获取到对应服务的所有接口文档资源以及接口文档信息
|