用zuul将微服务的多个swagger api文档聚合成一个文档

1.在每一个服务的pom中添加如下依赖java

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
</dependency>

注意:仅仅须要添加这个就行。zuul负责uigit

2.新建zuul工程,将这个放到你的config启动,注意这里我用了一个占位符获取当前文档的名称,避免写死后续可能添加其余模块github

@ConditionalOnClass(value = {Swagger.class})
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${spring.application.name}")
    private String applicationName;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.miaoyouche"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName+"接口文档")
                .description(applicationName+"接口文档")
                .contact(new Contact("miaoyouche", "http://www.miaoyouche.com", "mail.xxx@miaoyouche.com"))
                .version("1.0")
                .build();
    }

    private List<Parameter> parameters() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization")
                .description("Authorization")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        parameters.add(parameterBuilder.build());
        return parameters;
    }

}

接着是zuul的配置spring

3.在zuul的config中添加以下配置,注意这里有个apiNames是全部的分组服务名,避免写死,直接从配置文件读取bootstrap

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Value("${rest.api.names}")
    private String[] apiNames;

    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        if (apiNames != null) {
            Arrays.stream(apiNames).forEach(s ->
                    resources.add(swaggerResource(s, "/openapi/" + s + "/v2/api-docs", "2.0"))
            );
        }
        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;
    }
}

4.zuul的pom文件中添加如下依赖:api

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>swagger-bootstrap-ui</artifactId>
</dependency>

5.zuul的代理配置:app

zuul:
  routes:
    myc-user:
      path: /openapi/myc-user/**
      serviceId: myc-user
    myc-car:
      path: /openapi/myc-car/**
      serviceId: myc-car
    myc-auth:
      path: /openapi/myc-auth/**
      serviceId: myc-auth
    myc-order:
      path: /openapi/myc-order/**
      serviceId: myc-order
  stripPrefix: true

 

报错解决:ide

 

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;

这个问题解决办法是由于swagger依赖google的guava,而你当前项目的guava版本与之不匹配,而我由于使用当前最新的swagger2版本,我就将guava升到最新的版本ui

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>25.1-jre</version>
</dependency>