一、RouteDefinitionLocator
在Spring-Cloud-Gateway的GatewayAutoConfiguration初始化加载中会加载RouteDefinitionLocator(路由定义信息加载器)信息。
//初始化配置路由定义加载器 @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>
1.1、PropertiesRouteDefinitionLocator--从配置文件读取定义定位器
// 从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()); } }
- GatewayProperties 在GatewayProperties初始化加载,参看:
- Flux 响应式编程
1.2、CachingRouteDefinitionLocator--缓存路由定义定位器
// 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()); } }
1.3、DiscoveryClientRouteDefinitionLocator--配置中心路由定义定位器
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() { //... } }
1.4、CompositeRouteDefinitionLocator--各个定位器合并提供统一的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); } }
综合上述
PropertiesRouteDefinitionLocator-->|配置文件加载初始化| 、RouteDefinitionRepository-->|存储器中加载初始化|、DiscoveryClientRouteDefinitionLocator-->|注册中心加载初始化|
最终提供通过CompositeRouteDefinitionLocator提供统一的getRouteDefinitions方法
其中GatewayDiscoveryClientAutoConfiguration如下
@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); } }
原文地址:https://www.cnblogs.com/bjlhx/p/9785680.html
时间: 2024-10-11 23:12:02