refactor: Gateway 改用 PlatformDbContext
Some checks failed
Build / build (push) Failing after 44s
Deploy to K8s / deploy (push) Failing after 2s
Build and Push Docker / build (push) Failing after 17s

- 移除单独的 GatewayDbContext,使用 PlatformDbContext
- 修改 DbSet 名称为 GwTenantRoutes, GwServiceInstances
- 添加必要的 using 引用
This commit is contained in:
movingsam 2026-03-01 01:34:59 +08:00
parent 797ac930bd
commit 0678b44a90
4 changed files with 28 additions and 32 deletions

View File

@ -4,6 +4,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!-- Microsoft Packages --> <!-- Microsoft Packages -->
<PackageVersion Include="Fengling.Platform.Infrastructure" Version="1.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" /> <PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.3" /> <PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" /> <PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />

View File

@ -16,6 +16,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Fengling.Platform.Infrastructure" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
<PackageReference Include="Microsoft.OpenApi" /> <PackageReference Include="Microsoft.OpenApi" />
@ -32,9 +33,5 @@
<PackageReference Include="Swashbuckle.AspNetCore" /> <PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="../../fengling-gateway/src/YarpGateway.csproj" />
<ProjectReference Include="../../fengling-platform/Fengling.Platform.Infrastructure/Fengling.Platform.Infrastructure.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -11,7 +11,7 @@ using Microsoft.IdentityModel.Tokens;
using System.Text; using System.Text;
using NetCorePal.Extensions.DependencyInjection; using NetCorePal.Extensions.DependencyInjection;
using OpenIddict.Validation.AspNetCore; using OpenIddict.Validation.AspNetCore;
using YarpGateway.Data;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -28,8 +28,6 @@ builder.Services.AddDbContext<PlatformDbContext>(options =>
options.EnableDetailedErrors(); options.EnableDetailedErrors();
}); });
builder.Services.AddDbContext<GatewayDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("GatewayConnection")));
// Use Platform's identity // Use Platform's identity
builder.Services.AddIdentity<ApplicationUser, ApplicationRole>() builder.Services.AddIdentity<ApplicationUser, ApplicationRole>()

View File

@ -1,7 +1,7 @@
using Fengling.Platform.Infrastructure;
using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using YarpGateway.Data;
using YarpGateway.Models;
using Fengling.Console.Models.Dtos; using Fengling.Console.Models.Dtos;
namespace Fengling.Console.Services; namespace Fengling.Console.Services;
@ -24,10 +24,10 @@ public interface IGatewayService
public class GatewayService : IGatewayService public class GatewayService : IGatewayService
{ {
private readonly GatewayDbContext _dbContext; private readonly PlatformDbContext _dbContext;
private readonly ILogger<GatewayService> _logger; private readonly ILogger<GatewayService> _logger;
public GatewayService(GatewayDbContext dbContext, ILogger<GatewayService> logger) public GatewayService(PlatformDbContext dbContext, ILogger<GatewayService> logger)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_logger = logger; _logger = logger;
@ -35,8 +35,8 @@ public class GatewayService : IGatewayService
public async Task<GatewayStatisticsDto> GetStatisticsAsync() public async Task<GatewayStatisticsDto> GetStatisticsAsync()
{ {
var routes = await _dbContext.TenantRoutes.Where(r => !r.IsDeleted).ToListAsync(); var routes = await _dbContext.GwTenantRoutes.Where(r => !r.IsDeleted).ToListAsync();
var instances = await _dbContext.ServiceInstances.Where(i => !i.IsDeleted).ToListAsync(); var instances = await _dbContext.GwServiceInstances.Where(i => !i.IsDeleted).ToListAsync();
return new GatewayStatisticsDto return new GatewayStatisticsDto
{ {
@ -55,7 +55,7 @@ public class GatewayService : IGatewayService
public async Task<List<GatewayServiceDto>> GetServicesAsync(bool globalOnly = false, string? tenantCode = null) public async Task<List<GatewayServiceDto>> GetServicesAsync(bool globalOnly = false, string? tenantCode = null)
{ {
var query = _dbContext.TenantRoutes.Where(r => !r.IsDeleted); var query = _dbContext.GwTenantRoutes.Where(r => !r.IsDeleted);
if (globalOnly) if (globalOnly)
query = query.Where(r => r.IsGlobal); query = query.Where(r => r.IsGlobal);
@ -65,7 +65,7 @@ public class GatewayService : IGatewayService
var routes = await query.OrderByDescending(r => r.CreatedTime).ToListAsync(); var routes = await query.OrderByDescending(r => r.CreatedTime).ToListAsync();
var clusters = routes.Select(r => r.ClusterId).Distinct().ToList(); var clusters = routes.Select(r => r.ClusterId).Distinct().ToList();
var instances = await _dbContext.ServiceInstances var instances = await _dbContext.GwServiceInstances
.Where(i => clusters.Contains(i.ClusterId) && !i.IsDeleted) .Where(i => clusters.Contains(i.ClusterId) && !i.IsDeleted)
.GroupBy(i => i.ClusterId) .GroupBy(i => i.ClusterId)
.ToDictionaryAsync(g => g.Key, g => g.Count()); .ToDictionaryAsync(g => g.Key, g => g.Count());
@ -75,7 +75,7 @@ public class GatewayService : IGatewayService
public async Task<GatewayServiceDto?> GetServiceAsync(string serviceName, string? tenantCode = null) public async Task<GatewayServiceDto?> GetServiceAsync(string serviceName, string? tenantCode = null)
{ {
var route = await _dbContext.TenantRoutes var route = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r => .FirstOrDefaultAsync(r =>
r.ServiceName == serviceName && r.ServiceName == serviceName &&
r.IsDeleted == false && r.IsDeleted == false &&
@ -83,7 +83,7 @@ public class GatewayService : IGatewayService
if (route == null) return null; if (route == null) return null;
var instances = await _dbContext.ServiceInstances var instances = await _dbContext.GwServiceInstances
.CountAsync(i => i.ClusterId == route.ClusterId && !i.IsDeleted); .CountAsync(i => i.ClusterId == route.ClusterId && !i.IsDeleted);
return MapToServiceDto(route, instances); return MapToServiceDto(route, instances);
@ -98,7 +98,7 @@ public class GatewayService : IGatewayService
: dto.DestinationId; : dto.DestinationId;
// Check if route already exists // Check if route already exists
var existingRoute = await _dbContext.TenantRoutes var existingRoute = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r => .FirstOrDefaultAsync(r =>
r.ServiceName == dto.ServicePrefix && r.ServiceName == dto.ServicePrefix &&
r.IsGlobal == dto.IsGlobal && r.IsGlobal == dto.IsGlobal &&
@ -122,7 +122,7 @@ public class GatewayService : IGatewayService
Status = 1, Status = 1,
CreatedTime = DateTime.UtcNow CreatedTime = DateTime.UtcNow
}; };
await _dbContext.ServiceInstances.AddAsync(instance); await _dbContext.GwServiceInstances.AddAsync(instance);
// Add route // Add route
var routeId = instanceId + 1; var routeId = instanceId + 1;
@ -138,7 +138,7 @@ public class GatewayService : IGatewayService
IsGlobal = dto.IsGlobal, IsGlobal = dto.IsGlobal,
CreatedTime = DateTime.UtcNow CreatedTime = DateTime.UtcNow
}; };
await _dbContext.TenantRoutes.AddAsync(route); await _dbContext.GwTenantRoutes.AddAsync(route);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
@ -149,7 +149,7 @@ public class GatewayService : IGatewayService
public async Task<bool> UnregisterServiceAsync(string serviceName, string? tenantCode = null) public async Task<bool> UnregisterServiceAsync(string serviceName, string? tenantCode = null)
{ {
var route = await _dbContext.TenantRoutes var route = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r => .FirstOrDefaultAsync(r =>
r.ServiceName == serviceName && r.ServiceName == serviceName &&
r.IsDeleted == false && r.IsDeleted == false &&
@ -162,7 +162,7 @@ public class GatewayService : IGatewayService
route.UpdatedTime = DateTime.UtcNow; route.UpdatedTime = DateTime.UtcNow;
// Soft delete instances // Soft delete instances
var instances = await _dbContext.ServiceInstances var instances = await _dbContext.GwServiceInstances
.Where(i => i.ClusterId == route.ClusterId && !i.IsDeleted) .Where(i => i.ClusterId == route.ClusterId && !i.IsDeleted)
.ToListAsync(); .ToListAsync();
@ -181,7 +181,7 @@ public class GatewayService : IGatewayService
public async Task<List<GatewayRouteDto>> GetRoutesAsync(bool globalOnly = false) public async Task<List<GatewayRouteDto>> GetRoutesAsync(bool globalOnly = false)
{ {
var query = _dbContext.TenantRoutes.Where(r => !r.IsDeleted); var query = _dbContext.GwTenantRoutes.Where(r => !r.IsDeleted);
if (globalOnly) if (globalOnly)
query = query.Where(r => r.IsGlobal); query = query.Where(r => r.IsGlobal);
@ -189,7 +189,7 @@ public class GatewayService : IGatewayService
var routes = await query.OrderByDescending(r => r.Priority).ToListAsync(); var routes = await query.OrderByDescending(r => r.Priority).ToListAsync();
var clusters = routes.Select(r => r.ClusterId).Distinct().ToList(); var clusters = routes.Select(r => r.ClusterId).Distinct().ToList();
var instances = await _dbContext.ServiceInstances var instances = await _dbContext.GwServiceInstances
.Where(i => clusters.Contains(i.ClusterId) && !i.IsDeleted) .Where(i => clusters.Contains(i.ClusterId) && !i.IsDeleted)
.GroupBy(i => i.ClusterId) .GroupBy(i => i.ClusterId)
.ToDictionaryAsync(g => g.Key, g => g.Count()); .ToDictionaryAsync(g => g.Key, g => g.Count());
@ -210,7 +210,7 @@ public class GatewayService : IGatewayService
public async Task<GatewayRouteDto> CreateRouteAsync(CreateGatewayRouteDto dto) public async Task<GatewayRouteDto> CreateRouteAsync(CreateGatewayRouteDto dto)
{ {
var existing = await _dbContext.TenantRoutes var existing = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r => .FirstOrDefaultAsync(r =>
r.ServiceName == dto.ServiceName && r.ServiceName == dto.ServiceName &&
r.IsGlobal == dto.IsGlobal && r.IsGlobal == dto.IsGlobal &&
@ -234,7 +234,7 @@ public class GatewayService : IGatewayService
CreatedTime = DateTime.UtcNow CreatedTime = DateTime.UtcNow
}; };
await _dbContext.TenantRoutes.AddAsync(route); await _dbContext.GwTenantRoutes.AddAsync(route);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
return new GatewayRouteDto return new GatewayRouteDto
@ -253,7 +253,7 @@ public class GatewayService : IGatewayService
public async Task<List<GatewayInstanceDto>> GetInstancesAsync(string clusterId) public async Task<List<GatewayInstanceDto>> GetInstancesAsync(string clusterId)
{ {
var instances = await _dbContext.ServiceInstances var instances = await _dbContext.GwServiceInstances
.Where(i => i.ClusterId == clusterId && !i.IsDeleted) .Where(i => i.ClusterId == clusterId && !i.IsDeleted)
.OrderByDescending(i => i.Weight) .OrderByDescending(i => i.Weight)
.ToListAsync(); .ToListAsync();
@ -273,7 +273,7 @@ public class GatewayService : IGatewayService
public async Task<GatewayInstanceDto> AddInstanceAsync(CreateGatewayInstanceDto dto) public async Task<GatewayInstanceDto> AddInstanceAsync(CreateGatewayInstanceDto dto)
{ {
var existing = await _dbContext.ServiceInstances var existing = await _dbContext.GwServiceInstances
.FirstOrDefaultAsync(i => .FirstOrDefaultAsync(i =>
i.ClusterId == dto.ClusterId && i.ClusterId == dto.ClusterId &&
i.DestinationId == dto.DestinationId && i.DestinationId == dto.DestinationId &&
@ -296,7 +296,7 @@ public class GatewayService : IGatewayService
CreatedTime = DateTime.UtcNow CreatedTime = DateTime.UtcNow
}; };
await _dbContext.ServiceInstances.AddAsync(instance); await _dbContext.GwServiceInstances.AddAsync(instance);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
return new GatewayInstanceDto return new GatewayInstanceDto
@ -314,7 +314,7 @@ public class GatewayService : IGatewayService
public async Task<bool> RemoveInstanceAsync(long instanceId) public async Task<bool> RemoveInstanceAsync(long instanceId)
{ {
var instance = await _dbContext.ServiceInstances.FindAsync(instanceId); var instance = await _dbContext.GwServiceInstances.FindAsync(instanceId);
if (instance == null) return false; if (instance == null) return false;
instance.IsDeleted = true; instance.IsDeleted = true;
@ -326,7 +326,7 @@ public class GatewayService : IGatewayService
public async Task<bool> UpdateInstanceWeightAsync(long instanceId, int weight) public async Task<bool> UpdateInstanceWeightAsync(long instanceId, int weight)
{ {
var instance = await _dbContext.ServiceInstances.FindAsync(instanceId); var instance = await _dbContext.GwServiceInstances.FindAsync(instanceId);
if (instance == null) return false; if (instance == null) return false;
instance.Weight = weight; instance.Weight = weight;