Spring API 网关中的 Swagger Api 文档

问题描述 投票:0回答:2

我的项目中有上述架构。产品、订单、支付微服务是一个 Rest API,目前具有 swagger 集成,但现在流程发生了变化,我无法公开微服务 Rest API,现在所有 REST API 调用都是从 API 网关进行的。

是否有任何方法可以通过 API 网关以 swagger 方式记录 API,或者这种情况的最佳实践是什么。

这是API网关Spring boot中的路由配置

@Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/order/**")
                        .filters(f -> f.hystrix(option -> option.setName("order-service").
                                setFallbackUri("forward:/orderFallBack")))
                        .uri("lb://ORDER-SERVICE")
                        .id("order-service"))

                .route(r -> r.path("/payment/**")
                        .filters(f -> f.hystrix(option -> option.setName("payment-service")
                                .setFallbackUri("forward:/paymentFallBack")))
                        .uri("lb://PAYMENT-SERVICE")
                        .id("payment-service"))

                .route(r -> r.path("/product/**")
                        .filters(f -> f.hystrix(option -> option.setName("product-service")
                                .setFallbackUri("forward:/productFallBack")))
                        .uri("lb://PRODUCT-SERVICE")
                        .id("product-service"))
                .build();
    }

订单微服务项目中的Swagger配置

@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket orderApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(getApiInfo());
    }

    //create api metadata that goes at the top of the generated page
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("Fete Bird Order Microservice")
                .version("1.0")
                .description("API for managing Fete Bird Order Microservice.")
                .license("Fete Bird License Version 1.0")
                .build();
    }
}

java spring spring-boot swagger springfox
2个回答
0
投票

确保您的服务具有以下依赖项:产品、付款、订单和 api 网关:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
    </dependency>

在您的所有服务中添加

@EnableSwagger2
注释。

在您的API网关项目中添加zuul代理依赖项。这应该将流量从 api-gateway swagger 路由到您的其他服务。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

在 api-gateway 中添加

@EnableZuulProxy
注解。

然后,将此配置放入 api-gateway 中,一切就应该可以工作了。

@Primary
@Configuration
public class Swagger2Config implements SwaggerResourcesProvider {

@Autowired
private RouteLocator routeLocator;

@Override
public List<SwaggerResource> get() {
    List<SwaggerResource> resources = new ArrayList<>();

    routeLocator.getRoutes().forEach(route -> {
        resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"), "1.0"));
    });

    return resources;
}

private SwaggerResource swaggerResource(final String name, final String location, final String version) {
    SwaggerResource swaggerResource = new SwaggerResource();
    swaggerResource.setName(name);
    swaggerResource.setLocation(location);
    swaggerResource.setSwaggerVersion(version);
    return swaggerResource;
}

}

当您进入 api-gateway swagger 页面时,您将在右上角看到产品、付款和订单服务的选择选项。选择其中任意一个并尝试使用 API。


-1
投票

有一种常见的做法是让网关本身可以使用各个 swagger 端点。我在许多生产级项目中都看到过这样做。

例如,对于订单服务,文档位于:

http://gateway-url/order-service/swagger-ui.html

其他微服务也可以采用类似的方法。

© www.soinside.com 2019 - 2024. All rights reserved.