代码之家  ›  专栏  ›  技术社区  ›  Ganesh Satpute

Swagger网关微服务聚合

  •  5
  • Ganesh Satpute  · 技术社区  · 7 年前

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

    现在,我有多个微服务,每个微服务都使用Swagger公开了它们的API。我们希望为公共客户端聚合所有这些API招摇过市文档。

    我们合并的临时解决方案是,只需为网关服务中的每个微服务复制带Swagger注释的类。什么是 正确的方法 这样做?

    1 回复  |  直到 7 年前
        1
  •  6
  •   Ganesh Satpute    7 年前

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

    我在我的 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>
    

    我的主课是这样的

     @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);
        }
     }
    

    我为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;
        }
    }
    

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

    我的 application.properties 如下所示

    ...
    server.port=8001
    
    zuul.routes.cust.path=/cust/**
    zuul.routes.cust.url=http://1.1.1.2:8002/cust-service/
    ...