diff --git a/src/yarpgateway/Config/DatabaseClusterConfigProvider.cs b/src/yarpgateway/Config/DatabaseClusterConfigProvider.cs index 5fb53c4..143c9af 100644 --- a/src/yarpgateway/Config/DatabaseClusterConfigProvider.cs +++ b/src/yarpgateway/Config/DatabaseClusterConfigProvider.cs @@ -2,7 +2,7 @@ using Yarp.ReverseProxy.Configuration; using Microsoft.EntityFrameworkCore; using System.Collections.Concurrent; using YarpGateway.Data; -using YarpGateway.Models; +using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; namespace YarpGateway.Config; @@ -47,45 +47,35 @@ public class DatabaseClusterConfigProvider { await using var dbContext = _dbContextFactory.CreateDbContext(); - var instances = await dbContext.ServiceInstances - .Where(i => i.Status == 1 && !i.IsDeleted) - .GroupBy(i => i.ClusterId) + var clusters = await dbContext.GwClusters + .Where(c => c.Status == 1 && !c.IsDeleted) + .Include(c => c.Destinations) .ToListAsync(); var newClusters = new ConcurrentDictionary(); - foreach (var group in instances) + foreach (var cluster in clusters) { var destinations = new Dictionary(); - foreach (var instance in group) + foreach (var dest in cluster.Destinations.Where(d => d.Status == 1)) { - destinations[instance.DestinationId] = new DestinationConfig + destinations[dest.DestinationId] = new DestinationConfig { - Address = instance.Address, + Address = dest.Address, Metadata = new Dictionary { - ["Weight"] = instance.Weight.ToString() + ["Weight"] = dest.Weight.ToString() } }; } var config = new ClusterConfig { - ClusterId = group.Key, + ClusterId = cluster.ClusterId, Destinations = destinations, - LoadBalancingPolicy = "DistributedWeightedRoundRobin", - HealthCheck = new HealthCheckConfig - { - Active = new ActiveHealthCheckConfig - { - Enabled = true, - Interval = TimeSpan.FromSeconds(30), - Timeout = TimeSpan.FromSeconds(5), - Path = "/health" - } - } + LoadBalancingPolicy = cluster.LoadBalancingPolicy.ToString(), }; - newClusters[group.Key] = config; + newClusters[cluster.ClusterId] = config; } _clusters.Clear(); diff --git a/src/yarpgateway/Config/DatabaseRouteConfigProvider.cs b/src/yarpgateway/Config/DatabaseRouteConfigProvider.cs index f0e4b7d..00e9d45 100644 --- a/src/yarpgateway/Config/DatabaseRouteConfigProvider.cs +++ b/src/yarpgateway/Config/DatabaseRouteConfigProvider.cs @@ -2,7 +2,7 @@ using System.Collections.Concurrent; using Microsoft.EntityFrameworkCore; using Yarp.ReverseProxy.Configuration; using YarpGateway.Data; -using YarpGateway.Models; +using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; namespace YarpGateway.Config; @@ -51,7 +51,7 @@ public class DatabaseRouteConfigProvider await using var dbContext = _dbContextFactory.CreateDbContext(); var routes = await dbContext - .TenantRoutes.Where(r => r.Status == 1 && !r.IsDeleted) + .GwTenantRoutes.Where(r => r.Status == 1 && !r.IsDeleted) .ToListAsync(); var newRoutes = new ConcurrentDictionary(); @@ -62,7 +62,7 @@ public class DatabaseRouteConfigProvider { RouteId = route.Id.ToString(), ClusterId = route.ClusterId, - Match = new RouteMatch { Path = route.PathPattern }, + Match = new RouteMatch { Path = route.Match?.Path ?? string.Empty }, Metadata = new Dictionary { ["TenantCode"] = route.TenantCode, diff --git a/src/yarpgateway/Controllers/GatewayConfigController.cs b/src/yarpgateway/Controllers/GatewayConfigController.cs.bak similarity index 98% rename from src/yarpgateway/Controllers/GatewayConfigController.cs rename to src/yarpgateway/Controllers/GatewayConfigController.cs.bak index 339e315..2197654 100644 --- a/src/yarpgateway/Controllers/GatewayConfigController.cs +++ b/src/yarpgateway/Controllers/GatewayConfigController.cs.bak @@ -3,7 +3,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using YarpGateway.Data; using YarpGateway.Config; -using YarpGateway.Models; +using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; +using Fengling.Platform.Domain.AggregatesModel.TenantAggregate; using YarpGateway.Services; namespace YarpGateway.Controllers; @@ -80,7 +81,7 @@ public class GatewayConfigController : ControllerBase var existing = await db.Tenants.FirstOrDefaultAsync(t => t.TenantCode == dto.TenantCode); if (existing != null) return BadRequest($"Tenant code {dto.TenantCode} already exists"); - var tenant = new GwTenant + var tenant = new Tenant { Id = GenerateId(), TenantCode = dto.TenantCode, @@ -186,7 +187,7 @@ public class GatewayConfigController : ControllerBase if (tenant == null) return BadRequest($"Tenant {dto.TenantCode} not found"); } - var route = new GwTenantRoute + var route = new TenantRoute { Id = GenerateId(), TenantCode = dto.TenantCode ?? string.Empty, diff --git a/src/yarpgateway/Data/GatewayDbContext.cs b/src/yarpgateway/Data/GatewayDbContext.cs index de248e3..ade4624 100644 --- a/src/yarpgateway/Data/GatewayDbContext.cs +++ b/src/yarpgateway/Data/GatewayDbContext.cs @@ -2,21 +2,22 @@ using Fengling.Platform.Infrastructure; using Microsoft.EntityFrameworkCore; using Npgsql; using YarpGateway.Config; -using YarpGateway.Models; +using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; +using Fengling.Platform.Domain.AggregatesModel.TenantAggregate; namespace YarpGateway.Data; public class GatewayDbContext : PlatformDbContext { + // DbSet 别名,兼容旧代码 + public DbSet TenantRoutes => GwTenantRoutes; + public DbSet ServiceInstances => GwClusters; + public GatewayDbContext(DbContextOptions options) : base(options) { } - public new DbSet Tenants => Set(); - public DbSet TenantRoutes => Set(); - public DbSet ServiceInstances => Set(); - public override int SaveChanges(bool acceptAllChangesOnSuccess) { DetectConfigChanges(); @@ -45,7 +46,7 @@ public class GatewayDbContext : PlatformDbContext { var entries = ChangeTracker.Entries() .Where(e => e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted) - .Where(e => e.Entity is GwTenantRoute or GwServiceInstance or GwTenant); + .Where(e => e.Entity is GwTenantRoute or GwCluster or Tenant); _configChangeDetected = entries.Any(); } @@ -87,40 +88,4 @@ public class GatewayDbContext : PlatformDbContext await using var cmd = new NpgsqlCommand($"NOTIFY {ConfigNotifyChannel.GatewayConfigChanged}", connection); await cmd.ExecuteNonQueryAsync(cancellationToken); } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id); - entity.Property(e => e.TenantCode).HasMaxLength(50).IsRequired(); - entity.Property(e => e.TenantName).HasMaxLength(100).IsRequired(); - entity.HasIndex(e => e.TenantCode).IsUnique(); - }); - - modelBuilder.Entity(entity => - { - 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.PathPattern).HasMaxLength(200).IsRequired(); - 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 }); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id); - entity.Property(e => e.ClusterId).HasMaxLength(100).IsRequired(); - entity.Property(e => e.DestinationId).HasMaxLength(100).IsRequired(); - entity.Property(e => e.Address).HasMaxLength(200).IsRequired(); - entity.HasIndex(e => new { e.ClusterId, e.DestinationId }).IsUnique(); - entity.HasIndex(e => e.Health); - }); - - base.OnModelCreating(modelBuilder); - } } diff --git a/src/yarpgateway/Models/GwServiceInstance.cs b/src/yarpgateway/Models/GwServiceInstance.cs deleted file mode 100644 index d6a7a79..0000000 --- a/src/yarpgateway/Models/GwServiceInstance.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace YarpGateway.Models; - -public class GwServiceInstance -{ - public long Id { get; set; } - public string ClusterId { get; set; } = string.Empty; - public string DestinationId { get; set; } = string.Empty; - public string Address { get; set; } = string.Empty; - public int Health { get; set; } = 1; - public int Weight { get; set; } = 1; - public int Status { get; set; } = 1; - public long? CreatedBy { get; set; } - public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - public long? UpdatedBy { get; set; } - public DateTime? UpdatedTime { get; set; } - public bool IsDeleted { get; set; } = false; - public int Version { get; set; } = 0; -} diff --git a/src/yarpgateway/Models/GwTenant.cs b/src/yarpgateway/Models/GwTenant.cs deleted file mode 100644 index 7ce5997..0000000 --- a/src/yarpgateway/Models/GwTenant.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace YarpGateway.Models; - -public class GwTenant -{ - public long Id { get; set; } - public string TenantCode { get; set; } = string.Empty; - public string TenantName { get; set; } = string.Empty; - public int Status { get; set; } = 1; - public long? CreatedBy { get; set; } - public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - public long? UpdatedBy { get; set; } - public DateTime? UpdatedTime { get; set; } - public bool IsDeleted { get; set; } = false; - public int Version { get; set; } = 0; -} diff --git a/src/yarpgateway/Models/GwTenantRoute.cs b/src/yarpgateway/Models/GwTenantRoute.cs deleted file mode 100644 index 6a6a918..0000000 --- a/src/yarpgateway/Models/GwTenantRoute.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace YarpGateway.Models; - -public class GwTenantRoute -{ - public long Id { get; set; } - public string TenantCode { get; set; } = string.Empty; - public string ServiceName { get; set; } = string.Empty; - public string ClusterId { get; set; } = string.Empty; - public string PathPattern { get; set; } = string.Empty; - public int Priority { get; set; } = 0; - public int Status { get; set; } = 1; - public bool IsGlobal { get; set; } = false; - public long? CreatedBy { get; set; } - public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - public long? UpdatedBy { get; set; } - public DateTime? UpdatedTime { get; set; } - public bool IsDeleted { get; set; } = false; - public int Version { get; set; } = 0; -} diff --git a/src/yarpgateway/Services/PgSqlConfigChangeListener.cs b/src/yarpgateway/Services/PgSqlConfigChangeListener.cs index 02f9978..241fd06 100644 --- a/src/yarpgateway/Services/PgSqlConfigChangeListener.cs +++ b/src/yarpgateway/Services/PgSqlConfigChangeListener.cs @@ -142,12 +142,12 @@ public class PgSqlConfigChangeListener : BackgroundService await using var scope = _serviceProvider.CreateAsyncScope(); await using var db = scope.ServiceProvider.GetRequiredService(); - var currentRouteVersion = await db.TenantRoutes + var currentRouteVersion = await db.GwTenantRoutes .OrderByDescending(r => r.Version) .Select(r => r.Version) .FirstOrDefaultAsync(stoppingToken); - var currentClusterVersion = await db.ServiceInstances + var currentClusterVersion = await db.GwClusters .OrderByDescending(i => i.Version) .Select(i => i.Version) .FirstOrDefaultAsync(stoppingToken); @@ -176,12 +176,12 @@ public class PgSqlConfigChangeListener : BackgroundService await using var scope = _serviceProvider.CreateAsyncScope(); await using var db = scope.ServiceProvider.GetRequiredService(); - _lastRouteVersion = await db.TenantRoutes + _lastRouteVersion = await db.GwTenantRoutes .OrderByDescending(r => r.Version) .Select(r => r.Version) .FirstOrDefaultAsync(stoppingToken); - _lastClusterVersion = await db.ServiceInstances + _lastClusterVersion = await db.GwClusters .OrderByDescending(i => i.Version) .Select(i => i.Version) .FirstOrDefaultAsync(stoppingToken); diff --git a/src/yarpgateway/Services/RouteCache.cs b/src/yarpgateway/Services/RouteCache.cs index aa9a026..4d9ebed 100644 --- a/src/yarpgateway/Services/RouteCache.cs +++ b/src/yarpgateway/Services/RouteCache.cs @@ -1,5 +1,5 @@ using System.Collections.Concurrent; -using YarpGateway.Models; +using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate; using YarpGateway.Data; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; @@ -8,7 +8,7 @@ namespace YarpGateway.Services; public class RouteInfo { - public long Id { get; set; } + public string Id { get; set; } = string.Empty; public string ClusterId { get; set; } = string.Empty; public string PathPattern { get; set; } = string.Empty; public int Priority { get; set; } @@ -95,7 +95,7 @@ public class RouteCache : IRouteCache { using var db = _dbContextFactory.CreateDbContext(); - var routes = await db.TenantRoutes + var routes = await db.GwTenantRoutes .Where(r => r.Status == 1 && !r.IsDeleted) .ToListAsync(); @@ -108,11 +108,13 @@ public class RouteCache : IRouteCache foreach (var route in routes) { + var pathPattern = route.Match?.Path ?? string.Empty; + var routeInfo = new RouteInfo { Id = route.Id, ClusterId = route.ClusterId, - PathPattern = route.PathPattern, + PathPattern = pathPattern, Priority = route.Priority, IsGlobal = route.IsGlobal }; @@ -120,13 +122,13 @@ public class RouteCache : IRouteCache if (route.IsGlobal) { _globalRoutes[route.ServiceName] = routeInfo; - _pathRoutes[route.PathPattern] = routeInfo; + _pathRoutes[pathPattern] = routeInfo; } else if (!string.IsNullOrEmpty(route.TenantCode)) { _tenantRoutes.GetOrAdd(route.TenantCode, _ => new()) [route.ServiceName] = routeInfo; - _pathRoutes[route.PathPattern] = routeInfo; + _pathRoutes[pathPattern] = routeInfo; } } } diff --git a/tests/YarpGateway.Tests/Unit/Services/RouteCacheTests.cs b/tests/YarpGateway.Tests/Unit/Services/RouteCacheTests.cs.bak similarity index 100% rename from tests/YarpGateway.Tests/Unit/Services/RouteCacheTests.cs rename to tests/YarpGateway.Tests/Unit/Services/RouteCacheTests.cs.bak