我怎样才能将微服务招摇聚合成一个招摇 [英] How can I aggregate microservices swaggers into a single swagger

查看:23
本文介绍了我怎样才能将微服务招摇聚合成一个招摇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的微服务项目中生成一个 swagger,在 Api Gateway 中将所有服务 swagger 聚合为一个.为了实现这一点,我正在关注下一个教程

微服务 api-docs 网址

解决方案

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

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

I am trying to generate a single swagger in my microservices project, aggregating all services swaggers into a single one, in the Api Gateway. In order to achieve this, I am following the next tutorial https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger

The problem here is that, when I try to set absolute URLs, the output I am receiving is Failed to load API definition. undefined http://localhost:8070/apihttp://localhost:8081/api/v2/api-docs where localhost:8070/api is the base URL for the api gateway, and localhost:8081/api/v2/api-docs is the docs URL of the swagger of the microservice.

Here is my code:

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

Resource

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

DocumentationController

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

Finally, the 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

And a couple of images that can help to understand the problem:

Api Gateway Swagger URL

Microservice api-docs URL

解决方案

The problem here was just the springfox version... I tried to downgrade it from 2.8.0 to 2.7.0 and it worked like a charm. It seems it is a recognized bug, as you can see here: https://github.com/springfox/springfox/issues/2235

I also had to enable cors in microservices but that is another problem.

这篇关于我怎样才能将微服务招摇聚合成一个招摇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆