spring cloud zuul 整合 swagger 聚合分布式系统文档
2018-03-21 00:00
369 查看
摘要: 如果想使用swagger生成文档,想把整个系统的文档整合在同一个页面上,可以参考本文。
项目结构
在本例中,eureka作为注册中心,gateway作为网关(把文档聚合在这上面)it-baseifno和it-jw作为要聚合的两个微服务。
添加swagger依赖
在gateway中,添加相应微服务的路由信息。
网关层swagger配置
swagger配置类
swagger文档资源配置类
it-baseinfo-service的swagger配置
it-jw微服务中的swagger配置
访问 (网关地址/swagger-ui.html) 本例为 http://localhost:8010/swagger-ui.html
这样就可以在下拉列表中随时切换,查看分布式系统的接口文档了。
项目结构
在本例中,eureka作为注册中心,gateway作为网关(把文档聚合在这上面)it-baseifno和it-jw作为要聚合的两个微服务。
添加swagger依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-common</artifactId> <version>2.6.1</version> </dependency>
在gateway中,添加相应微服务的路由信息。
网关层swagger配置
swagger配置类
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXXXX系统") .description("XXXX系统接口文档说明") .termsOfServiceUrl("http://localhost:80") .version("1.0") .build(); } @Bean UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } }
swagger文档资源配置类
DocumentationConfig
@Component @Primary public class DocumentationConfig implements SwaggerResourcesProvider { private final RouteLocator routeLocator; public DocumentationConfig(RouteLocator routeLocator) { this.routeLocator = routeLocator; } @Override public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); List<Route> routes = routeLocator.getRoutes(); //在这里遍历的时候,可以排除掉敏感微服务的路由 routes.forEach(route -> resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "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; } }
it-baseinfo-service的swagger配置
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.wfu.it")) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("基本信息API") .description("基本信息接口文档说明") .version("1.0") .build(); } @Bean UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(new String[]{"swagger-ui.html"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/"}); registry.addResourceHandler(new String[]{"/webjars*"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}); } }
it-jw微服务中的swagger配置
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.wfu.it")) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("工作量API") .description("工作量接口文档说明") .version("1.0") .build(); } @Bean UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(new String[]{"swagger-ui.html"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/"}); registry.addResourceHandler(new String[]{"/webjars*"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}); } }
访问 (网关地址/swagger-ui.html) 本例为 http://localhost:8010/swagger-ui.html
这样就可以在下拉列表中随时切换,查看分布式系统的接口文档了。
相关文章推荐
- Spring Cloud Zuul中使用Swagger汇总API接口文档
- spring cloud整合swagger,通过zuul路由
- Swagger自动接口文档生成框架————springboot整合swagger总结
- 构建自我修复型分布式系统之Spring Cloud
- 分布式系统设计原理与方案Dubbo+Zookeeper+Spring整合
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
- Spring整合Quartz定时任务 在集群、分布式系统中的应用
- Spring整合Quartz定时任务 在集群、分布式系统中的应用
- Swagger-UI与Spring Cloud整合与安全设置
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十一):服务网关(Zuul)
- 用zuul将微服务的多个swagger api文档聚合成一个文档
- Spring Cloud与分布式系统
- Spring Cloud学习系列第五章:分布式系统外部配置Spring Cloud Config
- Spring Cloud与分布式系统
- Spring Boot + Spring Cloud 构建微服务系统(八):分布式链路追踪(Sleuth、Zipkin)
- Spring Cloud与分布式系统
- 详解spring cloud config整合gitlab搭建分布式的配置中心
- Springboot | SpringBoot 微服务整合Swagger生成API文档
- Spring Cloud Eureka 控制台快速查看Swagger API文档