【问题标题】:Swagger Gateway MicroService AggregationSwagger 网关微服务聚合
【发布时间】:2018-08-23 20:58:27
【问题描述】:

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

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

我们合并的临时解决方案是,只是为 Gateway Service 中的每个微服务复制了 Swagger Annotated 类。 正确的方法是什么?

【问题讨论】:

  • 你看过 Netflix Zuul 吗?
  • 我没有,让我试一试,然后回来:)。感谢您的意见@StefaanNeyts
  • 谢谢,@StefaanNeyts。我试过了,它就像一个魅力。我已将代码发布为答案,以便对其他用户有所帮助。
  • 做得很好。感谢分享您的解决方案。起来!
  • @StefaanNeyts 所有请求都应具有Authorizatoin: bearer: <token> 格式的有效授权标头。此令牌将在网关上得到验证,如果正确的请求将被传递到没有身份验证标头的特定服务。如何通过 Swagger 将此令牌添加到每个请求中?

标签: java spring-boot swagger microservices


【解决方案1】:

我使用了 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>

我的主课是这样的

 @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/
...

【讨论】:

  • 这可以使用spring cloud gateway吗?
  • return new UiConfiguration method 在 Springfox 的 2.9.2 中被标记为已弃用
  • @Mr_LinDowsMac 你可以使用任何最新的,我认为它会以同样的方式工作。我试图解决的问题是汇总所有招摇的文档
猜你喜欢
  • 2022-07-21
  • 1970-01-01
  • 2020-01-30
  • 2020-10-04
  • 1970-01-01
  • 2022-08-19
  • 2019-04-06
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多