Using zuul to aggregate multiple swagger api documents of microservices into one document

Posted by etully on Sun, 05 Jan 2020 04:47:00 +0100

First, look at the renderings:

Myc order: on behalf of order service

Myc user: on behalf of user service

Myc car: representative car service

... and so on.

Let me briefly talk about the integration steps and key points.

1. Add the following dependencies to the pom of each service.

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

Note: just add this. zuul is responsible for ui.

2. Put this in your config to start. Note that here I use a placeholder to get the name of the current document to avoid the possibility of adding other modules later.

@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+"Interface document")
                .description(applicationName+"Interface document")
                .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;
    }

}

Then zuul's configuration

3. Add the following configuration in zuul's config. Note that there is an apiNames that is all the grouping service names. Avoid write dead and read directly from the configuration file

@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's pom file adds the following dependencies:

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

5.zuul's agent configuration:

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
  sensitiveHeaders:

Here's how to access the startup project:

http://localhost:port/doc.html

Topics: Spring REST github