Compare commits
No commits in common. "4dff327fa617818a0a8338d1a172fb25887d7158" and "1b13efffc59e4e8c5d06247f3c87593d17f7ee28" have entirely different histories.
4dff327fa6
...
1b13efffc5
@ -129,30 +129,17 @@ public class RouteCache : IRouteCache
|
|||||||
IsGlobal = route.IsGlobal
|
IsGlobal = route.IsGlobal
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1. 全局路由
|
|
||||||
if (route.IsGlobal)
|
if (route.IsGlobal)
|
||||||
{
|
{
|
||||||
_globalRoutes[route.ServiceName] = routeInfo;
|
_globalRoutes[route.ServiceName] = routeInfo;
|
||||||
_pathRoutes[pathPattern] = routeInfo;
|
_pathRoutes[pathPattern] = routeInfo;
|
||||||
_logger.LogDebug("Loaded global route: {Service} -> {Cluster}",
|
|
||||||
route.ServiceName, route.ClusterId);
|
|
||||||
}
|
}
|
||||||
|
else if (!string.IsNullOrEmpty(route.TenantCode))
|
||||||
// 2. 租户专属路由(无论 IsGlobal 值如何,只要有 TenantCode 就添加到租户路由表)
|
|
||||||
if (!string.IsNullOrEmpty(route.TenantCode))
|
|
||||||
{
|
{
|
||||||
_tenantRoutes.GetOrAdd(route.TenantCode, _ => new ConcurrentDictionary<string, RouteInfo>())
|
_tenantRoutes.GetOrAdd(route.TenantCode, _ => new())
|
||||||
[route.ServiceName] = routeInfo;
|
[route.ServiceName] = routeInfo;
|
||||||
|
|
||||||
// 如果不是全局路由,也添加到路径路由
|
|
||||||
if (!route.IsGlobal)
|
|
||||||
{
|
|
||||||
_pathRoutes[pathPattern] = routeInfo;
|
_pathRoutes[pathPattern] = routeInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogDebug("Loaded tenant route: {Tenant}/{Service} -> {Cluster}",
|
|
||||||
route.TenantCode, route.ServiceName, route.ClusterId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
@ -105,10 +105,11 @@ public class ConfigReloadTests : IDisposable
|
|||||||
var newConfig = configProvider.GetConfig();
|
var newConfig = configProvider.GetConfig();
|
||||||
newConfig.Routes.Count.Should().BeGreaterThanOrEqualTo(initialRouteCount);
|
newConfig.Routes.Count.Should().BeGreaterThanOrEqualTo(initialRouteCount);
|
||||||
|
|
||||||
// 验证可以通过集群ID找到路由
|
// 验证可以通过服务名找到路由
|
||||||
var route = newConfig.Routes.FirstOrDefault(r =>
|
var route = newConfig.Routes.FirstOrDefault(r =>
|
||||||
r.ClusterId == "member-cluster");
|
r.Metadata?.ContainsKey("ServiceName") == true &&
|
||||||
route.Should().NotBeNull("Route with member-cluster should exist after reload");
|
r.Metadata["ServiceName"] == "proxy-reload-test");
|
||||||
|
route.Should().NotBeNull();
|
||||||
|
|
||||||
// 清理
|
// 清理
|
||||||
_dbContext.GwTenantRoutes.Remove(newRoute);
|
_dbContext.GwTenantRoutes.Remove(newRoute);
|
||||||
@ -352,7 +353,7 @@ public class ConfigReloadTests : IDisposable
|
|||||||
// Assert
|
// Assert
|
||||||
var route = routeCache.GetRoute("any-tenant", "notify-test");
|
var route = routeCache.GetRoute("any-tenant", "notify-test");
|
||||||
route.Should().NotBeNull();
|
route.Should().NotBeNull();
|
||||||
route!.ClusterId.Should().Be("member-cluster");
|
route!.ClusterId.Should().Be("test-cluster");
|
||||||
|
|
||||||
// 清理
|
// 清理
|
||||||
_dbContext.GwTenantRoutes.Remove(newRoute);
|
_dbContext.GwTenantRoutes.Remove(newRoute);
|
||||||
|
|||||||
@ -189,13 +189,12 @@ public class K8sDiscoveryTests : IDisposable
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task WhenMultipleServicesDiscovered_ShouldCreateMultiplePendingConfigs()
|
public async Task WhenMultipleServicesDiscovered_ShouldCreateMultiplePendingConfigs()
|
||||||
{
|
{
|
||||||
// Arrange - 使用唯一的命名空间避免与其他测试冲突
|
// Arrange
|
||||||
var uniqueNs = $"test-ns-{Guid.NewGuid():N}";
|
|
||||||
var services = new[]
|
var services = new[]
|
||||||
{
|
{
|
||||||
TestData.CreateRoutedK8sService("service-a", "/api/a", "cluster-a", @namespace: uniqueNs),
|
TestData.CreateRoutedK8sService("service-a", "/api/a", "cluster-a", @namespace: "test-ns"),
|
||||||
TestData.CreateRoutedK8sService("service-b", "/api/b", "cluster-b", @namespace: uniqueNs),
|
TestData.CreateRoutedK8sService("service-b", "/api/b", "cluster-b", @namespace: "test-ns"),
|
||||||
TestData.CreateRoutedK8sService("service-c", "/api/c", "cluster-c", @namespace: uniqueNs)
|
TestData.CreateRoutedK8sService("service-c", "/api/c", "cluster-c", @namespace: "test-ns")
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@ -207,15 +206,11 @@ public class K8sDiscoveryTests : IDisposable
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var pendingConfigs = await _dbContext.PendingServiceDiscoveries
|
var pendingConfigs = await _dbContext.PendingServiceDiscoveries
|
||||||
.Where(s => s.K8sNamespace == uniqueNs)
|
.Where(s => s.K8sNamespace == "test-ns")
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
pendingConfigs.Should().HaveCount(3);
|
pendingConfigs.Should().HaveCount(3);
|
||||||
pendingConfigs.Select(s => s.K8sServiceName).Should().Contain("service-a", "service-b", "service-c");
|
pendingConfigs.Select(s => s.K8sServiceName).Should().Contain("service-a", "service-b", "service-c");
|
||||||
|
|
||||||
// 清理
|
|
||||||
_dbContext.PendingServiceDiscoveries.RemoveRange(pendingConfigs);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user