Swagger 网关微服务聚合

Gan*_*ute 4 java swagger spring-boot microservices

我正在使用 SpringBoot 开发微服务应用程序。有面向公众的网关微服务,它将请求重定向到特定的微服务(在不同的主机上运行)。

现在,我有多个微服务,每个微服务都使用 Swagger 公开了它们的 API。我们希望为公共客户汇总所有这些 API Swagger 文档。

我们合并的临时解决方案是,只需为网关服务中的每个微服务复制 Swagger Annotated 类。什么是正确的方式做到这一点?

Gan*_*ute 6

我使用了 Zuul 并解决了我的问题这就是我的应用程序的部署方式 应用部署

我在我的 pom.xml

<dependencies>
        ....
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

我的主班看起来像这样

 @EnableZuulProxy
 @SpringBootApplication
 @EnableSwagger2
 public class Application {
     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
     }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration("validatorUrl", "list", "alpha", "schema",
            UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
    }
 }
Run Code Online (Sandbox Code Playgroud)

我为 swagger 文档创建了聚合器

@Component
@Primary
@EnableAutoConfiguration
public class SwaggerAggregatorController implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources= new ArrayList<>();
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName("cust-service");
        swaggerResource.setLocation("/cust/v2/api-docs");
        swaggerResource.setSwaggerVersion("2.0");

        resources.add(swaggerResource);
        return resources;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以在这个领域添加更多的微服务。(可以改进为从配置文件中读取)

我的application.properties样子如下

...
server.port=8001

zuul.routes.cust.path=/cust/**
zuul.routes.cust.url=http://1.1.1.2:8002/cust-service/
...
Run Code Online (Sandbox Code Playgroud)

  • 这可以使用 spring 云网关吗? (3认同)