4. OpenAPI 3.0 规范基础:核心概念、格式选择与三大模块
OpenAPI 3.0 规范(原 Swagger 规范)是业界标准的 RESTful API 描述语言。在 Kafka 堆积诊断场景中,它用于精确描述消费组管理、堆积查询、重置偏移等治理接口,确保客户端与诊断平台之间的契约清晰、可验证。本章聚焦规范的核心概念、YAML/JSON 格式选型以及 info、servers、paths、components 四大模块的结构化组织。
4.1 核心概念:从资源到契约
OpenAPI 3.0 将 API 视为一组可操作的资源,每个资源通过 HTTP 方法暴露。其核心概念包括:
| 概念 | 说明 | Kafka 诊断示例 |
|---|---|---|
| 路径(Path) | 资源的 URL 模板,支持路径参数 | /kafka/clusters/{clusterId}/consumer-groups |
| 操作(Operation) | HTTP 方法(GET/POST/PUT/DELETE) | GET 查询堆积列表 |
| 参数(Parameter) | 路径、查询、请求头、Cookie 参数 | ?lagThreshold=1000 |
| 请求体(RequestBody) | POST/PUT 操作的输入结构 | 重置偏移请求 JSON |
| 响应(Response) | 状态码 + 响应体 Schema | 200 返回堆积详情数组 |
| Schema | 数据模型定义(基于 JSON Schema) | ConsumerGroupLag 对象 |
{} 标记,如 /consumer-groups/{groupId};查询参数用于过滤、分页;请求体与响应体通过 $ref 引用 components/schemas 中的模型,实现复用。
4.2 YAML 与 JSON 格式选择
OpenAPI 规范支持 YAML 和 JSON 两种序列化格式。两者在语义上完全等价,但适用场景不同:
| 维度 | YAML | JSON |
|---|---|---|
| 可读性 | 高,缩进结构清晰,注释友好 | 中等,括号密集,无注释 |
| 编写效率 | 高,适合人工手写 | 低,适合机器生成 |
| 工具生态 | 所有 OpenAPI 工具均支持 | 所有 OpenAPI 工具均支持 |
| 文件大小 | 较小(无冗余括号) | 较大(键名加引号) |
| 推荐场景 | 设计阶段、代码仓库维护 | 运行时交换、CI/CD 流水线 |
swagger-cli)转换为 JSON 格式供网关和测试框架使用。
4.3 三大核心模块详解
OpenAPI 3.0 规范文档由顶层字段构成,其中 info、servers、paths、components 是最核心的四个模块。下面逐一拆解。
4.3.1 info:API 元信息
info 模块描述 API 的基本信息,包括标题、版本、描述、联系人等。它是 API 文档的“身份证”。
info:
title: Kafka 堆积诊断 API
description: |
用于查询和管理 Kafka 消费组堆积状态的 RESTful 接口。
支持堆积阈值告警、偏移重置、消费组拓扑分析。
version: "2.1.0"
contact:
name: 消息中间件团队
email: kafka-ops@company.com
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0
version 字段应遵循语义化版本(SemVer),与 API 的实际发布版本保持一致。不要与实现版本混淆。
4.3.2 servers:服务端点
servers 模块定义 API 的基础 URL,支持多环境配置。每个 server 包含 url 和可选的 description、variables。
servers:
- url: https://api.company.com/kafka/v2
description: 生产环境
- url: https://staging-api.company.com/kafka/v2
description: 预发环境
- url: http://localhost:8080/kafka/v2
description: 本地开发环境
使用 variables 实现动态端点:
servers:
- url: https://{env}.company.com/kafka/{apiVersion}
variables:
env:
default: api
enum:
- api
- staging-api
- dev-api
apiVersion:
default: v2
4.3.3 paths:API 路径与操作
paths 是规范的核心,定义所有可访问的端点及其操作。每个路径下可包含多个 HTTP 方法,每个方法内定义参数、请求体、响应。
paths:
/consumer-groups:
get:
summary: 查询所有消费组列表
parameters:
- name: clusterId
in: query
required: true
schema:
type: string
- name: status
in: query
schema:
type: string
enum: [active, stalled, unknown]
responses:
'200':
description: 消费组列表
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ConsumerGroup'
/consumer-groups/{groupId}/lag:
get:
summary: 查询指定消费组的堆积详情
parameters:
- name: groupId
in: path
required: true
schema:
type: string
responses:
'200':
description: 堆积详情
content:
application/json:
schema:
$ref: '#/components/schemas/GroupLagDetail'
4.3.4 components:可复用组件
components 模块集中管理可复用的 Schema、参数、响应、请求体等。通过 $ref 引用,避免重复定义,提升规范的可维护性。
components:
schemas:
ConsumerGroup:
type: object
properties:
groupId:
type: string
description: 消费组 ID
state:
type: string
enum: [Stable, Empty, Dead]
members:
type: integer
description: 消费者成员数
totalLag:
type: integer
description: 总堆积数
GroupLagDetail:
type: object
properties:
groupId:
type: string
topicPartitions:
type: array
items:
$ref: '#/components/schemas/PartitionLag'
PartitionLag:
type: object
properties:
topic:
type: string
partition:
type: integer
currentOffset:
type: integer
endOffset:
type: integer
lag:
type: integer
parameters:
clusterIdParam:
name: clusterId
in: query
required: true
schema:
type: string
responses:
NotFound:
description: 资源不存在
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
clusterId、groupId 参数定义为 components/parameters;将 PartitionLag、ErrorResponse 等模型放入 components/schemas。这样当 API 版本升级时,只需修改组件定义,所有引用自动生效。
4.4 三大模块关系图
下图展示了 OpenAPI 3.0 规范文档中 info、servers、paths、components 四大模块的层级关系与数据流向:
4.5 完整规范示例(Kafka 堆积诊断)
以下是一个精简但完整的 OpenAPI 3.0 YAML 示例,整合了上述三大模块,用于 Kafka 消费组堆积查询接口:
openapi: "3.0.3"
info:
title: Kafka 堆积诊断 API
version: "2.1.0"
description: 用于查询和管理 Kafka 消费组堆积状态
servers:
- url: https://api.company.com/kafka/v2
description: 生产环境
paths:
/consumer-groups/{groupId}/lag:
get:
summary: 查询消费组堆积详情
parameters:
- name: groupId
in: path
required: true
schema:
type: string
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/GroupLagDetail'
'404':
$ref: '#/components/responses/NotFound'
components:
schemas:
GroupLagDetail:
type: object
properties:
groupId:
type: string
totalLag:
type: integer
partitions:
type: array
items:
$ref: '#/components/schemas/PartitionLag'
PartitionLag:
type: object
properties:
topic:
type: string
partition:
type: integer
lag:
type: integer
responses:
NotFound:
description: 消费组不存在
content:
application/json:
schema:
type: object
properties:
code:
type: integer
example: 404
message:
type: string
example: "Consumer group not found"
swagger-cli validate 或 redocly lint 验证规范合法性;确保所有 $ref 路径正确,响应状态码覆盖 2xx、4xx、5xx 场景。