Swagger + Zuul 整合微服务接口文档 简介 Swagger
Swagger 是一款 RESTFUL 接口的文档在线自动生成+功能测试功能软件。Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。目标是使客户端和文件系统作为服务器以同样的速度来更新文件的方法,参数和模型紧密集成到服务器。
Zuul
Spring Cloud Zuul 是 Spring Cloud Netflix 子项目的核心组件之一,可以作为微服务架构中的 API 网关使用,支持动态路由与过滤功能 API 网关为微服务架构中的服务提供了统一的访问入口,客户端通过 API 网关访问相关服务。 API 网关的定义类似于设计模式中的门面模式,它相当于整个微服务架构中的门面,所有客户端的访问都通过它来进行路由及过滤。 它实现了请求路由、负载均衡、校验过滤、服务容错、服务聚合等功能。
技术
1 2 3 4 <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger2</artifactId > </dependency >
1 2 3 4 5 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-zuul</artifactId > <version > 2.0.1.RELEASE</version > </dependency >
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger2</artifactId > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-boot-starter</artifactId > <version > 3.4.0</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger-ui</artifactId > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.czq.api.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.ParameterBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.schema.ModelRef;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.Parameter;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;import java.util.List;@Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket createRestApi () { List<Parameter> parameters = new ArrayList (); parameters.add((new ParameterBuilder ()).name("Authorization" ).description("Authorization" ).modelRef(new ModelRef ("string" )).parameterType("header" ).required(false ).build()); return new Docket (DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .globalOperationParameters(parameters) .select() .apis(RequestHandlerSelectors.basePackage("com.czq" )) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo () { return new ApiInfoBuilder () .title("RecycleShop文档" ) .description("RecycleShop文档" ) .version("1.0" ) .build(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-zuul</artifactId > <version > 2.0.1.RELEASE</version > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > <version > 2.0.1.RELEASE</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger2</artifactId > <version > 2.7.0</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger-ui</artifactId > <version > 2.7.0</version > </dependency >
application.yml 文件部分配置(路由配置)
1 2 3 4 5 6 7 zuul: routes: manage-service: path: /manage-service/** serviceId: manage-service sensitiveHeaders:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.czq.gateway.config;import org.springframework.cloud.netflix.zuul.filters.Route;import org.springframework.cloud.netflix.zuul.filters.RouteLocator;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import springfox.documentation.swagger.web.SwaggerResource;import springfox.documentation.swagger.web.SwaggerResourcesProvider;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;import java.util.List;@Configuration @EnableSwagger2 @Primary public class SwaggerConfig implements SwaggerResourcesProvider { private final RouteLocator routeLocator; public SwaggerConfig (RouteLocator routeLocator) { this .routeLocator = routeLocator; } @Override public List<SwaggerResource> get () { List resources = new ArrayList (); List<Route> routes = routeLocator.getRoutes(); for (Route route: routes) { 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; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.czq.gateway;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication @EnableZuulProxy public class GatewayApplication { public static void main (String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
1 http://localhost:port/api/v1.0.1/swagger-ui.html
进入地址后选择对应微服务测试效果