147 lines
4.3 KiB
Markdown
147 lines
4.3 KiB
Markdown
---
|
||
phase: 01-gateway-config-management
|
||
plan: 01
|
||
type: execute
|
||
wave: 1
|
||
depends_on: []
|
||
files_modified:
|
||
- src/Services/GatewayService.cs
|
||
- src/Services/ConfigNotificationService.cs
|
||
- src/Data/ConsoleDbContext.cs
|
||
autonomous: true
|
||
requirements: []
|
||
user_setup: []
|
||
|
||
must_haves:
|
||
truths:
|
||
- "配置变更后下游 Gateway 能收到通知"
|
||
- "手动触发 /reload 端点能广播配置变更"
|
||
- "自动触发:服务/路由/实例变更时自动广播"
|
||
artifacts:
|
||
- path: "src/Services/ConfigNotificationService.cs"
|
||
provides: "配置变更通知服务"
|
||
contains: "INotificationService"
|
||
- path: "src/Services/GatewayService.cs"
|
||
provides: "触发通知逻辑"
|
||
contains: "ReloadGatewayAsync"
|
||
key_links:
|
||
- from: "GatewayService"
|
||
to: "ConfigNotificationService"
|
||
via: "依赖注入"
|
||
pattern: "INotificationService"
|
||
---
|
||
|
||
<objective>
|
||
实现配置变更广播机制,使下游 Gateway 能够监听到配置变更。
|
||
</objective>
|
||
|
||
<context>
|
||
@.planning/ROADMAP.md
|
||
@.planning/PROJECT.md
|
||
@.planning/STATE.md
|
||
@.planning/phases/01-gateway-config-management/01-CONTEXT.md
|
||
@src/Services/GatewayService.cs
|
||
@src/Controllers/GatewayController.cs
|
||
</context>
|
||
|
||
<tasks>
|
||
|
||
<task type="auto">
|
||
<name>Task 1: 修改 ConfigNotificationService 使用 DbContext 获取连接字符串</name>
|
||
<files>src/Services/ConfigNotificationService.cs</files>
|
||
<action>
|
||
修改现有的 PgSqlNotificationService 实现:
|
||
|
||
1. 修改构造函数:
|
||
- 注入 ConsoleDbContext(而非使用 IConfiguration)
|
||
- 使用 DbContext.Database.GetConnectionString() 获取连接字符串
|
||
|
||
2. 移除:
|
||
- IConfiguration 依赖
|
||
- _configuration.GetConnectionString("DefaultConnection")
|
||
|
||
3. 示例代码:
|
||
```csharp
|
||
public PgSqlNotificationService(
|
||
ConsoleDbContext dbContext,
|
||
ILogger<PgSqlNotificationService> logger)
|
||
{
|
||
_connectionString = dbContext.Database.GetConnectionString()
|
||
?? throw new InvalidOperationException("DefaultConnection not configured");
|
||
_logger = logger;
|
||
}
|
||
```
|
||
|
||
4. 在 Program.cs 中注册服务时传入 DbContext:
|
||
```csharp
|
||
services.AddScoped<INotificationService>(sp =>
|
||
new PgSqlNotificationService(
|
||
sp.GetRequiredService<ConsoleDbContext>(),
|
||
sp.GetRequiredService<ILogger<PgSqlNotificationService>>()));
|
||
```
|
||
</action>
|
||
<verify>
|
||
<automated>dotnet build --no-restore 2>&1 | head -20</automated>
|
||
</verify>
|
||
<done>PgSqlNotificationService 已修改为使用 DbContext 获取连接字符串</done>
|
||
</task>
|
||
|
||
<task type="auto">
|
||
<name>Task 2: 修改 GatewayService 集成通知服务</name>
|
||
<files>src/Services/GatewayService.cs</files>
|
||
<action>
|
||
修改 GatewayService 以集成通知服务:
|
||
|
||
1. 添加 INotificationService 依赖注入到 GatewayService 构造函数
|
||
|
||
2. 修改 ReloadGatewayAsync() 实现:
|
||
- 调用 _notificationService.PublishAsync("gateway_config_changed", JsonSerialize(reloadEvent))
|
||
- 日志记录广播成功
|
||
|
||
3. 在以下 CRUD 操作中添加自动广播(创建/更新/删除后):
|
||
- RegisterServiceAsync - 服务注册
|
||
- UnregisterServiceAsync - 服务注销
|
||
- CreateRouteAsync - 路由创建
|
||
- AddInstanceAsync - 实例添加
|
||
- RemoveInstanceAsync - 实例删除
|
||
- UpdateInstanceWeightAsync - 权重更新
|
||
|
||
4. 事件 Payload 格式:
|
||
```json
|
||
{
|
||
"eventType": "service|route|instance",
|
||
"action": "create|update|delete|reload",
|
||
"timestamp": "2026-03-02T12:00:00Z",
|
||
"details": { ... }
|
||
}
|
||
```
|
||
</action>
|
||
<verify>
|
||
<automated>dotnet build --no-restore 2>&1 | head -30</automated>
|
||
</verify>
|
||
<done>GatewayService 集成通知服务,所有配置变更操作自动触发广播</done>
|
||
</task>
|
||
|
||
</tasks>
|
||
|
||
<verification>
|
||
整体验证:
|
||
1. dotnet build 编译通过
|
||
2. 手动调用 POST /api/console/gateway/reload 返回成功
|
||
3. PostgreSQL 数据库能收到 NOTIFY 消息
|
||
</verification>
|
||
|
||
<success_criteria>
|
||
- [x] ConfigNotificationService 改为使用 DbContext 获取连接字符串
|
||
- [ ] INotificationService 接口定义完成
|
||
- [ ] PgSqlNotificationService 实现完成
|
||
- [ ] GatewayService 集成通知服务
|
||
- [ ] ReloadGatewayAsync 触发广播
|
||
- [ ] CRUD 操作自动触发广播
|
||
- [ ] 编译通过
|
||
</success_criteria>
|
||
|
||
<output>
|
||
完成后创建 .planning/phases/01-gateway-config-management/01-SUMMARY.md
|
||
</output>
|