简体   繁体   English

Swagger Api Spring API 网关中的文档

[英]Swagger Api Documentation in Spring API Gateway

w

I have the above architecture in the project.我在项目中有上述架构。 Product, Order, Payment Microservice is a Rest API which currently has swagger integration, but now the flow is changed I can't expose the Microservice Rest API now all the REST API calls is been made from API Gateway. Product, Order, Payment Microservice is a Rest API which currently has swagger integration, but now the flow is changed I can't expose the Microservice Rest API now all the REST API calls is been made from API Gateway.

Is there any way to document the API through API gateway in swagger or what is the best practice for this case.有没有办法通过 swagger 中的 API 网关记录 API 或这种情况的最佳实践。

This is the routing configuration in API Gateway Spring boot这是API网关Spring启动中的路由配置

@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 in Order Microservice Project订单微服务项目中的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();
    }
}

在此处输入图像描述

There is one common practice to make individual swagger endpoints available from gateway itself.有一种常见的做法是让网关本身可以使用各个 swagger 端点。 I have seen this being done in many production level projects.我已经在许多生产级项目中看到了这一点。

For example, for Order service the documentation would be at:例如,对于订单服务,文档将位于:

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

Similar approach could be followed for other micro-services.其他微服务也可以采用类似的方法。

Make sure to have the below dependencies in your services: product, payment, order and api-gateway:确保在您的服务中具有以下依赖项:产品、付款、订单和 api-gateway:

    <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>

Add @EnableSwagger2 annotation in all of your services.在所有服务中添加@EnableSwagger2注释。

Add zuul proxy dependencies in your API gateway project.在您的 API 网关项目中添加 zuul 代理依赖项。 This should route the traffic from api-gateway swagger to your other services.这应该将来自 api-gateway swagger 的流量路由到您的其他服务。

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

Add @EnableZuulProxy annotation in api-gateway.在 api-gateway 中添加@EnableZuulProxy注解。

Then, put this config in api-gateway and things should work.然后,将此配置放入 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;
}

} }

When you will land in your api-gateway swagger page, in the top right you will see a select option for product, payment and order services.当您登陆您的 api-gateway swagger 页面时,您将在右上角看到一个 select 选项,用于产品、支付和订单服务。 Select any of them and try to use the APIs. Select 并尝试使用 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM