Spring API Gateway 中的 Swagger Api 文档

San*_*isy 9 java spring swagger spring-boot springfox

w

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

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

这是API Gateway 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();
    }
Run Code Online (Sandbox Code Playgroud)

订单微服务项目中的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 -1

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

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

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

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