---
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"
---
实现配置变更广播机制,使下游 Gateway 能够监听到配置变更。
@.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
Task 1: 修改 ConfigNotificationService 使用 DbContext 获取连接字符串
src/Services/ConfigNotificationService.cs
修改现有的 PgSqlNotificationService 实现:
1. 修改构造函数:
- 注入 ConsoleDbContext(而非使用 IConfiguration)
- 使用 DbContext.Database.GetConnectionString() 获取连接字符串
2. 移除:
- IConfiguration 依赖
- _configuration.GetConnectionString("DefaultConnection")
3. 示例代码:
```csharp
public PgSqlNotificationService(
ConsoleDbContext dbContext,
ILogger logger)
{
_connectionString = dbContext.Database.GetConnectionString()
?? throw new InvalidOperationException("DefaultConnection not configured");
_logger = logger;
}
```
4. 在 Program.cs 中注册服务时传入 DbContext:
```csharp
services.AddScoped(sp =>
new PgSqlNotificationService(
sp.GetRequiredService(),
sp.GetRequiredService>()));
```
dotnet build --no-restore 2>&1 | head -20
PgSqlNotificationService 已修改为使用 DbContext 获取连接字符串
Task 2: 修改 GatewayService 集成通知服务
src/Services/GatewayService.cs
修改 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": { ... }
}
```
dotnet build --no-restore 2>&1 | head -30
GatewayService 集成通知服务,所有配置变更操作自动触发广播
整体验证:
1. dotnet build 编译通过
2. 手动调用 POST /api/console/gateway/reload 返回成功
3. PostgreSQL 数据库能收到 NOTIFY 消息
- [x] ConfigNotificationService 改为使用 DbContext 获取连接字符串
- [ ] INotificationService 接口定义完成
- [ ] PgSqlNotificationService 实现完成
- [ ] GatewayService 集成通知服务
- [ ] ReloadGatewayAsync 触发广播
- [ ] CRUD 操作自动触发广播
- [ ] 编译通过