☰
微服务通过网关聚合所有服务接口文档
2026/10/15 17:05:49 网站建设 项目流程

示例使用的JDK为:1.8,Spring Boot 版本: 2.6.13

微服务配置(Gateway除外)

添加依赖

<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <version>3.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) { List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>(); Collection<ExposableWebEndpoint> 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> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.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: - StripPrefix=1 - 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: - StripPrefix=1 # 动态路由,会在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.html

predicates: 次属性必须存在并且配置属性(可选path)。如果你predicates里配置了path的话并且有两个意思的路由策略的话,他们的值不能是/** 因为这样的话会找不到格式必须为:/自定义名称/**,因为是聚合文档要访问doc.html 所有必须截取一位。直接访问localhost:8080/doc.html 就可以打开文档

在左上角这个名称会自动获取到uri:lb:// 这后面的值,切换也是根据这个名称来进行匹配的!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询