gpt4 book ai didi

java - Spring API Gateway 中的 Swagger Api 文档

转载 作者:行者123 更新时间:2023-12-04 14:26:42 25 4
gpt4 key购买 nike

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();
}
订单微服务项目中的 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();
}
}
enter image description here

最佳答案

有一种常见的做法是使网关本身可以使用单个 swagger 端点。我已经在许多生产级项目中看到了这一点。
例如,对于订单服务,文档位于:
http://gateway-url/order-service/swagger-ui.html
其他微服务也可以采用类似的方法。

关于java - Spring API Gateway 中的 Swagger Api 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62845550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com