|
|
3 months ago | |
|---|---|---|
| .. | ||
| src | 3 months ago | |
| README.md | 3 months ago | |
| pom.xml | 3 months ago | |
基于 Knife4j 4.x + OpenAPI 3 规范的接口文档增强解决方案。
在需要暴露 API 文档的微服务的 pom.xml 中添加依赖:
<dependency>
<groupId>cn.hfln.framework</groupId>
<artifactId>knife4j-doc-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
在 application.yml 中添加配置:
lnxx:
knife4j:
doc:
enable: true
base-package: com.your.package # 接口所在包路径
title: 服务名称
description: 服务描述
version: ${project.version}
enable-security: true # 是否启用安全认证
在网关项目的 pom.xml 中添加依赖:
<dependency>
<groupId>cn.hfln.framework</groupId>
<artifactId>knife4j-doc-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
在网关的 application.yml 中添加如下配置:
lnxx:
knife4j:
doc:
# 基础配置
enable: false # 禁用普通模式
title: "微服务 API 文档聚合"
description: "统一管理所有微服务的 API 文档"
version: "1.0.0"
# 网关配置
gateway:
enable: true # 启用网关模式
services: # 配置需要聚合的服务列表
# 用户服务
- name: "用户服务"
url: "/v3/api-docs" # 注意:这里不需要服务名称前缀
description: "用户注册、登录、权限管理等相关接口"
# 订单服务
- name: "订单服务"
url: "/v3/api-docs" # 注意:这里不需要服务名称前缀
description: "订单创建、查询、状态管理等相关接口"
# 商品服务
- name: "商品服务"
url: "/v3/api-docs" # 注意:这里不需要服务名称前缀
description: "商品信息、库存、分类管理等相关接口"
# 支付服务
- name: "支付服务"
url: "/v3/api-docs" # 注意:这里不需要服务名称前缀
description: "支付处理、退款、对账等相关接口"
# 通知服务
- name: "通知服务"
url: "/v3/api-docs" # 注意:这里不需要服务名称前缀
description: "短信、邮件、推送通知等相关接口"
确保网关能正确路由到各个微服务,关键是要配置 StripPrefix 过滤器:
spring:
cloud:
gateway:
routes:
# 用户服务路由
- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**
filters:
- StripPrefix=1 # 移除路径前缀,将 /user-service/v3/api-docs 转换为 /v3/api-docs
# 订单服务路由
- id: order-service
uri: lb://order-service
predicates:
- Path=/order-service/**
filters:
- StripPrefix=1 # 移除路径前缀
# 商品服务路由
- id: product-service
uri: lb://product-service
predicates:
- Path=/product-service/**
filters:
- StripPrefix=1 # 移除路径前缀
# 支付服务路由
- id: payment-service
uri: lb://payment-service
predicates:
- Path=/payment-service/**
filters:
- StripPrefix=1 # 移除路径前缀
# 通知服务路由
- id: notification-service
uri: lb://notification-service
predicates:
- Path=/notification-service/**
filters:
- StripPrefix=1 # 移除路径前缀
http://服务地址:端口/doc.htmlhttp://网关地址:端口/swagger-ui/index.htmlhttp://网关地址:端口/v3/api-docs/swagger-config| 配置项 | 说明 | 默认值 |
|---|---|---|
| lnxx.knife4j.doc.enable | 是否启用文档 | true |
| lnxx.knife4j.doc.base-package | 接口扫描包路径 | - |
| lnxx.knife4j.doc.title | 文档标题 | API 文档 |
| lnxx.knife4j.doc.description | 文档描述 | - |
| lnxx.knife4j.doc.version | 文档版本 | 1.0.0 |
| lnxx.knife4j.doc.enable-security | 是否启用安全认证 | false |
| lnxx.knife4j.doc.name | 联系方式姓名 | - |
| lnxx.knife4j.doc.url | 联系方式网站 | - |
| lnxx.knife4j.doc.email | 联系方式邮箱 | - |
| 配置项 | 说明 | 默认值 |
|---|---|---|
| lnxx.knife4j.doc.gateway.enable | 是否启用网关模式 | false |
| lnxx.knife4j.doc.gateway.services | 需要聚合的服务列表 | - |
| 配置项 | 说明 | 必填 |
|---|---|---|
| name | 服务名称 | 是 |
| url | 服务的 API 文档路径(不需要服务名称前缀) | 是 |
| description | 服务描述 | 否 |
http://gateway:8080/user-service/v3/api-docsuser-service 路由/user-service 前缀http://user-service:8081/v3/api-docs# 网关路由配置
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**
filters:
- StripPrefix=1
# Swagger 聚合配置
lnxx:
knife4j:
doc:
gateway:
services:
- name: "用户服务"
url: "/user-service/v3/api-docs" # ❌ 错误:包含服务名称
# 网关路由配置
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**
filters:
- StripPrefix=1
# Swagger 聚合配置
lnxx:
knife4j:
doc:
gateway:
services:
- name: "用户服务"
url: "/v3/api-docs" # ✅ 正确:不包含服务名称
如果需要动态配置服务列表,可以:
@Autowired
private DiscoveryClient discoveryClient;
private List<ServiceUrl> buildServiceUrls() {
List<ServiceUrl> serviceUrls = new ArrayList<>();
discoveryClient.getServices().forEach(serviceId -> {
serviceUrls.add(new ServiceUrl(
serviceId + "服务",
"/v3/api-docs", // 注意:这里不需要服务名称前缀
serviceId + "相关接口"
));
});
return serviceUrls;
}
@Autowired
private ServiceConfigRepository repository;
private List<ServiceUrl> buildServiceUrls() {
return repository.findAll().stream()
.map(config -> new ServiceUrl(
config.getName(),
"/v3/api-docs", // 注意:这里不需要服务名称前缀
config.getDescription()
))
.collect(Collectors.toList());
}
完整的配置示例可以参考 application-gateway.yml 文件。