gpt4 book ai didi

swagger - 如何将微服务的 Swagger 聚合成一个 Swagger

转载 作者:行者123 更新时间:2023-12-04 15:16:43 26 4
gpt4 key购买 nike

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

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

这是我的代码:

Swagger配置

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

文档 Controller
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 gateway swagger URL

微服务 api-docs URL
Microservice api-docs URL

最佳答案

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

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

关于swagger - 如何将微服务的 Swagger 聚合成一个 Swagger ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com