From b66b23191750772c3662258c68cfcc6b3bcaa1dd Mon Sep 17 00:00:00 2001 From: Kimi CLI Date: Sun, 8 Mar 2026 15:21:43 +0800 Subject: [PATCH] refactor: replace GwTenantRoute with GwRoute, change Id type to string - Remove GwTenantRoute (old tenant-specific route entity) - Add GwRoute with string Id (Guid.CreateVersion7) - Update IRouteManager and IRouteStore interfaces - Update PlatformDbContext configuration for new schema - GwRoute is now global, tenant-specific routing moved to GwDestination.TenantCode BREAKING CHANGE: Database schema change requires table recreation --- .../{GwTenantRoute.cs => GwRoute.cs} | 16 +++------ .../IRouteManager.cs | 11 +++---- .../IRouteStore.cs | 17 +++++----- .../PlatformDbContext.cs | 10 +++--- .../RouteManager.cs | 13 +++----- .../RouteStore.cs | 33 +++++++------------ 6 files changed, 38 insertions(+), 62 deletions(-) rename Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/{GwTenantRoute.cs => GwRoute.cs} (87%) diff --git a/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwTenantRoute.cs b/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRoute.cs similarity index 87% rename from Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwTenantRoute.cs rename to Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRoute.cs index 467f09d..8ce8809 100644 --- a/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwTenantRoute.cs +++ b/Fengling.Platform.Domain/AggregatesModel/GatewayAggregate/GwRoute.cs @@ -1,17 +1,13 @@ namespace Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; /// -/// 网关租户路由实体 - 表示路由规则配置 +/// 网关路由实体 - 表示全局路由规则配置 /// -public class GwTenantRoute +public class GwRoute { public string Id { get; set; } = Guid.CreateVersion7().ToString("N"); - /// - /// 租户代码 - /// - public string TenantCode { get; set; } = string.Empty; - + /// /// 服务名称 /// @@ -67,11 +63,7 @@ public class GwTenantRoute /// public int Status { get; set; } = 1; - /// - /// 是否全局路由 - /// - public bool IsGlobal { get; set; } = false; - + /// /// 创建人ID /// diff --git a/Fengling.Platform.Infrastructure/IRouteManager.cs b/Fengling.Platform.Infrastructure/IRouteManager.cs index 4bb7826..b6ed5a1 100644 --- a/Fengling.Platform.Infrastructure/IRouteManager.cs +++ b/Fengling.Platform.Infrastructure/IRouteManager.cs @@ -9,10 +9,9 @@ namespace Fengling.Platform.Infrastructure; /// public interface IRouteManager { - Task FindByIdAsync(string? id, CancellationToken cancellationToken = default); - Task FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default); - Task> GetAllAsync(CancellationToken cancellationToken = default); - Task CreateRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default); - Task UpdateRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default); - Task DeleteRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default); + Task FindByIdAsync(string? id, CancellationToken cancellationToken = default); + Task> GetAllAsync(CancellationToken cancellationToken = default); + Task CreateRouteAsync(GwRoute route, CancellationToken cancellationToken = default); + Task UpdateRouteAsync(GwRoute route, CancellationToken cancellationToken = default); + Task DeleteRouteAsync(GwRoute route, CancellationToken cancellationToken = default); } diff --git a/Fengling.Platform.Infrastructure/IRouteStore.cs b/Fengling.Platform.Infrastructure/IRouteStore.cs index fadeff1..9f3a26a 100644 --- a/Fengling.Platform.Infrastructure/IRouteStore.cs +++ b/Fengling.Platform.Infrastructure/IRouteStore.cs @@ -9,15 +9,14 @@ namespace Fengling.Platform.Infrastructure; /// public interface IRouteStore { - Task FindByIdAsync(string? id, CancellationToken cancellationToken = default); - Task FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default); - Task FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default); - Task> GetAllAsync(CancellationToken cancellationToken = default); - Task> GetPagedAsync(int page, int pageSize, string? tenantCode = null, + Task FindByIdAsync(string? id, CancellationToken cancellationToken = default); + Task FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default); + Task> GetAllAsync(CancellationToken cancellationToken = default); + Task> GetPagedAsync(int page, int pageSize, string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default); - Task GetCountAsync(string? tenantCode = null, string? serviceName = null, + Task GetCountAsync(string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default); - Task CreateAsync(GwTenantRoute route, CancellationToken cancellationToken = default); - Task UpdateAsync(GwTenantRoute route, CancellationToken cancellationToken = default); - Task DeleteAsync(GwTenantRoute route, CancellationToken cancellationToken = default); + Task CreateAsync(GwRoute route, CancellationToken cancellationToken = default); + Task UpdateAsync(GwRoute route, CancellationToken cancellationToken = default); + Task DeleteAsync(GwRoute route, CancellationToken cancellationToken = default); } diff --git a/Fengling.Platform.Infrastructure/PlatformDbContext.cs b/Fengling.Platform.Infrastructure/PlatformDbContext.cs index 75a8095..cd5218a 100644 --- a/Fengling.Platform.Infrastructure/PlatformDbContext.cs +++ b/Fengling.Platform.Infrastructure/PlatformDbContext.cs @@ -18,7 +18,7 @@ public class PlatformDbContext(DbContextOptions options) public DbSet AuditLogs => Set(); // Gateway 实体 - public DbSet GwTenantRoutes => Set(); + public DbSet GwRoutes => Set(); public DbSet GwClusters => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -91,10 +91,10 @@ public class PlatformDbContext(DbContextOptions options) }); // Gateway 实体配置 - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { + entity.ToTable("GwRoutes"); entity.HasKey(e => e.Id); - entity.Property(e => e.TenantCode).HasMaxLength(50); entity.Property(e => e.ServiceName).HasMaxLength(100).IsRequired(); entity.Property(e => e.ClusterId).HasMaxLength(100).IsRequired(); entity.Property(e => e.AuthorizationPolicy).HasMaxLength(100); @@ -132,15 +132,15 @@ public class PlatformDbContext(DbContextOptions options) c => JsonSerializer.Deserialize>(JsonSerializer.Serialize(c, jsonOptions), jsonOptions)!)) .HasColumnType("jsonb"); - entity.HasIndex(e => e.TenantCode); entity.HasIndex(e => e.ServiceName); entity.HasIndex(e => e.ClusterId); - entity.HasIndex(e => new { e.ServiceName, e.IsGlobal, e.Status }); + entity.HasIndex(e => new { e.ServiceName, e.Status }); }); // GwCluster 聚合根配置 modelBuilder.Entity(entity => { + entity.ToTable("ServiceInstances"); entity.HasKey(e => e.Id); entity.Property(e => e.ClusterId).HasMaxLength(100).IsRequired(); entity.Property(e => e.Name).HasMaxLength(100).IsRequired(); diff --git a/Fengling.Platform.Infrastructure/RouteManager.cs b/Fengling.Platform.Infrastructure/RouteManager.cs index aefed8a..9e01988 100644 --- a/Fengling.Platform.Infrastructure/RouteManager.cs +++ b/Fengling.Platform.Infrastructure/RouteManager.cs @@ -15,24 +15,21 @@ public class RouteManager : IRouteManager _store = store; } - public virtual Task FindByIdAsync(string? id, CancellationToken cancellationToken = default) + public virtual Task FindByIdAsync(string? id, CancellationToken cancellationToken = default) => _store.FindByIdAsync(id, cancellationToken); - public virtual Task FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default) - => _store.FindByTenantCodeAsync(tenantCode, cancellationToken); - - public virtual Task> GetAllAsync(CancellationToken cancellationToken = default) + public virtual Task> GetAllAsync(CancellationToken cancellationToken = default) => _store.GetAllAsync(cancellationToken); - public virtual Task CreateRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual Task CreateRouteAsync(GwRoute route, CancellationToken cancellationToken = default) { route.CreatedTime = DateTime.UtcNow; return _store.CreateAsync(route, cancellationToken); } - public virtual Task UpdateRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual Task UpdateRouteAsync(GwRoute route, CancellationToken cancellationToken = default) => _store.UpdateAsync(route, cancellationToken); - public virtual Task DeleteRouteAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual Task DeleteRouteAsync(GwRoute route, CancellationToken cancellationToken = default) => _store.DeleteAsync(route, cancellationToken); } diff --git a/Fengling.Platform.Infrastructure/RouteStore.cs b/Fengling.Platform.Infrastructure/RouteStore.cs index 20aeb68..16b62e2 100644 --- a/Fengling.Platform.Infrastructure/RouteStore.cs +++ b/Fengling.Platform.Infrastructure/RouteStore.cs @@ -11,45 +11,37 @@ public class RouteStore : IRouteStore where TContext : PlatformDbContext { private readonly TContext _context; - private readonly DbSet _routes; + private readonly DbSet _routes; public RouteStore(TContext context) { _context = context; - _routes = context.GwTenantRoutes; + _routes = context.GwRoutes; } public void Dispose() { } - public virtual Task FindByIdAsync(string? id, CancellationToken cancellationToken = default) + public virtual Task FindByIdAsync(string? id, CancellationToken cancellationToken = default) { - if (id == null) return Task.FromResult(null); + if (id == null) return Task.FromResult(null); return _routes.FirstOrDefaultAsync(r => r.Id == id, cancellationToken); } - public virtual Task FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default) - { - return _routes.FirstOrDefaultAsync(r => r.TenantCode == tenantCode && !r.IsDeleted, cancellationToken); - } - - public virtual Task FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default) + public virtual Task FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default) { return _routes.FirstOrDefaultAsync(r => r.ClusterId == clusterId && !r.IsDeleted, cancellationToken); } - public virtual async Task> GetAllAsync(CancellationToken cancellationToken = default) + public virtual async Task> GetAllAsync(CancellationToken cancellationToken = default) { return await _routes.Where(r => !r.IsDeleted).ToListAsync(cancellationToken); } - public virtual async Task> GetPagedAsync(int page, int pageSize, string? tenantCode = null, + public virtual async Task> GetPagedAsync(int page, int pageSize, string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default) { var query = _routes.AsQueryable(); - if (!string.IsNullOrEmpty(tenantCode)) - query = query.Where(r => r.TenantCode.Contains(tenantCode)); - if (!string.IsNullOrEmpty(serviceName)) query = query.Where(r => r.ServiceName.Contains(serviceName)); @@ -64,14 +56,11 @@ public class RouteStore : IRouteStore .ToListAsync(cancellationToken); } - public virtual async Task GetCountAsync(string? tenantCode = null, string? serviceName = null, + public virtual async Task GetCountAsync(string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default) { var query = _routes.AsQueryable(); - if (!string.IsNullOrEmpty(tenantCode)) - query = query.Where(r => r.TenantCode.Contains(tenantCode)); - if (!string.IsNullOrEmpty(serviceName)) query = query.Where(r => r.ServiceName.Contains(serviceName)); @@ -81,14 +70,14 @@ public class RouteStore : IRouteStore return await query.Where(r => !r.IsDeleted).CountAsync(cancellationToken); } - public virtual async Task CreateAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual async Task CreateAsync(GwRoute route, CancellationToken cancellationToken = default) { _routes.Add(route); await _context.SaveChangesAsync(cancellationToken); return IdentityResult.Success; } - public virtual async Task UpdateAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual async Task UpdateAsync(GwRoute route, CancellationToken cancellationToken = default) { route.UpdatedTime = DateTime.UtcNow; _routes.Update(route); @@ -96,7 +85,7 @@ public class RouteStore : IRouteStore return IdentityResult.Success; } - public virtual async Task DeleteAsync(GwTenantRoute route, CancellationToken cancellationToken = default) + public virtual async Task DeleteAsync(GwRoute route, CancellationToken cancellationToken = default) { // 软删除 route.IsDeleted = true;