There is no technology involved in this article, just a simple tip. To read this article, you need to understand the configuration and use of Spring-Cloud-Zuul, Spring-Cloud-Eureka, and Swagger.

If your system also uses Zuul as a gateway to a distributed system, and uses Swagger to generate documentation, and wants to consolidate the entire system’s documentation on one page, refer to this article.

The project structure

Eureka-server: Eureka service registry, port 8080, Zuul-server: Zuul gateway, port 8081 Payment-server: payment system, port 8082 order-server: Order system, port 8083 order-server1: order system, port 8084 order-server2: order system, port 8085 where order-server, order-server1, order-server2 constitute the order system cluster.

http://localhost:8081/swagger-ui.html

Implementation method

zuul-server

The routing configuration

zuul:
  routes:
    payment-server:
      path: /pay/**
    order-server:
      path: /order/**
Copy the code

Swagger Configuration class SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Distributed Shopping System")
                .description("Shopping system interface Documentation")
                .termsOfServiceUrl("http://localhost:8081")
                .contact(new Contact("vker".""."[email protected]"))
                .version("1.0")
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list"."alpha"."schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false.true, 60000L); }}Copy the code

Important: Swagger document resource configuration class DocumentationConfig

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        resources.add(swaggerResource("Order system"."/order/v2/api-docs"."2.0"));
        resources.add(swaggerResource("Payment system"."/pay/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);
        returnswaggerResource; }}Copy the code

Can see implementation of key in DocumentationConfig, through configuration document resources, when on the front page drop-down box to choose the order system, will ask http://localhost:8081/order/v2/api-docs for documentation for details, and according to the routing configuration of zuul, Zuul will route /order/** requests to the system with serviceId as order-server. And because the order-server is a cluster, even if one of the services fails, the document retrieval will not be affected.

order-server

For swagger configuration class SwaggerConfig, the order-server and payment-Serverswagger configurations are basically the same.

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("w.m.vker.demo"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Order System API")
                .description("Order System Interface Documentation")
                .contact(new Contact("vker".""."[email protected]"))
                .version("1.0")
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list"."alpha"."schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false.true, 60000L); }}Copy the code

Swagger integration xrebel

Xrebel is a Web debugging tool. See the Tutorials to use XRebel. Xrebel works by tracking requests to a page and analyzing the flow and time spent on the entire request, while Swagger provides online interface debugging for the page. Combining the two features allows you to quickly debug the interface while simultaneously analyzing the flow and defects of the interface, which is a great addition. As shown in figure:

Implementation method

To integrate the xrebel into zuul – vm server startup options in the parameters, after zuul among them successful, open the http://localhost:8081/xrebel pages, want to js code within the page just below the text box

<script>
  window.XREBEL_SERVERS = ['http://localhost:8081'];
  (function() {
    const script = document.createElement('script');
    script.src = window.XREBEL_SERVERS[0] + '/a65f4bf22bdd793dca6963ffe7fa0c62/resources/init.min.js'; document.body.appendChild(script); } ()); </script>Copy the code

Let me copy that. The jar is located in the maven repository folder \ IO \springfox\ springfox-Swagger-ui \ version number. Unzip the jar package, find swagger-ui. HTML, paste the js file you copied into it, and run zuul-server. Can see the lower left corner in swagger http://localhost:8081/swagger-ui.html home page in this lovely toolbar.

Finally, attach the code address: github.com/91wangmeng/…