← 中文资源

简体中文版本

如何用 Schema Registry 版本化 Avro Schema

用 schema registry 安全演进事件 schema:兼容模式、写入与读取 schema,以及发布前的 CI 检查。

阅读英文原文

引言

事件驱动系统依赖对每个事件形状的共享理解。当一个生产者的 schema 变化,每个消费者要么容忍改动要么断裂。schema registry 把该理解变成受管理的契约:schema 被注册与版本化,且在生产者能发布会破坏消费者的 schema 前检查兼容性。Avro 以其写入与读取 schema 分离正是为此演进设计,前提是遵守兼容规则。

为何用 Registry、为何用 Avro

没有 registry 时,每个生产者把 schema 嵌入消息、每个消费者按其碰巧的样子读,意味 schema 变化作为运行时失败而非部署时检查被发现。registry 存储 schema、分配 id、强制兼容,使破坏性变更在发布前被拒绝。

Avro 适合是因为它分离编码数据时用的写入 schema 与解码时用的读取 schema。读取方知道如何把写入方的数据投影到自己的形状,因此用较旧 schema 的读取方能在兼容规则内读较新数据。解码时 registry 按 id 提供写入 schema。

演进规则

Avro 兼容性定义在写入与读取 schema 之间。添加带默认值的字段向后兼容:用新数据的旧读取方得到默认值。移除带默认值的字段向前兼容。重命名字段破坏两者除非用别名。改变类型通常破坏。

这些规则正是默认值要紧的原因:新字段必须有默认值使较旧读取方能解码缺它的消息,被移除的可选字段可为较旧数据设默认。registry 对照最新或所有先前版本强制所选兼容模式,把抽象规则变成注册时检查。

兼容模式

向后兼容意味新消费者能读旧数据,是不断增长的消费者群的常见默认。向前兼容意味旧消费者能读新数据。完全兼容意味两个方向都工作,允许任意发布者与消费者版本共存。你选的模式决定允许哪些改动。

选与你发布现实匹配的模式:若消费者升级慢,你要向后也许完全;若生产者与消费者独立且不同时部署,完全最安全。按 subject 配置模式并记录它,因为改变某 subject 的模式会改变未来发布被允许做什么。

注册与版本化

生产者在其 subject 下注册 schema,通常为主题名加 key 或 value 等策略。registry 分配 schema id 与版本。每条消息携带 schema id,使消费者用产生它的确切写入 schema 解码,无论当前最新版本是什么。

版本化按 subject 单调:兼容的新 schema 成为下一版本。因消息携带 id 而非完整 schema,负载保持小;因 id 解析到特定 schema,消费者不受其尚未采用的后续版本影响。

发布前的 CI 检查

registry 在注册时保护,但注册发生在部署时。加一个 CI 检查把拟议 schema 提交到兼容端点并在不兼容时使构建失败,使生产者作者在拉取请求中而非部署时看到失败。

也在 CI 中对照测试消息校验生产者与消费者 schema,使往返在部署前被证明。通过 registry 兼容检查的 schema 在应用代码中仍可能错,因此 CI 检查补充而非替代 registry。

处理必要的破坏性变更

有时确实需要破坏性变更。用新 subject 或主题做,而非把不兼容变更强加到现有 subject 上,使旧消费者继续工作而新生产者与消费者采用新 schema。随时间迁移消费者,再退役旧 subject。

或者用在新旧形状间映射的转换层,或在过渡期保留两个字段并以截止时间弃用旧的。要点是破坏性变更成为有计划的显式迁移,而非在部署时失败并让所有人意外的不可兼容注册。

常见陷阱与小结

不要无兼容模式地注册;NONE 让破坏性变更通过。若需向后兼容,不要添加无默认值的字段。不要无别名重命名字段。不要只依赖注册时检查;加 CI 检查使作者在部署前抓住它。也不要把破坏性变更强加到现有 subject,新 subject 能安全处理它。

schema registry 把事件 schema 变成受管理、版本化的契约,注册时强制兼容。采用 Avro 因其写入/读取分离、选匹配你发布的兼容模式、为添加字段保留默认值、在 CI 检查兼容、并把偶发的破坏性变更作为带迁移的新 subject 处理。于是生产者与消费者可演进而不互相意外。

代码示例

# with a registry
producer -> register schema v2 -> registry checks compatibility
         -> compatible? assign id, allow -> producer sends id + data
consumer -> fetch schema v2 by id -> read with its own reader schema
# incompatible schema => rejected at registration, before deploy
# the registry is the contract; the id links each message to its schema
// Backward compatible: added field has a default.
{
  "type": "record", "name": "OrderPlaced",
  "fields": [
    { "name": "order_id", "type": "string" },
    { "name": "amount",   "type": "long" },
    { "name": "currency", "type": "string", "default": "USD" }  // safe add
  ]
}
// BREAKING: removing a field with no default, or renaming without an alias.
# registry compatibility modes
NONE          any change allowed (no safety)
BACKWARD      new consumers read old data  (default for many teams)
FORWARD       old consumers read new data
FULL          both  (any producer/consumer version coexist)
# choose per subject from your rollout reality; document it
# changing a subject's mode changes what future publishes may do
# Register a value schema for a topic; the registry checks compatibility.
curl -X POST "$REGISTRY/subjects/orders-value/versions" \
  -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
  -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"
# => { "id": 12 }  (assigns an id; rejects if incompatible with the mode)

# Check compatibility BEFORE registering, in CI:
curl -X POST "$REGISTRY/compatibility/subjects/orders-value/versions/latest" \
  -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
  -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"
# CI step: fail the PR if the schema is not compatible.
- name: Check schema compatibility
  run: |
    curl --fail-with-body -sS -X POST \
      "$REGISTRY/compatibility/subjects/orders-value/versions/latest" \
      -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
      -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"
    # a non-2xx response fails the build -> the author fixes it pre-deploy
# breaking change, done safely
option A: new subject/topic  orders-value-v2
          old consumers keep reading orders-value; new adopt v2
          migrate consumers, then retire orders-value
option B: transition fields   add new field, keep old, dual-write
          deprecate old with a deadline, then remove
# never force an incompatible change into an existing subject