API网关不聚合微服务swagger docsSpring Boot

eivgtgni  于 2022-11-06  发布在  Spring
关注(0)|答案(1)|浏览(302)

Pom.xml:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.7</version>
</dependency>

SwaggerConfig.java:

@Configuration
@RequiredArgsConstructor

public class SwaggerConfig {

private final RouteDefinitionLocator locator;

@Bean
public List<GroupedOpenApi> apis() {
    List<GroupedOpenApi> groups = new ArrayList<>();
    List<RouteDefinition> definitions = locator.getRouteDefinitions().collectList().block();
    definitions.stream().filter(routeDefinition -> routeDefinition.getId().matches(".*-service")).forEach(routeDefinition -> {
        String name = routeDefinition.getId().replaceAll("-service", "");
        GroupedOpenApi api = GroupedOpenApi.builder().pathsToMatch("/" + name + "/**").group(name).build();
        groups.add(api);
    });
    return groups;
}

}

application.yml:

gateway:
  discovery:
    locator:
      enabled: true
      lower-case-service-id: true
  routes:
    - id: swagger
      uri: http://localhost:${server.port}
      predicates:
        - Path=/v3/api-docs/**
      filters:
        - RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/v3/api-docs
    - id: test-service
      uri: lb://test-service
      predicates:
        - Path=/testservice/**
      filters:
        - RewritePath=/testservice/(?<path>.*), /$\{path}

Api gateway在discovery-server的帮助下看到了test-service。当我输入我的gateway服务器的swagger-ui时,在“Select a definition”选项卡中,我只看到了“default”定义,而没有看到我真正想要的test-service定义。我已经使用了这些教程:

  1. https://piotrminkowski.com/2020/02/20/microservices-api-documentation-with-springdoc-openapi/
  2. https://dgempiuc.medium.com/api-gateway-swagger-composition-e9416398ca47
    Screenshot:
mm5n2pyu

mm5n2pyu1#

您可以在yaml文件springdoc中添加此配置:api-docs:已启用:真正的霸气-ui:配置URL:v3/api-docs/swagger-config网址:/v3/api-docs网址:- 名称:测试URL:/v3/api-docs/测试

相关问题