fengling-console/.planning/phases/01-gateway-config-management/01-PLAN.md

4.3 KiB
Raw Blame History

phase plan type wave depends_on files_modified autonomous requirements user_setup must_haves
01-gateway-config-management 01 execute 1
src/Services/GatewayService.cs
src/Services/ConfigNotificationService.cs
src/Data/ConsoleDbContext.cs
true
truths artifacts key_links
配置变更后下游 Gateway 能收到通知
手动触发 /reload 端点能广播配置变更
自动触发:服务/路由/实例变更时自动广播
path provides contains
src/Services/ConfigNotificationService.cs 配置变更通知服务 INotificationService
path provides contains
src/Services/GatewayService.cs 触发通知逻辑 ReloadGatewayAsync
from to via pattern
GatewayService ConfigNotificationService 依赖注入 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. 示例代码:

    public PgSqlNotificationService(
        ConsoleDbContext dbContext,
        ILogger<PgSqlNotificationService> logger)
    {
        _connectionString = dbContext.Database.GetConnectionString() 
            ?? throw new InvalidOperationException("DefaultConnection not configured");
        _logger = logger;
    }
    
  4. 在 Program.cs 中注册服务时传入 DbContext

    services.AddScoped<INotificationService>(sp => 
        new PgSqlNotificationService(
            sp.GetRequiredService<ConsoleDbContext>(),
            sp.GetRequiredService<ILogger<PgSqlNotificationService>>()));
    
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 格式:

    {
      "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 消息

<success_criteria>

  • ConfigNotificationService 改为使用 DbContext 获取连接字符串
  • INotificationService 接口定义完成
  • PgSqlNotificationService 实现完成
  • GatewayService 集成通知服务
  • ReloadGatewayAsync 触发广播
  • CRUD 操作自动触发广播
  • 编译通过 </success_criteria>
完成后创建 .planning/phases/01-gateway-config-management/01-SUMMARY.md