微服务通过网关聚合所有服务接口文档
示例使用的JDK为1.8Spring Boot 版本 2.6.13微服务配置(Gateway除外)添加依赖dependency groupIdcom.github.xiaoymin/groupId artifactIdknife4j-micro-spring-boot-starter/artifactId version3.0.3/version /dependency配置文件 (yml)spring: # knife4j开启接口显示 mvc: pathmatch: matching-strategy: ant_path_matcher main: allow-circular-references: true swagger: enable: true添加config文件(Knife4jConfiguration)Configuration public class Knife4jConfiguration { Value(${swagger.enable:true}) private boolean enableSwagger; Bean(value defaultApi2) public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title(provider服务) .version(1.0) .build()) .enable(enableSwagger) .select() .apis(RequestHandlerSelectors.basePackage(cn.msj.pro.controller)) .paths(PathSelectors.any()) .build(); } Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping( WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) { ListExposableEndpoint? allEndpoints new ArrayList(); CollectionExposableWebEndpoint webEndpoints webEndpointsSupplier.getEndpoints(); allEndpoints.addAll(webEndpoints); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); String basePath webEndpointProperties.getBasePath(); EndpointMapping endpointMapping new EndpointMapping(basePath); boolean shouldRegisterLinksMapping webEndpointProperties.getDiscovery().isEnabled() (org.springframework.util.StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null); } }Gateway相关配置添加依赖dependency groupIdcom.github.xiaoymin/groupId artifactIdknife4j-spring-boot-starter/artifactId version3.0.3/version /dependency配置文件(yml)### consul服务端口号 server: port: 8601 spring: redis: host: localhost port: 6379 application: name: service-gateway-api #### consul注册中心地址 cloud: consul: host: localhost port: 8500 discovery: #hostname: 127.0.0.1 service-name: ${spring.application.name} gateway: routes: # 路由配置 - id: service-feign-demo uri: lb://service-feign-demo filters: - StripPrefix1 - name: RequestRateLimiter args: # 每秒允许处理的请求数量 redis-rate-limiter.replenishRate: 1 # 每秒最大处理的请求数量 redis-rate-limiter.burstCapacity: 2 # 限流策略对应策略的Bean key-resolver: #{ipKeyResolver} predicates: - Path/abc/** - id: service-ribbon-client uri: lb://service-ribbon-client predicates: - Path/client/** filters: - StripPrefix1 # 动态路由会在consul里自动拉取所有服务生成路由表 discovery: locator: #开启从注册中心动态创建路由的功能 enabled: true #使用小写服务名默认是大写 lower-case-service-id: true # 必须添加mvc mvc: pathmatch: # 将路径匹配策略改为 ant_matcher与 knife4j 兼容 matching-strategy: ant_path_matcher logging: level: org.springframework.cloud.gateway: debug主要属性说明StripPrefix 为删除第一个内容比如说localhost:8080/firng/doc.html localhost:8080/doc.htmlpredicates: 次属性必须存在并且配置属性(可选path)。如果你predicates里配置了path的话并且有两个意思的路由策略的话他们的值不能是/** 因为这样的话会找不到格式必须为/自定义名称/**因为是聚合文档要访问doc.html 所有必须截取一位。直接访问localhost:8080/doc.html 就可以打开文档在左上角这个名称会自动获取到urilb:// 这后面的值切换也是根据这个名称来进行匹配的