看到这文章的话,证明各子服务的swagger已经是使用起来了。如果还没有使用起来,可自行百度一下swagger的使用方法
下面我直接上干货了
1.保持其他子服务配置不变,在zuul路由中添加两个配置类
其中SwaggerConfig.java可以直接copy之前的子服务的配置类,可以无需做任何修改。(swagger依赖就不用说了,必须也是加入的)
2.配置DocumentationConfig.java类
先贴一下源码
package com.paixi.zuul.api.config;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Galen
* @Date: 2019/5/20-10:58
* @Description: 路由代理其他子服务的接口文档
**/
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("购物车", "/service-feign-cart/v2/api-docs", "1.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;
}
}
实现SwaggerResourcesProvider类的get方法,在这个方法配置要代理的子服务。
其中,“购物车”是对子服务接口文档的一个别名;service-feign-cart是 子服务的 serviceId;/v2/api-docs是固定的
重启zuul,访问zuul的swagger-ui.html(zuul的ip:port/swagger-ui.html),就可以从下拉列表中选择路由代理的子服务的swagger。