全面整合多个微服务

mik*_*ike 6 java spring swagger spring-boot springfox

我有多个微服务,已经为此大刀阔斧了。我想将所有api置于单一的swagger UI下。我已经按照以下链接进行了此操作。但在STS中以Maven方法尝试过。 昂首阔步的Github示例

这是我在项目中的不同文件,

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableSwagger2
public class SgtestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SgtestApplication.class, args);
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() 
                  .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                  .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
                  .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.security")))
                     .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Single swagger")
                .description("API to retrieve swagger apis")
                .version("1.0.0")
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的资源提供者如下,

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

    @Autowired
    private SwaggerServicesConfig swaggerServiceList;

    public GatewaySwaggerResourceProvider() {
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();

        List<SwaggerServices> servList=swaggerServiceList.getServiceList();
        for (SwaggerServices swaggerServices : servList) {
            resources.add(swaggerResource(swaggerServices.getName(), swaggerServices.getUrl(),swaggerServices.getVersion()));
        }
        /*swaggerServiceList.getServiceList().forEach(service -> {
            resources.add(swaggerResource(service.getName(), service.getUrl(), service.getVersion()));
        });*/
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是我的服务配置,

@Component
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "documentation.swagger")
public class SwaggerServicesConfig {

    List<SwaggerServices> swagger;

    public List<SwaggerServices> getServiceList() {
        return swagger;
    }

    public void setServiceList(List<SwaggerServices> swaggerResources) {
        this.swagger = swaggerResources;
    }

    @EnableConfigurationProperties
    @ConfigurationProperties(prefix = "documentation.swagger.services")
    public static class SwaggerServices {
        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;
        }

        @Override
        public String toString() {
            return "SwaggerServices [name=" + name + ", url=" + url + ", version=" + version + "]";
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

在我的application.yml中,我指定了不同微服务的api docs端点。

spring:
  profiles: default

server:
  port: 8014


documentation:
  swagger:
    service-list:
    - name: local-swagger
      url: http://localhost:8085/v2/api-docs
      version: 1.0
Run Code Online (Sandbox Code Playgroud)

我的输出如下输出

有人可以通过我在这里到底在做什么错来帮助我吗?我能够得到swager ui,但其中没有显示任何api列表。我是新手,所以请查看我的程序。

Gna*_*yam 2

我认为存在 CORS 错误。请为所有 Swagger 文档服务指定 CORS 原始标头。

更好的方法: 而不是显式指定微服务 URL。将所有服务注册到服务寄存器并从中获取所有实例详细信息。这是集中式 swagger 的更好方法。

供参考: 集中式 Swagger 文档