在Spring-Cloud-Gateway的GatewayAutoConfiguration初始化加載中會加載RouteDefinitionLocator(路由定義信息加載器)信息。spring
//初始化配置路由定義加載器 @Bean @ConditionalOnMissingBean public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator(GatewayProperties properties) { return new PropertiesRouteDefinitionLocator(properties); } //初始化存儲路由定義加載器 @Bean @ConditionalOnMissingBean(RouteDefinitionRepository.class) public InMemoryRouteDefinitionRepository inMemoryRouteDefinitionRepository() { return new InMemoryRouteDefinitionRepository(); } //初始化聚合路由定義加載器 @Bean @Primary public RouteDefinitionLocator routeDefinitionLocator(List<RouteDefinitionLocator> routeDefinitionLocators) { return new CompositeRouteDefinitionLocator(Flux.fromIterable(routeDefinitionLocators)); }
查看RouteDefinitionLocator源碼編程
/** * 路由定義信息的定位器, * 負責讀取路由配置( org.springframework.cloud.gateway.route.RouteDefinition * 子類實現類 * 1.CachingRouteDefinitionLocator -RouteDefinitionLocator包裝類, 緩存目標RouteDefinitionLocator 爲routeDefinitions提供緩存功能 * 2.CompositeRouteDefinitionLocator -RouteDefinitionLocator包裝類,組合多種 RouteDefinitionLocator 的實現,爲 routeDefinitions提供統一入口 * 3.PropertiesRouteDefinitionLocator-從配置文件(GatewayProperties 例如,YML / Properties 等 ) 讀取RouteDefinition * 4.DiscoveryClientRouteDefinitionLocator-從註冊中心( 例如,Eureka / Consul / Zookeeper / Etcd 等 )讀取RouteDefinition * 5.RouteDefinitionRepository-從存儲器( 例如,內存 / Redis / MySQL 等 )讀取RouteDefinition
*/ public interface RouteDefinitionLocator { /** * 獲取RouteDefinition */ Flux<RouteDefinition> getRouteDefinitions(); }
RouteDefinitionLocator接口只有一個方法getRouteDefinitions,此方法獲取RouteDefinition的核心方法,返回Flux<RouteDefinition>緩存
// 從Properties(GatewayProperties)中加載RouteDefinition信息 public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator { // 從appliccation.yml中解析前綴爲spring.cloud.gateway的配置 private final GatewayProperties properties; public PropertiesRouteDefinitionLocator(GatewayProperties properties) { this.properties = properties; } @Override public Flux<RouteDefinition> getRouteDefinitions() { return Flux.fromIterable(this.properties.getRoutes()); } }
// RouteDefinitionLocator 包裝實現類,實現了路由定義的本地緩存功能 public class CachingRouteDefinitionLocator implements RouteDefinitionLocator { // 實際路由定義定位器 private final RouteDefinitionLocator delegate; private final Flux<RouteDefinition> routeDefinitions; // 路由定義的本地緩存 private final Map<String, List> cache = new HashMap<>(); public CachingRouteDefinitionLocator(RouteDefinitionLocator delegate) { this.delegate = delegate; routeDefinitions = CacheFlux.lookup(cache, "routeDefs", RouteDefinition.class) .onCacheMissResume(() -> this.delegate.getRouteDefinitions()); } }
public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLocator { //* 註冊中心客戶端 private final DiscoveryClient discoveryClient; //* 本地配置信息 private final DiscoveryLocatorProperties properties; //* 路由ID前綴 private final String routeIdPrefix; public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { this.discoveryClient = discoveryClient; this.properties = properties; if (StringUtils.hasText(properties.getRouteIdPrefix())) { this.routeIdPrefix = properties.getRouteIdPrefix(); } else { this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_"; } } //* 經過註冊中心查找服務組裝路由定義信息 @Override public Flux<RouteDefinition> getRouteDefinitions() { //... } }
// 組合多個 RouteDefinitionLocator 的實現,爲 routeDefinitions提供統一入口 public class CompositeRouteDefinitionLocator implements RouteDefinitionLocator { /** * 全部路由定義定位器實例集合 */ private final Flux<RouteDefinitionLocator> delegates; public CompositeRouteDefinitionLocator(Flux<RouteDefinitionLocator> delegates) { this.delegates = delegates; } @Override public Flux<RouteDefinition> getRouteDefinitions() { //將各個RouteDefinitionLocator的getRouteDefinitions合併返回統一的Flux<RouteDefinition> return this.delegates.flatMap(RouteDefinitionLocator::getRouteDefinitions); } }
綜合上述app
PropertiesRouteDefinitionLocator-->|配置文件加載初始化| 、RouteDefinitionRepository-->|存儲器中加載初始化|、DiscoveryClientRouteDefinitionLocator-->|註冊中心加載初始化|ide
最終提供經過CompositeRouteDefinitionLocator提供統一的getRouteDefinitions方法this
其中GatewayDiscoveryClientAutoConfiguration以下spa
@Configuration @ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) @AutoConfigureBefore(GatewayAutoConfiguration.class) @ConditionalOnClass({DispatcherHandler.class, DiscoveryClient.class}) @EnableConfigurationProperties public class GatewayDiscoveryClientAutoConfiguration { //初始化註冊中心路由定義定位器 @Bean @ConditionalOnBean(DiscoveryClient.class) @ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled") public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator( DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties); } }