【问题标题】:How can I aggregate microservices swaggers into a single swagger我怎样才能将微服务招摇聚合成一个招摇
【发布时间】:2018-07-16 16:42:35
【问题描述】:

我正在尝试在我的微服务项目中生成单个 swagger,在 Api 网关中将所有服务 swagger 聚合为一个。为了实现这一点,我正在关注下一个教程https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger

这里的问题是,当我尝试设置绝对 URL 时,我收到的输出是 无法加载 API 定义。 undefined http://localhost:8070/apihttp://localhost:8081/api/v2/api-docs 其中 localhost:8070/api 是 api 网关的基本 URL,localhost:8081/api/v2/api-docs是微服务swagger的docs URL。

这是我的代码:

SwaggerConfiguration

package com.rfd.apigateway.swagger;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfiguration {

    private List<Resource> resources;

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }
}

资源

package com.rfd.apigateway.swagger;

public class Resource {
    private String name;
    private String url;
    private String version;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

文档控制器

package com.rfd.apigateway.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
@EnableAutoConfiguration
public class DocumentationController implements SwaggerResourcesProvider {

    private SwaggerConfiguration swaggerConfiguration;

    @Autowired
    public DocumentationController(SwaggerConfiguration swaggerConfiguration){
        this.swaggerConfiguration = swaggerConfiguration;
    }

    @Override
    public List get() {
        List resources = new ArrayList<>();
        for(Resource resource : this.swaggerConfiguration.getResources()){
            resources.add(createSwaggerResource(resource));
        }

        return resources;
    }

    private SwaggerResource createSwaggerResource(Resource resource) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(resource.getName());
        swaggerResource.setUrl(resource.getUrl());
        swaggerResource.setSwaggerVersion(resource.getVersion());
        return swaggerResource;
    }
}

最后,application.yml

swagger:
  resources:
    - name: transactions
      url: http://localhost:8081/api/v2/api-docs
      version: 1.0
    - name: payments
      url: http://localhost:8083/api/v2/api-docs
      version: 1.0

还有几张可以帮助理解问题的图片:

Api 网关 Swagger URL

微服务 api-docs 网址

【问题讨论】:

  • 您必须提供文档的网址。这是什么/api?
  • 我在 yml 文件中设置的 URL 是微服务招摇 URL。我有一个控制器,它在请求基本 URL 时将调用重定向到 /swagger-ui.html(在这种情况下,/api 是所有调用的前缀),所以基本上当您访问 localhost:808x/api 时,您正在访问微服务的大摇大摆。
  • 好的。让我们退后一点。在这里运行的服务localhost:8070/api。你如何访问这个服务的 swagger ui?
  • 感谢您的评论,我已经编辑了我的问题,我没有指向 api-docs 路径,但问题仍然存在。 localhost:8070/api 是带有“api”前缀的 api 网关,我有一个从 /api 重定向到 /api/swagger-ui.html 的控制器,因此访问 localhost:8070/api,我收到了招摇屏幕错误消息。
  • 准确地说,我正要说你没有将你的 url 指向 api-docs。很好,你想通了。你得到什么错误信息

标签: swagger api-gateway


【解决方案1】:

这里的问题只是 springfox 版本......我试图将它从 2.8.0 降级到 2.7.0,它就像一个魅力。这似乎是一个公认的错误,你可以在这里看到:https://github.com/springfox/springfox/issues/2235

我还必须在微服务中启用 cors,但这是另一个问题。

【讨论】:

【解决方案2】:

我可以通过以下方式解决它:https://github.com/varghgeorge/microservices-single-swagger

这个简单的 sprintboot 微服务可以在一个地方显示你所有的 swagger 文档。基于 YAML 配置,它将显示服务列表并为所有服务提供单个文档服务器。在 YAML 配置中,您可以添加不同的 swagger URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多