From 650f2f48e0454fa4eacb8239f270d7288e58613c Mon Sep 17 00:00:00 2001 From: movingsam Date: Sun, 8 Mar 2026 11:05:36 +0800 Subject: [PATCH] fix: improve RouteCache tenant route loading logic - Change from if-else if to separate if statements - Ensure tenant routes are loaded regardless of IsGlobal flag - Add detailed logging for route loading - Fix null check for GetRoute parameters --- src/yarpgateway/Services/RouteCache.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/yarpgateway/Services/RouteCache.cs b/src/yarpgateway/Services/RouteCache.cs index 0656d01..bb2cb44 100644 --- a/src/yarpgateway/Services/RouteCache.cs +++ b/src/yarpgateway/Services/RouteCache.cs @@ -129,16 +129,29 @@ public class RouteCache : IRouteCache IsGlobal = route.IsGlobal }; + // 1. 全局路由 if (route.IsGlobal) { _globalRoutes[route.ServiceName] = 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()) + _tenantRoutes.GetOrAdd(route.TenantCode, _ => new ConcurrentDictionary()) [route.ServiceName] = routeInfo; - _pathRoutes[pathPattern] = routeInfo; + + // 如果不是全局路由,也添加到路径路由 + if (!route.IsGlobal) + { + _pathRoutes[pathPattern] = routeInfo; + } + + _logger.LogDebug("Loaded tenant route: {Tenant}/{Service} -> {Cluster}", + route.TenantCode, route.ServiceName, route.ClusterId); } } }