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>
<ItemGroup>
<!-- 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.Identity.EntityFrameworkCore" Version="10.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
@ -25,4 +26,4 @@
<PackageVersion Include="SkiaSharp" Version="3.119.2" />
<PackageVersion Include="QRCoder" Version="1.7.0" />
</ItemGroup>
</Project>
</Project>

View File

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

View File

@ -11,7 +11,7 @@ using Microsoft.IdentityModel.Tokens;
using System.Text;
using NetCorePal.Extensions.DependencyInjection;
using OpenIddict.Validation.AspNetCore;
using YarpGateway.Data;
var builder = WebApplication.CreateBuilder(args);
@ -28,8 +28,6 @@ builder.Services.AddDbContext<PlatformDbContext>(options =>
options.EnableDetailedErrors();
});
builder.Services.AddDbContext<GatewayDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("GatewayConnection")));
// Use Platform's identity
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 YarpGateway.Data;
using YarpGateway.Models;
using Fengling.Console.Models.Dtos;
namespace Fengling.Console.Services;
@ -24,10 +24,10 @@ public interface IGatewayService
public class GatewayService : IGatewayService
{
private readonly GatewayDbContext _dbContext;
private readonly PlatformDbContext _dbContext;
private readonly ILogger<GatewayService> _logger;
public GatewayService(GatewayDbContext dbContext, ILogger<GatewayService> logger)
public GatewayService(PlatformDbContext dbContext, ILogger<GatewayService> logger)
{
_dbContext = dbContext;
_logger = logger;
@ -35,8 +35,8 @@ public class GatewayService : IGatewayService
public async Task<GatewayStatisticsDto> GetStatisticsAsync()
{
var routes = await _dbContext.TenantRoutes.Where(r => !r.IsDeleted).ToListAsync();
var instances = await _dbContext.ServiceInstances.Where(i => !i.IsDeleted).ToListAsync();
var routes = await _dbContext.GwTenantRoutes.Where(r => !r.IsDeleted).ToListAsync();
var instances = await _dbContext.GwServiceInstances.Where(i => !i.IsDeleted).ToListAsync();
return new GatewayStatisticsDto
{
@ -55,7 +55,7 @@ public class GatewayService : IGatewayService
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)
query = query.Where(r => r.IsGlobal);
@ -65,7 +65,7 @@ public class GatewayService : IGatewayService
var routes = await query.OrderByDescending(r => r.CreatedTime).ToListAsync();
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)
.GroupBy(i => i.ClusterId)
.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)
{
var route = await _dbContext.TenantRoutes
var route = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r =>
r.ServiceName == serviceName &&
r.IsDeleted == false &&
@ -83,7 +83,7 @@ public class GatewayService : IGatewayService
if (route == null) return null;
var instances = await _dbContext.ServiceInstances
var instances = await _dbContext.GwServiceInstances
.CountAsync(i => i.ClusterId == route.ClusterId && !i.IsDeleted);
return MapToServiceDto(route, instances);
@ -98,7 +98,7 @@ public class GatewayService : IGatewayService
: dto.DestinationId;
// Check if route already exists
var existingRoute = await _dbContext.TenantRoutes
var existingRoute = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r =>
r.ServiceName == dto.ServicePrefix &&
r.IsGlobal == dto.IsGlobal &&
@ -122,7 +122,7 @@ public class GatewayService : IGatewayService
Status = 1,
CreatedTime = DateTime.UtcNow
};
await _dbContext.ServiceInstances.AddAsync(instance);
await _dbContext.GwServiceInstances.AddAsync(instance);
// Add route
var routeId = instanceId + 1;
@ -138,7 +138,7 @@ public class GatewayService : IGatewayService
IsGlobal = dto.IsGlobal,
CreatedTime = DateTime.UtcNow
};
await _dbContext.TenantRoutes.AddAsync(route);
await _dbContext.GwTenantRoutes.AddAsync(route);
await _dbContext.SaveChangesAsync();
@ -149,7 +149,7 @@ public class GatewayService : IGatewayService
public async Task<bool> UnregisterServiceAsync(string serviceName, string? tenantCode = null)
{
var route = await _dbContext.TenantRoutes
var route = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r =>
r.ServiceName == serviceName &&
r.IsDeleted == false &&
@ -162,7 +162,7 @@ public class GatewayService : IGatewayService
route.UpdatedTime = DateTime.UtcNow;
// Soft delete instances
var instances = await _dbContext.ServiceInstances
var instances = await _dbContext.GwServiceInstances
.Where(i => i.ClusterId == route.ClusterId && !i.IsDeleted)
.ToListAsync();
@ -181,7 +181,7 @@ public class GatewayService : IGatewayService
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)
query = query.Where(r => r.IsGlobal);
@ -189,7 +189,7 @@ public class GatewayService : IGatewayService
var routes = await query.OrderByDescending(r => r.Priority).ToListAsync();
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)
.GroupBy(i => i.ClusterId)
.ToDictionaryAsync(g => g.Key, g => g.Count());
@ -210,7 +210,7 @@ public class GatewayService : IGatewayService
public async Task<GatewayRouteDto> CreateRouteAsync(CreateGatewayRouteDto dto)
{
var existing = await _dbContext.TenantRoutes
var existing = await _dbContext.GwTenantRoutes
.FirstOrDefaultAsync(r =>
r.ServiceName == dto.ServiceName &&
r.IsGlobal == dto.IsGlobal &&
@ -234,7 +234,7 @@ public class GatewayService : IGatewayService
CreatedTime = DateTime.UtcNow
};
await _dbContext.TenantRoutes.AddAsync(route);
await _dbContext.GwTenantRoutes.AddAsync(route);
await _dbContext.SaveChangesAsync();
return new GatewayRouteDto
@ -253,7 +253,7 @@ public class GatewayService : IGatewayService
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)
.OrderByDescending(i => i.Weight)
.ToListAsync();
@ -273,7 +273,7 @@ public class GatewayService : IGatewayService
public async Task<GatewayInstanceDto> AddInstanceAsync(CreateGatewayInstanceDto dto)
{
var existing = await _dbContext.ServiceInstances
var existing = await _dbContext.GwServiceInstances
.FirstOrDefaultAsync(i =>
i.ClusterId == dto.ClusterId &&
i.DestinationId == dto.DestinationId &&
@ -296,7 +296,7 @@ public class GatewayService : IGatewayService
CreatedTime = DateTime.UtcNow
};
await _dbContext.ServiceInstances.AddAsync(instance);
await _dbContext.GwServiceInstances.AddAsync(instance);
await _dbContext.SaveChangesAsync();
return new GatewayInstanceDto
@ -314,7 +314,7 @@ public class GatewayService : IGatewayService
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;
instance.IsDeleted = true;
@ -326,7 +326,7 @@ public class GatewayService : IGatewayService
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;
instance.Weight = weight;