概述
- 目标:通过Schema Registry管理消息模式,设置兼容策略(BACKWARD/FORWARD/FULL),避免破坏性变更影响消费者。
- 适用:Kafka主题的Avro/JSON Schema消息治理与版本演进。
核心与实战
- 注册模式(Avro):
```
curl -s -X POST http://schema-registry:8081/subjects/appdb.public.orders-value/versions \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"schema":"{\"type\":\"record\",\"name\":\"Order\",\"fields\":[{\"name\":\"id\",\"type\":\"long\"},{\"name\":\"status\",\"type\":\"string\"},{\"name\":\"amount\",\"type\":\"double\"}]}"}'
```
- 设置兼容策略(BACKWARD):
```
curl -s -X PUT http://schema-registry:8081/config/appdb.public.orders-value \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"compatibility":"BACKWARD"}'
```
- 新增可选字段(兼容):
```
curl -s -X POST http://schema-registry:8081/subjects/appdb.public.orders-value/versions \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"schema":"{\"type\":\"record\",\"name\":\"Order\",\"fields\":[{\"name\":\"id\",\"type\":\"long\"},{\"name\":\"status\",\"type\":\"string\"},{\"name\":\"amount\",\"type\":\"double\"},{\"name\":\"currency\",\"type\":[\"null\",\"string\"],\"default\":null}]}"}'
```
示例
- 查询subject版本与模式:
```
curl -s http://schema-registry:8081/subjects/appdb.public.orders-value/versions | jq
curl -s http://schema-registry:8081/subjects/appdb.public.orders-value/versions/latest | jq
```
- 兼容性测试:
```
curl -s -X POST http://schema-registry:8081/compatibility/subjects/appdb.public.orders-value/versions/latest \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"schema":"{...新模式...}"}' | jq
```
验证与监控
- 主题绑定:
- 确认生产者与消费者使用同一subject命名规则(`

发表评论 取消回复