我试图在我的微服务项目中产生一个招摇,在Api网关中将所有服务swaggers聚合成一个。为了实现这一点,我正在关注下一个教程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;
}
}
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;
}
}
最后,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
以及一些有助于理解问题的图像:
这里的问题只是springfox版本......我试图将它从2.8.0降级到2.7.0,它就像一个魅力。它似乎是一个公认的错误,你可以在这里看到:https://github.com/springfox/springfox/issues/2235
我还必须在微服务中启用cors,但这是另一个问题。
试试这个:https://github.com/varghgeorge/microservices-single-swagger
来自不同Swagger的简单服务器聚合