动态配置 (Redis V2)
在 v1.3.0 起,Aidy Gateway 增加了 Redis V2 作为动态配置源。相较于 Redis V1,Redis V2 具有以下优势:
- 控制侧修改配置时,无需每次全量写入所有配置,只需写入变更的配置
- 单一路由的配置异常时不会影响其他路由(v1.4.0 起可以享受这一特性)
时间线
- v1.3.0:初步支持 Redis V2(保持 Redis V1 可用),仍然采用所有路由全量加载的形式
- v1.4.0:增强 Redis V2,采用单一路由变更时热更新的形式
- v1.5.0 / v2.0.0:将 Redis V2 作为默认的动态配置源,移除 Redis V1 的支持(待定)
使用方式
在启动时指定 --config-redis 即代表使用 Redis 作为 Dynamic Config Source(如 --config-redis=redis://localhost:6379/0?key=aidy-gateway-config-v2),且同时需指定 --enable-redis-v2。
使用 Redis V2 作为配置源时,Aidy Gateway 会从 aidy-gateway-config-v2 hash 中读取配置(该 key 可由 --config-redis-key 参数 uri 中的 key query param 指定)。
aidy-gateway-config-v2 的类型应为 hash,包括若干内部字段
当 Aidy Gateway 启动时:
- 如果对应的 Redis 无法连接,会报错退出;
- 作为一个特殊的优化,当配置的 key 不存在时,程序不会报错,而是仅打印「配置 key 不存在」的警告(当后面该 key 出现时,会自动加载对应的配置)。
- 如果配置的 key 存在,但是其不存在任何路由 field(以
route/开头),不会报错。
对于 Redis V2 配置,Aidy Gateway 会自动利用 Redis Keyspace Notifications 能力监控变化。该机制默认不开启,因此需要修改 redis.conf 的 notify-keyspace-events 配置,建议至少开启 Ksh。
Field
Content-Type
参考: 动态配置
在 Redis V1 中,必须存在 content-type field,否则会报错退出。
在 Redis V2 中,这一配置被移除,始终为 application/protobuf+json;因此不再需要 content-type 字段。
Hash
该字段在 Redis V2 中保留(使用 :hash 作为 Field Name);但因不再采用全量更新,该字段可能不再有意义,后续版本中可能会忽略。
Global Plugin Config
Global Plugin Config 现已为可选的,且利用 Protobuf Edition 2023,所有子配置项也都是 optional 的。Aidy 会采用赋值项覆盖静态配置的方式来处理。
目前支持如下配置:
| Field | 类型 |
|---|---|
plugin/log | LogPluginGlobalConfig |
plugin/guard | GuardPluginGlobalConfig |
Route Config
Route Config 现已拆成多个不同的 Field,每个 Field 对应着一个路由的配置
| Field | 类型 |
|---|---|
route/<prefix> | Route |
注:在 Route 配置中,prefix 字段必须以 / 开头,但是在 Field 中的 <prefix> 中则不以 / 开头。
例如,存在如下配置
{
"id": "test-id",
"prefix": "/test-prefix",
// ...
}
则应当指定 route/test-prefix 作为 Field Name、以上的 JSON 作为 Field Value。