Each service has its own interface and manages the interface documentation through Swagger. When there are many services we want to have a unified portal to view the documents, so we can display the documents aggregated in Zuul.

Let’s take a look at the specific integration steps and pit records. Cloud version: finchley. SR2; Boot version: 2.0.6

Add Swagger’s dependency:

<! -- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> < version > 2.9.2 < / version > < / dependency > < the dependency > < groupId >. IO springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 2.9.2 < / version > < / dependency >Copy the code

Add aggregate code:

@EnableSwagger2
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {

    @Autowired
    private DiscoveryClient discoveryClient;
    
    @Value("${spring.application.name}")
    private String applicationName;
    
    @Override
    public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); / / out of themselves, will add other service discoveryClient. GetServices () stream (). The filter (s - >! s.equals(applicationName)).forEach(name -> { resources.add(swaggerResource(name,"/" + name + "/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

I will directly use DiscoveryClient to obtain the service list for aggregation. Of course, you can also write your service list or connect to the configuration center.

In addition to DiscoveryClient, we can also obtain the list of services based on the configuration of zuul routes, using a RouteLocator. There are many ways to do it.

Zull’s Swagger file cannot be displayed because the path is wrong and the configuration of context-path is missing. The effect is shown below:

Context path = context-path = context-path = context-path = context-path = context-path

resources.add(swaggerResource(name, "/" + name + "/v2/api-docs"."2.0"));
Copy the code

The easiest way to do this is to add a configuration that configures the context-path for each service so that you can concatenate it directly from here. But that’s kind of low end, haha.

DiscoveryClient is very powerful, and we can use DiscoveryClient to get information from Eureka, so I had an idea that the business service would put its context-path into Eureka’s metadata-map. Then Zuul gets the context-path from the metadata-map for aggregation.

Business Service plus configuration:

server.servlet.context-path=/yinjihuan
eureka.instance.metadata-map.context-path=${server.servlet.context-path}
Copy the code

Modified in Zull:

@Override
public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); / / out of themselves, will add other service discoveryClient. GetServices () stream (). The filter (s - >! s.equals(applicationName)).forEach(name -> { Optional<ServiceInstance> instanceOptional = discoveryClient.getInstances(name).stream().findFirst();if (instanceOptional.isPresent() && instanceOptional.get().getMetadata().containsKey("context-path")) {
    		String contexPath = instanceOptional.get().getMetadata().get("context-path");
    		resources.add(swaggerResource(name, "/" + name + contexPath + "/v2/api-docs"."2.0"));
    	} else {
    		resources.add(swaggerResource(name, "/" + name + "/v2/api-docs"."2.0")); }});return resources;
}
Copy the code

This perfectly solves the problem caused by adding context-path. Join us on planet Earth.